Skip to content

Commit

Permalink
Added "Last Trip" and "Last Visit" fields
Browse files Browse the repository at this point in the history
To Caves and Cavers.
  • Loading branch information
IdlePhysicist committed Aug 3, 2021
1 parent f4245f2 commit 9cb7c83
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 50 deletions.
12 changes: 9 additions & 3 deletions internal/gui/cavers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type cavers struct {
*tview.Table
cavers chan *model.Caver
cavers chan *model.Caver
filterCol, filterTerm string
}

Expand All @@ -22,8 +22,8 @@ func newCavers(g *Gui) *cavers {
Table: tview.NewTable().
SetScrollBarVisibility(tview.ScrollBarNever).
SetSelectable(true, false).
Select(0,0).
SetFixed(1,1),
Select(0, 0).
SetFixed(1, 1),
}

cavers.SetTitle(``).SetTitleAlign(tview.AlignLeft)
Expand Down Expand Up @@ -70,6 +70,7 @@ func (c *cavers) setEntries(g *Gui) {
"Name",
"Club",
"Count",
"Last Trip",
}

for i, header := range headers {
Expand Down Expand Up @@ -98,6 +99,11 @@ func (c *cavers) setEntries(g *Gui) {
SetTextColor(tview.Styles.PrimaryTextColor).
SetMaxWidth(0).
SetExpansion(1))

table.SetCell(i+1, 3, tview.NewTableCell(caver.LastTrip).
SetTextColor(tview.Styles.PrimaryTextColor).
SetMaxWidth(0).
SetExpansion(1))
}
}

Expand Down
12 changes: 9 additions & 3 deletions internal/gui/caves.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type caves struct {
*tview.Table
caves chan *model.Cave
caves chan *model.Cave
filterCol, filterTerm string
}

Expand All @@ -22,8 +22,8 @@ func newCaves(g *Gui) *caves {
Table: tview.NewTable().
SetScrollBarVisibility(tview.ScrollBarNever).
SetSelectable(true, false).
Select(0,0).
SetFixed(1,1),
Select(0, 0).
SetFixed(1, 1),
}

caves.SetTitle(``).SetTitleAlign(tview.AlignLeft)
Expand Down Expand Up @@ -88,6 +88,7 @@ func (c *caves) setEntries(g *Gui) {
"Country",
"SRT",
"Visits",
"Last Visit",
}

for i, header := range headers {
Expand Down Expand Up @@ -126,6 +127,11 @@ func (c *caves) setEntries(g *Gui) {
SetTextColor(tview.Styles.PrimaryTextColor).
SetMaxWidth(0).
SetExpansion(1))

table.SetCell(i+1, 5, tview.NewTableCell(cave.LastVisit).
SetTextColor(tview.Styles.PrimaryTextColor).
SetMaxWidth(0).
SetExpansion(1))
}
}

Expand Down
17 changes: 9 additions & 8 deletions internal/gui/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package gui
import (
"fmt"

tview "gitlab.com/tslocum/cview"
"github.com/gdamore/tcell/v2"
tview "gitlab.com/tslocum/cview"

"github.com/idlephysicist/cave-logger/internal/model"
)

var inspectorFormat = map[string]string{
`trips` : "\tDate: %s\n\tCave: %s\n\tCavers: %s\n\tNotes: %s",
`cavers`: "\tName: %s\n\tClub: %s\n\tCount: %d\n\tNotes: %s",
`caves` : "\tName: %s\n\tRegion: %s\n\tCountry: %s\n\tSRT: %s\n\tVisits: %d\n\tNotes: %s",
`trips`: "\tDate: %s\n\tCave: %s\n\tCavers: %s\n\tNotes: %s",
`cavers`: "\tName: %s\n\tClub: %s\n\tCount: %d\n\tLast Trip: %s\n\tNotes: %s",
`caves`: "\tName: %s\n\tRegion: %s\n\tCountry: %s\n\tSRT: %s\n\tVisits: %d\n\tLast Visit: %s\n\tNotes: %s",
}

