1
0

Added comments.

This commit is contained in:
Alexander Andreev 2023-05-22 02:35:07 +04:00
parent 5d56b56882
commit 141e1215a3
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -5,14 +5,27 @@ import (
"time" "time"
) )
const DateFormat = "2006-01-02 15:04:05" const (
// DateFormat is a format of the date and time used in a guestbook.
DateFormat = "2006-01-02 15:04:05"
// Reply holds a reply for a guestbook's entry.
type Reply struct { type Reply struct {
// 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:"-"` ID int64 `json:"-"`
// Created holds a date and time when a reply was created.
Created time.Time `json:"created,omitempty"` Created time.Time `json:"created,omitempty"`
// Message holds a reply's message.
Message string `json:"message"` Message string `json:"message"`
} }
// NewReply creates a new reply and, before that, verifies that a passed message
// is not empty.
func NewReply(entryID int64, message string) (*Reply, error) { func NewReply(entryID int64, message string) (*Reply, error) {
if message == "" { if message == "" {
return nil, errors.New("empty message field") return nil, errors.New("empty message field")
@ -24,16 +37,32 @@ func NewReply(entryID int64, message string) (*Reply, error) {
Message: message}, nil Message: message}, nil
} }
// Entry holds a guestbook entry.
type Entry struct { type Entry struct {
// ID of an entry.
ID int64 `json:"entry_id"` ID int64 `json:"entry_id"`
// Created holds the date and time when an entry was created.
Created time.Time `json:"created"` Created time.Time `json:"created"`
// Name holds a nick-/name of a guest who posted an entry.
Name string `json:"name"` Name string `json:"name"`
// Website of a guest. Can be empty.
Website string `json:"website,omitempty"` Website string `json:"website,omitempty"`
// HideWebsite tells wether to hide or show guest's website.
HideWebsite bool `json:"hide_website,omitempty"` HideWebsite bool `json:"hide_website,omitempty"`
// Message that a guest wrote.
Message string `json:"message"` Message string `json:"message"`
// Reply holds a reply for this entry.
Reply *Reply `json:"reply,omitempty"` Reply *Reply `json:"reply,omitempty"`
} }
// 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.
func NewEntry(name, message, website string, hideWebsite bool) (*Entry, error) { func NewEntry(name, message, website string, hideWebsite bool) (*Entry, error) {
if name == "" || message == "" { if name == "" || message == "" {
return nil, errors.New("name and message field are required") return nil, errors.New("name and message field are required")
@ -47,14 +76,24 @@ func NewEntry(name, message, website string, hideWebsite bool) (*Entry, error) {
Message: message}, nil Message: message}, nil
} }
// Guestbook is an interface that should be implemented by a DB.
type Guestbook interface { type Guestbook interface {
// Entries returns a slice of guestbook entries.
Entries(page, pageSize int64) ([]*Entry, error) Entries(page, pageSize int64) ([]*Entry, error)
// Count returns how much entries are in a DB.
Count() (int64, error) Count() (int64, error)
// NewEntry inserts a given Entry into a DB.
NewEntry(entry *Entry) error NewEntry(entry *Entry) error
// EditEntry modifies the fields of an existing entry.
EditEntry(entry *Entry) error EditEntry(entry *Entry) error
// DeleteEntry removes an Entry with a given ID.
DeleteEntry(entryID int64) error DeleteEntry(entryID int64) error
// NewReply inserts a given Reply into a DB.
NewReply(reply *Reply) error NewReply(reply *Reply) error
// EditReply modifies a message field of a given Reply.
EditReply(reply *Reply) error EditReply(reply *Reply) error
// DeleteReply removes a Reply with a given ID of an Entry.
DeleteReply(entryID int64) error DeleteReply(entryID int64) error
// Close closes a DB.
Close() error Close() error
} }