go-ssb-room/roomdb/sqlite/notices.go

188 lines
4.1 KiB
Go
Raw Normal View History

package sqlite
import (
"context"
"database/sql"
"fmt"
2021-02-23 19:23:50 +00:00
"github.com/friendsofgo/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
2021-02-23 19:23:50 +00:00
"github.com/volatiletech/sqlboiler/v4/queries/qm"
2021-03-10 15:44:46 +00:00
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
)
// compiler assertion to ensure the struct fullfills the interface
2021-03-10 15:44:46 +00:00
var _ roomdb.PinnedNoticesService = (*PinnedNotices)(nil)
type PinnedNotices struct {
db *sql.DB
}
2021-02-23 19:23:50 +00:00
// List returns a sortable map of all the pinned notices
2021-03-10 15:44:46 +00:00
func (pn PinnedNotices) List(ctx context.Context) (roomdb.PinnedNotices, error) {
2021-02-23 19:23:50 +00:00
// get all the pins and eager-load the related notices
lst, err := models.Pins(qm.Load("Notices")).All(ctx, pn.db)
if err != nil {
return nil, err
}
// prepare a map for them
2021-03-10 15:44:46 +00:00
var pinnedMap = make(roomdb.PinnedNotices, len(lst))
2021-02-23 19:23:50 +00:00
for _, entry := range lst {
// skip a pin if it has no entries
noticeCount := len(entry.R.Notices)
if noticeCount == 0 {
continue
}
2021-03-10 15:44:46 +00:00
name := roomdb.PinnedNoticeName(entry.Name)
2021-02-23 19:23:50 +00:00
// get the exisint map entry or create a new slice
notices, has := pinnedMap[name]
if !has {
2021-03-10 15:44:46 +00:00
notices = make([]roomdb.Notice, 0, noticeCount)
2021-02-23 19:23:50 +00:00
}
// add all the related notice to the slice
for _, n := range entry.R.Notices {
2021-03-10 15:44:46 +00:00
relatedNotice := roomdb.Notice{
2021-02-23 19:23:50 +00:00
ID: n.ID,
Title: n.Title,
Content: n.Content,
Language: n.Language,
}
notices = append(notices, relatedNotice)
}
// update the map
pinnedMap[name] = notices
}
return pinnedMap, nil
}
2021-03-10 15:44:46 +00:00
func (pn PinnedNotices) Get(ctx context.Context, name roomdb.PinnedNoticeName, lang string) (*roomdb.Notice, error) {
2021-02-23 19:23:50 +00:00
p, err := models.Pins(
qm.Where("name = ?", name),
qm.Load("Notices", qm.Where("language = ?", lang)),
).One(ctx, pn.db)
if err != nil {
return nil, err
}
if n := len(p.R.Notices); n != 1 {
return nil, fmt.Errorf("pinnedNotice: expected 1 notice but got %d", n)
}
modelNotice := p.R.Notices[0]
2021-03-10 15:44:46 +00:00
return &roomdb.Notice{
2021-02-23 19:23:50 +00:00
ID: modelNotice.ID,
Title: modelNotice.Title,
Content: modelNotice.Content,
Language: modelNotice.Language,
}, nil
}
2021-03-10 15:44:46 +00:00
func (pn PinnedNotices) Set(ctx context.Context, name roomdb.PinnedNoticeName, noticeID int64) error {
if !name.Valid() {
2021-02-23 19:23:50 +00:00
return fmt.Errorf("pinned notice: invalid notice name: %s", name)
}
n, err := models.FindNotice(ctx, pn.db, noticeID)
if err != nil {
return err
}
p, err := models.Pins(qm.Where("name = ?", name)).One(ctx, pn.db)
if err != nil {
return err
}
2021-02-23 19:23:50 +00:00
err = p.AddNotices(ctx, pn.db, false, n)
if err != nil {
return err
}
return nil
}
// compiler assertion to ensure the struct fullfills the interface
2021-03-10 15:44:46 +00:00
var _ roomdb.NoticesService = (*Notices)(nil)
type Notices struct {
db *sql.DB
}
2021-03-10 15:44:46 +00:00
func (n Notices) GetByID(ctx context.Context, id int64) (roomdb.Notice, error) {
var notice roomdb.Notice
2021-02-23 19:23:50 +00:00
dbEntry, err := models.FindNotice(ctx, n.db, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
2021-03-10 15:44:46 +00:00
return notice, roomdb.ErrNotFound
}
2021-02-23 19:23:50 +00:00
return notice, err
}
// convert models type to admindb type
2021-02-23 19:23:50 +00:00
notice.ID = dbEntry.ID
notice.Title = dbEntry.Title
notice.Language = dbEntry.Language
notice.Content = dbEntry.Content
2021-02-23 19:23:50 +00:00
return notice, nil
}
2021-02-23 19:23:50 +00:00
func (n Notices) RemoveID(ctx context.Context, id int64) error {
dbEntry, err := models.FindNotice(ctx, n.db, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
2021-03-10 15:44:46 +00:00
return roomdb.ErrNotFound
}
return err
}
2021-02-23 19:23:50 +00:00
_, err = dbEntry.Delete(ctx, n.db)
if err != nil {
return err
}
return nil
}
2021-03-10 15:44:46 +00:00
func (n Notices) Save(ctx context.Context, p *roomdb.Notice) error {
if p.ID == 0 {
var newEntry models.Notice
newEntry.Title = p.Title
newEntry.Content = p.Content
newEntry.Language = p.Language
2021-02-23 19:23:50 +00:00
err := newEntry.Insert(ctx, n.db, boil.Whitelist("title", "content", "language"))
if err != nil {
return err
}
p.ID = newEntry.ID
return nil
}
var existing models.Notice
existing.ID = p.ID
existing.Title = p.Title
existing.Content = p.Content
existing.Language = p.Language
2021-02-23 19:23:50 +00:00
_, err := existing.Update(ctx, n.db, boil.Whitelist("title", "content", "language"))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
2021-03-10 15:44:46 +00:00
return roomdb.ErrNotFound
}
return err
}
return nil
}