update item status

This commit is contained in:
Nazar Kanaev
2020-07-05 14:38:50 +01:00
parent 7ecdada4ca
commit 533debf708
6 changed files with 52 additions and 8 deletions

View File

@@ -32,7 +32,7 @@ func (s ItemStatus) MarshalJSON() ([]byte, error) {
func (s *ItemStatus) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &s); err != nil {
if err := json.Unmarshal(b, &str); err != nil {
return err
}
*s = StatusValues[str]
@@ -40,7 +40,8 @@ func (s *ItemStatus) UnmarshalJSON(b []byte) error {
}
type Item struct {
Id string `json:"id"`
Id int64 `json:"id"`
GUID string `json:"guid"`
FeedId int64 `json:"feed_id"`
Title string `json:"title"`
Link string `json:"link"`
@@ -62,12 +63,12 @@ func (s *Storage) CreateItems(items []Item) bool {
for _, item := range items {
_, err = tx.Exec(`
insert into items (
id, feed_id, title, link, description,
guid, feed_id, title, link, description,
content, author, date, date_updated, status, image
)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
on conflict (id) do update set date_updated=?`,
item.Id, item.FeedId, item.Title, item.Link, item.Description,
on conflict (guid) do update set date_updated=?`,
item.GUID, item.FeedId, item.Title, item.Link, item.Description,
item.Content, item.Author, item.Date, item.DateUpdated, UNREAD, item.Image,
// upsert values
item.DateUpdated,
@@ -92,7 +93,7 @@ func itemQuery(s *Storage, cond string, v ...interface{}) []Item {
result := make([]Item, 0, 0)
query := fmt.Sprintf(`
select
id, feed_id, title, link, description,
id, guid, feed_id, title, link, description,
content, author, date, date_updated, status, image
from items
where %s`, cond)
@@ -105,6 +106,7 @@ func itemQuery(s *Storage, cond string, v ...interface{}) []Item {
var x Item
err = rows.Scan(
&x.Id,
&x.GUID,
&x.FeedId,
&x.Title,
&x.Link,
@@ -144,3 +146,8 @@ func (s *Storage) ListFeedItems(feed_id int64) []Item {
func (s *Storage) ListFeedItemsFiltered(feed_id int64, status ItemStatus) []Item {
return itemQuery(s, `feed_id = ? and status = ?`, feed_id, status)
}
func (s *Storage) UpdateItemStatus(item_id int64, status ItemStatus) bool {
_, err := s.db.Exec(`update items set status = ? where id = ?`, status, item_id)
return err == nil
}

View File

@@ -28,7 +28,8 @@ create index if not exists idx_feed_folder_id on feeds(folder_id);
create unique index if not exists idx_feed_feed_link on feeds(feed_link);
create table if not exists items (
id string primary key,
id integer primary key autoincrement,
guid string not null,
feed_id references feeds(id),
title text,
link text,
@@ -43,6 +44,7 @@ create table if not exists items (
create index if not exists idx_item_feed_id on items(feed_id);
create index if not exists idx_item_status on items(status);
create unique index if not exists idx_item_guid on items(guid);
`
type Storage struct {