80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package storage
|
|
|
|
import "context"
|
|
|
|
type AdminFAQ struct {
|
|
ID int64 `json:"id"`
|
|
Question string `json:"question"`
|
|
Answer string `json:"answer"`
|
|
ServiceID *int64 `json:"service_id"`
|
|
ServiceName string `json:"service_name,omitempty"`
|
|
Sort int `json:"sort"`
|
|
Published bool `json:"published"`
|
|
}
|
|
|
|
func (s *Storage) AdminFAQ(ctx context.Context) ([]AdminFAQ, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT f.id, f.question, f.answer, f.service_id, COALESCE(s.name, ''), f.sort, f.published
|
|
FROM faq f
|
|
LEFT JOIN services s ON s.id = f.service_id
|
|
ORDER BY f.sort, f.id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []AdminFAQ
|
|
for rows.Next() {
|
|
var f AdminFAQ
|
|
if err := rows.Scan(&f.ID, &f.Question, &f.Answer, &f.ServiceID, &f.ServiceName,
|
|
&f.Sort, &f.Published); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, f)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
type FAQInput struct {
|
|
Question string
|
|
Answer string
|
|
ServiceID *int64
|
|
Sort int
|
|
Published bool
|
|
}
|
|
|
|
func (s *Storage) AdminCreateFAQ(ctx context.Context, in FAQInput) (int64, error) {
|
|
var id int64
|
|
err := s.pool.QueryRow(ctx, `
|
|
INSERT INTO faq (question, answer, service_id, sort, published)
|
|
VALUES ($1,$2,$3,$4,$5)
|
|
RETURNING id`,
|
|
in.Question, in.Answer, in.ServiceID, in.Sort, in.Published).Scan(&id)
|
|
return id, wrapConflict(err)
|
|
}
|
|
|
|
func (s *Storage) AdminUpdateFAQ(ctx context.Context, id int64, in FAQInput) error {
|
|
tag, err := s.pool.Exec(ctx, `
|
|
UPDATE faq SET question=$2, answer=$3, service_id=$4, sort=$5, published=$6
|
|
WHERE id=$1`,
|
|
id, in.Question, in.Answer, in.ServiceID, in.Sort, in.Published)
|
|
if err != nil {
|
|
return wrapConflict(err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Storage) AdminDeleteFAQ(ctx context.Context, id int64) error {
|
|
tag, err := s.pool.Exec(ctx, `DELETE FROM faq WHERE id=$1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|