onasty/internal/models/notes.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com feat: add password support to notes (#41)..., 1 year ago
ss2316544@gmail.com feat: add password support to notes (#41)..., 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 | CreatedAt time.Time |
| 24 | ExpiresAt time.Time |
| 25 | } |
| 26 | |
| 27 | func (n Note) Validate() error { |
| 28 | if n.Content == "" { |
| 29 | return ErrNoteContentIsEmpty |
| 30 | } |
| 31 | |
| 32 | if n.IsExpired() { |
| 33 | return ErrNoteExpired |
| 34 | } |
| 35 | |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | func (n Note) IsExpired() bool { |
| 40 | return !n.ExpiresAt.IsZero() && |
| 41 | n.ExpiresAt.Before(time.Now()) |
| 42 | } |
| 43 | |
| 44 | func (n Note) ShouldBeBurnt() bool { |
| 45 | return !n.ExpiresAt.IsZero() && |
| 46 | n.BurnBeforeExpiration |
| 47 | } |