-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_room.go
111 lines (96 loc) · 2.37 KB
/
model_room.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
package main
import (
"strings"
"time"
)
type RoomFilter struct {
IDs []int64 `json:"ids"`
Names []string `json:"names"`
IsUsable *bool `json:"is_usable"`
}
func (f *RoomFilter) Validate() error {
for i, v := range f.Names {
name := strings.Trim(v, " ")
if name == "" {
return NewErr(ErrInput, nil, "name is required")
}
f.Names[i] = name
}
return nil
}
type RoomInput struct {
Name string `json:"name,omitempty"`
}
func (i *RoomInput) Validate() error {
i.Name = strings.Trim(i.Name, " ")
if i.Name == "" {
return NewErr(ErrInput, nil, "name is required")
}
return nil
}
type Room struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
// relation
Capacity int64 `json:"capacity"`
}
func NewRoom(input RoomInput) (*Room, error) {
err := input.Validate()
if err != nil {
return nil, err
}
room := Room{
Name: input.Name,
}
return &room, nil
}
type SeatInput struct {
RoomID int64 `json:"room_id,omitempty"`
Name string `json:"name,omitempty"`
AdditionalPrice int `json:"additional_price,omitempty"`
}
func (i *SeatInput) Validate() error {
i.Name = strings.Trim(i.Name, " ")
if i.Name == "" {
return NewErr(ErrInput, nil, "name is required")
}
if i.RoomID <= 0 {
return NewErr(ErrInput, nil, "room id is invalid")
}
if i.AdditionalPrice < 0 {
return NewErr(ErrInput, nil, "additional price minimum is 0")
}
return nil
}
type SeatFilter struct {
IDs []int64 `json:"ids,omitempty"`
RoomIDs []int64 `json:"room_ids,omitempty"`
Names []string `json:"names,omitempty"`
}
type Seat struct {
ID int64 `json:"id"`
RoomID int64 `json:"room_id"`
Name string `json:"name"`
AdditionalPrice int `json:"additional_price"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// relation
IsAvailable bool `json:"is_available,omitempty"`
}
func NewSeat(input SeatInput) (*Seat, error) {
err := input.Validate()
if err != nil {
return nil, err
}
now := time.Now()
seat := Seat{
RoomID: input.RoomID,
Name: strings.Trim(input.Name, " "),
AdditionalPrice: input.AdditionalPrice,
CreatedAt: now,
UpdatedAt: now,
}
return &seat, nil
}