-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
59 lines (52 loc) · 1.15 KB
/
entity.go
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
48
49
50
51
52
53
54
55
56
57
58
59
package files
import (
"errors"
"github.com/guregu/null/v5"
"time"
)
var (
ErrOwnerRequired = errors.New("owner required")
ErrNameRequired = errors.New("folder name is required")
ErrTypeRequired = errors.New("folder type is required")
ErrPathRequired = errors.New("folder path is required")
)
func New(ownerId int64, name, fileType, path string) (*File, error) {
f := File{
OwnerId: ownerId,
Name: name,
Type: fileType,
Path: path,
ModifiedAt: time.Now(),
}
err := f.Validate()
if err != nil {
return nil, err
}
return &f, nil
}
type File struct {
ID int64 `json:"id"`
FolderId null.Int `json:"-"`
OwnerId int64 `json:"owner_id"`
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"-"`
CreatedAt time.Time `json:"created_at"`
ModifiedAt time.Time `json:"modified_at"`
Deleted bool `json:"-"`
}
func (f *File) Validate() error {
if f.OwnerId == 0 {
return ErrOwnerRequired
}
if f.Name == "" {
return ErrNameRequired
}
if f.Type == "" {
return ErrTypeRequired
}
if f.Path == "" {
return ErrPathRequired
}
return nil
}