2023-05-22 01:01:10 +04:00
|
|
|
package justguestbook
|
2022-10-19 03:25:43 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
const (
|
|
|
|
// DateFormat is a format of the date and time used in a guestbook.
|
|
|
|
DateFormat = "2006-01-02 15:04:05"
|
2023-05-22 00:50:11 +04:00
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
|
|
|
|
// Reply holds a reply for a guestbook's entry.
|
2023-05-22 00:50:11 +04:00
|
|
|
type Reply struct {
|
2023-05-22 02:35:07 +04:00
|
|
|
// ID of a guestbook's entry is used when update of a reply is performed.
|
|
|
|
//
|
|
|
|
// Not present in JSON because a reply comes within its corresponding entry.
|
|
|
|
ID int64 `json:"-"`
|
|
|
|
|
|
|
|
// Created holds a date and time when a reply was created.
|
2023-05-22 00:50:11 +04:00
|
|
|
Created time.Time `json:"created,omitempty"`
|
2023-05-22 02:35:07 +04:00
|
|
|
|
|
|
|
// Message holds a reply's message.
|
|
|
|
Message string `json:"message"`
|
2023-05-22 00:50:11 +04:00
|
|
|
}
|
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
// NewReply creates a new reply and, before that, verifies that a passed message
|
|
|
|
// is not empty.
|
2023-05-22 00:50:11 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
// Entry holds a guestbook entry.
|
2022-10-19 03:25:43 +04:00
|
|
|
type Entry struct {
|
2023-05-22 02:35:07 +04:00
|
|
|
// ID of an entry.
|
|
|
|
ID int64 `json:"entry_id"`
|
|
|
|
|
|
|
|
// Created holds the date and time when an entry was created.
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
|
|
|
|
// Name holds a nick-/name of a guest who posted an entry.
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
// Website of a guest. Can be empty.
|
|
|
|
Website string `json:"website,omitempty"`
|
|
|
|
|
|
|
|
// HideWebsite tells wether to hide or show guest's website.
|
|
|
|
HideWebsite bool `json:"hide_website,omitempty"`
|
|
|
|
|
|
|
|
// Message that a guest wrote.
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
|
|
// Reply holds a reply for this entry.
|
|
|
|
Reply *Reply `json:"reply,omitempty"`
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
// NewEntry creates a new Entry and, before that, verifies if the name and message
|
|
|
|
// aren't empty and the website field do not exceed the limit.
|
2022-10-21 03:01:01 +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
|
|
|
|
2023-05-22 02:35:07 +04:00
|
|
|
// Guestbook is an interface that should be implemented by a DB.
|
2023-05-22 00:50:11 +04:00
|
|
|
type Guestbook interface {
|
2023-05-22 02:35:07 +04:00
|
|
|
// Entries returns a slice of guestbook entries.
|
2023-05-22 00:50:11 +04:00
|
|
|
Entries(page, pageSize int64) ([]*Entry, error)
|
2023-05-22 02:35:07 +04:00
|
|
|
// Count returns how much entries are in a DB.
|
2023-05-22 00:50:11 +04:00
|
|
|
Count() (int64, error)
|
2023-05-22 02:35:07 +04:00
|
|
|
// NewEntry inserts a given Entry into a DB.
|
2023-05-22 00:50:11 +04:00
|
|
|
NewEntry(entry *Entry) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// EditEntry modifies the fields of an existing entry.
|
2023-05-22 00:50:11 +04:00
|
|
|
EditEntry(entry *Entry) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// DeleteEntry removes an Entry with a given ID.
|
2023-05-22 00:50:11 +04:00
|
|
|
DeleteEntry(entryID int64) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// NewReply inserts a given Reply into a DB.
|
2023-05-22 00:50:11 +04:00
|
|
|
NewReply(reply *Reply) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// EditReply modifies a message field of a given Reply.
|
2023-05-22 00:50:11 +04:00
|
|
|
EditReply(reply *Reply) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// DeleteReply removes a Reply with a given ID of an Entry.
|
2023-05-22 00:50:11 +04:00
|
|
|
DeleteReply(entryID int64) error
|
2023-05-22 02:35:07 +04:00
|
|
|
// Close closes a DB.
|
2023-05-22 00:50:11 +04:00
|
|
|
Close() error
|
|
|
|
}
|