-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_movie.go
155 lines (135 loc) · 3.83 KB
/
model_movie.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"net/url"
"strings"
"time"
)
type MovieInput struct {
Title string `json:"title,omitempty"`
ReleaseDate time.Time `json:"release_date,omitempty" example:"2006-01-02T15:04:05+08:00"`
Director string `json:"director,omitempty"`
Duration int64 `json:"duration,omitempty"`
PosterURL string `json:"poster_url,omitempty"`
Description string `json:"description,omitempty"`
GenreIDs []int64 `json:"genre_ids,omitempty"`
}
func (i *MovieInput) Validate() error {
i.Title = strings.Trim(i.Title, " ")
i.Director = strings.Trim(i.Director, " ")
i.PosterURL = strings.Trim(i.PosterURL, " ")
i.Description = strings.Trim(i.Description, " ")
if i.Title == "" {
return NewErr(ErrInput, nil, "title is required")
}
if i.ReleaseDate.IsZero() {
return NewErr(ErrInput, nil, "release date is required")
}
if i.Director == "" {
return NewErr(ErrInput, nil, "director is required")
}
if i.Duration <= 0 {
return NewErr(ErrInput, nil, "duration (minutes) is required")
}
if i.Description == "" {
return NewErr(ErrInput, nil, "description is required")
}
if i.PosterURL == "" {
return NewErr(ErrInput, nil, "poster url is required")
}
_, err := url.ParseRequestURI(i.PosterURL)
if err != nil {
return NewErr(ErrInput, err, "poster url is not a valid url")
}
if len(i.GenreIDs) == 0 {
return NewErr(ErrInput, nil, "genre ids is required")
}
return nil
}
type MovieFilter struct {
IDs []int64 `json:"ids"`
Search string `json:"search"`
GenreIDs []int64 `json:"genre_ids"`
ShowtimeAfter time.Time `json:"showtime_after" example:"2006-01-02T15:04:05+08:00"`
}
func (f *MovieFilter) Validate() error {
minSearch := 3
if len(f.Search) > 0 && len(f.Search) < minSearch {
return NewErr(ErrInput, nil, "minimum search character is %d", minSearch)
}
return nil
}
func NewMovie(input MovieInput) (*Movie, error) {
err := input.Validate()
if err != nil {
return nil, err
}
now := time.Now()
movie := Movie{
Title: input.Title,
ReleaseDate: input.ReleaseDate,
Director: input.Director,
Duration: input.Duration,
PosterURL: input.PosterURL,
Description: input.Description,
GenreIDs: input.GenreIDs,
CreatedAt: now,
UpdatedAt: now,
}
return &movie, nil
}
type Movie struct {
ID int64 `json:"id"`
Title string `json:"title"`
ReleaseDate time.Time `json:"release_date"`
Director string `json:"director"`
Duration int64 `json:"duration"`
PosterURL string `json:"poster_url"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// relation
GenreIDs []int64 `json:"genre_ids"`
Genres []string `json:"genres"`
}
func (m *Movie) ValidateDuration(startAt, endAt time.Time) error {
duration := endAt.Sub(startAt).Minutes()
if duration < float64(m.Duration) {
return NewErr(ErrInput, nil, "showtime duration less than movie duration, minimum %d minutes", m.Duration)
}
return nil
}
func (m *Movie) GetDuration() time.Duration {
return time.Duration(m.Duration * int64(time.Minute))
}
type GenreFilter struct {
IDs []int64 `json:"ids"`
Names []string `json:"names"`
}
func (i *GenreFilter) Validate() error {
return nil
}
type GenreInput struct {
Name string `json:"name"`
}
func (i *GenreInput) Validate() error {
i.Name = strings.Trim(i.Name, " ")
if i.Name == "" {
return NewErr(ErrInput, nil, "name is required")
}
i.Name = strings.ToLower(i.Name)
return nil
}
func NewGenre(input GenreInput) (*Genre, error) {
err := input.Validate()
if err != nil {
return nil, err
}
genre := Genre{Name: input.Name}
return &genre, nil
}
type Genre struct {
ID int64 `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}