-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
64 lines (52 loc) · 1.13 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
60
61
62
63
64
package files
import (
"errors"
"time"
"gopkg.in/guregu/null.v4"
)
var (
ErrOwnerRequired = errors.New("owner is required")
ErrNameRequired = errors.New("name is required")
ErrTypeRequired = errors.New("type is required")
ErrPathRequired = errors.New("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
}