46 lines
810 B
Go
46 lines
810 B
Go
package messages
|
|
|
|
import "errors"
|
|
|
|
type Message struct {
|
|
MessageId uint64 `json:"message_id"`
|
|
Author_id uint64 `json:"author_id"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
var messages []Message
|
|
|
|
func initMessages() {
|
|
messages = make([]Message, 0)
|
|
}
|
|
|
|
func insertMessage(msg Message) error {
|
|
if msg.Author_id == 0 {
|
|
return errors.New("An author_id must be specified")
|
|
}
|
|
|
|
if msg.Text == "" {
|
|
return errors.New("The field text must not be empty")
|
|
}
|
|
|
|
messages = append(messages, Message{
|
|
MessageId: uint64(len(messages)),
|
|
Text: msg.Text,
|
|
Author_id: msg.Author_id,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func getAllMessages() []Message {
|
|
return messages
|
|
}
|
|
|
|
func getMessageById(id uint64) (Message, error) {
|
|
if int(id) >= len(messages) {
|
|
return Message{}, errors.New("Message doesn't exists")
|
|
}
|
|
|
|
return messages[id], nil
|
|
}
|