all repos

onasty @ 5290b5fe6a6bba7e2328582aae3c4a359ec60e01

a one-time notes service

onasty/internal/models/note.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: notes manipulations for the note authors (#117)..., 1 year ago
1
package models
2
3
import (
4
	"errors"
5
	"time"
6
7
	"github.com/gofrs/uuid/v5"
8
)
9
10
var (
11
	ErrNoteContentIsEmpty     = errors.New("note: content is empty")
12
	ErrNoteSlugIsAlreadyInUse = errors.New("note: slug is already in use")
13
	ErrNoteExpired            = errors.New("note: expired")
14
	ErrNoteNotFound           = errors.New("note: not found")
15
)
16
17
type Note struct {
18
	ID                   uuid.UUID
19
	Content              string
20
	Slug                 string
21
	Password             string
22
	BurnBeforeExpiration bool
23
	ReadAt               time.Time
24
	CreatedAt            time.Time
25
	ExpiresAt            time.Time
26
}
27
28
func (n Note) Validate() error {
29
	if n.Content == "" {
30
		return ErrNoteContentIsEmpty
31
	}
32
33
	if n.IsExpired() {
34
		return ErrNoteExpired
35
	}
36
37
	return nil
38
}
39
40
func (n Note) IsExpired() bool {
41
	return !n.ExpiresAt.IsZero() &&
42
		n.ExpiresAt.Before(time.Now())
43
}
44
45
func (n Note) ShouldBeBurnt() bool {
46
	return !n.ExpiresAt.IsZero() &&
47
		n.BurnBeforeExpiration
48
}
49
50
func (n Note) IsRead() bool {
51
	return !n.ReadAt.IsZero()
52
}