all repos

onasty @ 4073d2b

a one-time notes service

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package models

import (
	"errors"
	"time"

	"github.com/gofrs/uuid/v5"
)

var (
	ErrNoteContentIsEmpty     = errors.New("note: content is empty")
	ErrNoteSlugIsAlreadyInUse = errors.New("note: slug is already in use")
	ErrNoteExpired            = errors.New("note: expired")
	ErrNoteNotFound           = errors.New("note: not found")
)

type Note struct {
	ID                   uuid.UUID
	Content              string
	Slug                 string
	Password             string
	BurnBeforeExpiration bool
	CreatedAt            time.Time
	ExpiresAt            time.Time
}

func (n Note) Validate() error {
	if n.Content == "" {
		return ErrNoteContentIsEmpty
	}

	if n.IsExpired() {
		return ErrNoteExpired
	}

	return nil
}

func (n Note) IsExpired() bool {
	return !n.ExpiresAt.IsZero() &&
		n.ExpiresAt.Before(time.Now())
}

func (n Note) ShouldBeBurnt() bool {
	return !n.ExpiresAt.IsZero() &&
		n.BurnBeforeExpiration
}