diff --git a/main.go b/main.go index 8e17e96..6081474 100644 --- a/main.go +++ b/main.go @@ -136,6 +136,11 @@ func main() { continue } } + + if err := m.UpdateRefreshedAt(item.ID, time.Now()); err != nil { + log.Printf("couldn't update refreshed_at for %d: %v\n", item.ID, err) + continue + } } time.Sleep(10 * time.Second) diff --git a/model/items.go b/model/items.go index c6023bc..616cb6f 100644 --- a/model/items.go +++ b/model/items.go @@ -1,6 +1,9 @@ package model -import "fmt" +import ( + "fmt" + "time" +) func (m *Model) GetItems() ([]*Item, error) { rows, err := m.db.Queryx("SELECT * FROM items") @@ -19,3 +22,14 @@ func (m *Model) GetItems() ([]*Item, error) { return items, nil } + +func (m *Model) UpdateRefreshedAt(itemId int64, refreshedAt time.Time) error { + if _, err := m.db.NamedExec("UPDATE items SET refreshed_at = :refreshed_at WHERE id = :item_id", map[string]any{ + "refreshed_at": refreshedAt, + "item_id": itemId, + }); err != nil { + return fmt.Errorf("failed to update refreshed_at field: %v", err) + } + + return nil +} diff --git a/model/model.go b/model/model.go index d1c7779..b723f08 100644 --- a/model/model.go +++ b/model/model.go @@ -28,11 +28,12 @@ type Torrent struct { } type Item struct { - ID int64 `json:"id" db:"id"` - Query string `json:"query" db:"query"` - Category int `json:"category" db:"category"` - CreatedAt time.Time `json:"createdAt" db:"created_at"` - Torrents []Torrent `json:"torrents,omitempty"` + ID int64 `json:"id" db:"id"` + Query string `json:"query" db:"query"` + Category int `json:"category" db:"category"` + CreatedAt time.Time `json:"createdAt" db:"created_at"` + Torrents []Torrent `json:"torrents,omitempty"` + RefreshedAt *time.Time `json:"refreshedAt" db:"refreshed_at"` } func (m *Model) Init() { @@ -59,6 +60,8 @@ func (m *Model) Init() { item_id integer not null, FOREIGN KEY (item_id) REFERENCES users(id) )`) + + m.db.Exec(`ALTER TABLE items ADD COLUMN refreshed_at datetime default null`) } func New(db *sqlx.DB) *Model { diff --git a/web/src/api/useItemsQuery.ts b/web/src/api/useItemsQuery.ts index 756ecd2..da75525 100644 --- a/web/src/api/useItemsQuery.ts +++ b/web/src/api/useItemsQuery.ts @@ -4,6 +4,7 @@ export type ItemDetails = { id: number; query: string; createdAt: string; + refreshedAt: string | null; }; export const useItemsQuery = () => { diff --git a/web/src/components/Item.tsx b/web/src/components/Item.tsx index 56044a5..f5b4139 100644 --- a/web/src/components/Item.tsx +++ b/web/src/components/Item.tsx @@ -12,9 +12,12 @@ import { useDownloadTorrentMutation } from "../api/useDownloadTorrentMutation"; import { useDeleteItemMutation } from "../api/useDeleteItemMutation"; import { useQueryClient } from "@tanstack/react-query"; import dayjs from "dayjs"; +import relativeTime from "dayjs/plugin/relativeTime"; import { humanFileSize } from "../utils/humanFileSize"; import { useDeleteTorrentMutation } from "../api/useDeleteTorrentMutation"; +dayjs.extend(relativeTime); + export type ItemProps = { item: ItemDetails; }; @@ -79,7 +82,16 @@ export const Item = ({ item }: ItemProps) => { {open && (