1
0
justguestbook/guestbook.go

61 lines
1.4 KiB
Go
Raw Normal View History

2022-10-19 03:25:43 +04:00
package guestbook
import (
"errors"
"time"
)
2023-05-22 00:50:11 +04:00
const DateFormat = "2006-01-02 15:04:05"
type Reply struct {
ID int64 `json:"-"`
Created time.Time `json:"created,omitempty"`
Message string `json:"message"`
}
func NewReply(entryID int64, message string) (*Reply, error) {
if message == "" {
return nil, errors.New("empty message field")
}
return &Reply{
ID: entryID,
Created: time.Now().UTC(),
Message: message}, nil
}
2022-10-19 03:25:43 +04:00
type Entry struct {
2023-02-05 16:28:55 +04:00
ID int64 `json:"entry_id"`
Created time.Time `json:"created"`
Name string `json:"name"`
Website string `json:"website,omitempty"`
HideWebsite bool `json:"hide_website,omitempty"`
2023-05-06 22:16:40 +04:00
Message string `json:"message"`
2023-02-05 16:28:55 +04:00
Reply *Reply `json:"reply,omitempty"`
2022-10-19 03:25:43 +04:00
}
func NewEntry(name, message, website string, hideWebsite bool) (*Entry, error) {
2022-10-19 03:25:43 +04:00
if name == "" || message == "" {
return nil, errors.New("name and message field are required")
}
return &Entry{
2023-02-05 16:28:55 +04:00
Created: time.Now().UTC(),
2022-10-19 03:25:43 +04:00
Name: name,
Website: website,
HideWebsite: hideWebsite,
Message: message}, nil
}
2023-05-22 00:50:11 +04:00
type Guestbook interface {
Entries(page, pageSize int64) ([]*Entry, error)
Count() (int64, error)
NewEntry(entry *Entry) error
EditEntry(entry *Entry) error
DeleteEntry(entryID int64) error
NewReply(reply *Reply) error
EditReply(reply *Reply) error
DeleteReply(entryID int64) error
Close() error
}