func (g *Gui) displayInspect(data, page string) {
Expand All @@ -39,7 +39,7 @@ func (g *Gui) inspectTrip() {
selected := g.selectedTrip()

if selected == nil {
g.warning("No trips in table", `trips`, []string{`OK`}, func() {return})
g.warning("No trips in table", `trips`, []string{`OK`}, func() { return })
return
}

Expand Down Expand Up @@ -81,10 +81,11 @@ func (g *Gui) formatTrip(t *model.Log) string {
}

func (g *Gui) formatCave(c *model.Cave) string {
return fmt.Sprintf(inspectorFormat[`caves`], c.Name, c.Region, c.Country, yesOrNo(c.SRT), c.Visits, c.Notes)
return fmt.Sprintf(inspectorFormat[`caves`],
c.Name, c.Region, c.Country, yesOrNo(c.SRT), c.Visits, c.LastVisit, c.Notes,
)
}

func (g *Gui) formatPerson(c *model.Caver) string {
return fmt.Sprintf(inspectorFormat[`cavers`], c.Name, c.Club, c.Count, c.Notes)
return fmt.Sprintf(inspectorFormat[`cavers`], c.Name, c.Club, c.Count, c.LastTrip, c.Notes)
}

6 changes: 3 additions & 3 deletions internal/gui/trips.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type trips struct {
*tview.Table
trips chan *model.Log
trips chan *model.Log
filterCol, filterTerm string
}

Expand All @@ -21,8 +21,8 @@ func newTrips(g *Gui) *trips {
Table: tview.NewTable().
SetScrollBarVisibility(tview.ScrollBarNever).
SetSelectable(true, false).
Select(0,0).
SetFixed(1,1),
Select(0, 0).
SetFixed(1, 1),
trips: make(chan *model.Log),
}

Expand Down
36 changes: 19 additions & 17 deletions internal/model/register.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package model

type Cave struct {
ID string
Name string
Region string
Country string
SRT bool
Visits int64
Notes string
ID string
Name string
Region string
Country string
SRT bool
Visits int64
LastVisit string
Notes string
}

type Caver struct {
ID string
Name string
Club string
Count int64
Notes string
ID string
Name string
Club string
Count int64
LastTrip string
Notes string
}

type Log struct {
ID string
Date string
Cave string
Names string // `, ` sep
Notes string
ID string
Date string
Cave string
Names string // `, ` sep
Notes string
}
58 changes: 42 additions & 16 deletions internal/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"time"

_ "modernc.org/sqlite"
"github.com/sirupsen/logrus"
_ "modernc.org/sqlite"

"github.com/idlephysicist/cave-logger/internal/model"
)
Expand All @@ -16,9 +16,9 @@ const datetime = `2006-01-02T15:04:05Z`
const date = `2006-01-02`

type Register struct {
log *logrus.Logger
db *sql.DB
ctx context.Context
log *logrus.Logger
db *sql.DB
ctx context.Context
}

func New(log *logrus.Logger, dbFN string) *Register {
Expand Down Expand Up @@ -245,15 +245,23 @@ func (reg *Register) GetTrip(id string) (*model.Log, error) {

func (reg *Register) GetAllCavers() ([]*model.Caver, error) {
query := `
SELECT
SELECT
people.id AS 'id',
people.name AS 'name',
people.club AS 'club',
(
SELECT COUNT(1)
FROM trip_groups
WHERE trip_groups.caverid = people.id
)
),
(
SELECT printf("%s in %s", date(trips.date, 'unixepoch'), locations.name)
FROM trips, trip_groups, locations
WHERE trips.caveid == locations.id
AND trips.id == trip_groups.tripid
AND trip_groups.caverid == people.id
ORDER BY trips.date DESC, trips.id DESC LIMIT 1
) AS 'last_trip'
FROM people
ORDER BY name`

Expand All @@ -266,7 +274,7 @@ func (reg *Register) GetAllCavers() ([]*model.Caver, error) {
for result.Next() {
var c model.Caver

err = result.Scan(&c.ID, &c.Name, &c.Club, &c.Count)
err = result.Scan(&c.ID, &c.Name, &c.Club, &c.Count, &c.LastTrip)
if err != nil {
reg.log.Errorf("Scan: %v", err)
}
Expand All @@ -279,16 +287,23 @@ func (reg *Register) GetAllCavers() ([]*model.Caver, error) {
return cavers, err
}


func (reg *Register) GetCaver(id string) (*model.Caver, error) {
query := `
SELECT
SELECT
people.id AS 'id',
people.name AS 'name',
people.club AS 'club',
(
SELECT COUNT(1) FROM trip_groups WHERE trip_groups.caverid = people.id
) AS 'count',
(
SELECT printf("%s in %s", date(trips.date, 'unixepoch'), locations.name)
FROM trips, trip_groups, locations
WHERE trips.caveid == locations.id
AND trips.id == trip_groups.tripid
AND trip_groups.caverid == people.id
ORDER BY trips.date DESC, trips.id DESC LIMIT 1
) AS 'last_trip',
people.notes AS 'notes'
FROM people
WHERE people.id = ?`
Expand All @@ -303,7 +318,7 @@ func (reg *Register) GetCaver(id string) (*model.Caver, error) {
var caver model.Caver
for result.Next() {

err = result.Scan(&caver.ID, &caver.Name, &caver.Club, &caver.Count, &caver.Notes)
err = result.Scan(&caver.ID, &caver.Name, &caver.Club, &caver.Count, &caver.LastTrip, &caver.Notes)
if err != nil {
reg.log.Errorf("reg.scan", err)
return nil, err
Expand Down Expand Up @@ -332,19 +347,25 @@ func (reg *Register) GetAllCaves() ([]*model.Cave, error) {
SELECT COUNT(1)
FROM trips
WHERE trips.caveid = locations.id
) AS 'visits'
) AS 'visits',
(
SELECT date(trips.date, 'unixepoch')
FROM trips
WHERE trips.caveid = locations.id
ORDER BY trips.date DESC LIMIT 1
) AS 'last_visit'
FROM locations
ORDER BY name`
result, err := reg.db.Query(query)
if err != nil {
reg.log.Errorf("reg.getcaverlist: Failed to get cavers", err)
reg.log.Errorf("reg.getcavelist: Failed to get caves", err)
}

caves := make([]*model.Cave, 0)
for result.Next() {
var c model.Cave

err = result.Scan(&c.ID, &c.Name, &c.Region, &c.Country, &c.SRT, &c.Visits)
err = result.Scan(&c.ID, &c.Name, &c.Region, &c.Country, &c.SRT, &c.Visits, &c.LastVisit)
if err != nil {
reg.log.Errorf("Scan: %v", err)
}
Expand All @@ -357,7 +378,6 @@ func (reg *Register) GetAllCaves() ([]*model.Cave, error) {
return caves, err
}


func (reg *Register) GetCave(id string) (*model.Cave, error) {
query := `
SELECT
Expand All @@ -369,6 +389,12 @@ func (reg *Register) GetCave(id string) (*model.Cave, error) {
(
SELECT COUNT(1) FROM trips WHERE trips.caveid = locations.id
) AS 'visits',
(
SELECT date(trips.date, 'unixepoch')
FROM trips
WHERE trips.caveid = locations.id
ORDER BY trips.date DESC LIMIT 1
) AS 'last_visit',
locations.notes AS 'notes'
FROM locations
WHERE id = ?`
Expand All @@ -382,7 +408,7 @@ func (reg *Register) GetCave(id string) (*model.Cave, error) {

var cave model.Cave
for result.Next() {
err = result.Scan(&cave.ID, &cave.Name, &cave.Region, &cave.Country, &cave.SRT, &cave.Visits, &cave.Notes)
err = result.Scan(&cave.ID, &cave.Name, &cave.Region, &cave.Country, &cave.SRT, &cave.Visits, &cave.LastVisit, &cave.Notes)
if err != nil {
reg.log.Error(err)
return nil, err
Expand All @@ -396,7 +422,6 @@ func (reg *Register) GetCave(id string) (*model.Cave, error) {
return &cave, err
}


//
// DELETE FUNCS ---- ----

Expand Down Expand Up @@ -597,6 +622,7 @@ func (reg *Register) ModifyCave(id, name, region, country, notes string, srt boo

return nil
}

//
// INTERNAL FUNCTIONS ----------------------------------------------------------
//
Expand Down

0 comments on commit 9cb7c83

Please sign in to comment.