-
Notifications
You must be signed in to change notification settings - Fork 0
/
apartments.go
135 lines (102 loc) · 2.62 KB
/
apartments.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
package rentals
import (
"errors"
"time"
)
type Apartment struct {
// Primary key
ID uid `gorm:"primary_key" json:"id"`
// Date added
DateAdded time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"dateAdded"`
// Name of this property
Name string `json:"name"`
// Description
Desc string `json:"description"`
// Realtor associated with this apartment
Realtor User `json:"-" gorm:"foreignkey:RealtorId"`
RealtorId uint `json:"realtorId"`
// Floor size area
// See:
// https://stackoverflow.com/questions/445191/should-we-put-units-of-measurements-in-attribute-names
FloorAreaMeters float32 `json:"floorAreaMeters"`
// Monthly rent
PricePerMonthUsd float32 `json:"pricePerMonthUSD"`
// Number of rooms
RoomCount int `json:"roomCount"`
// Geolocation
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
// Availability of the apartment
Available bool `json:"available"`
}
func (uid) UnmarshalJSON([]byte) error {
return nil
}
// Validates data for a new apartment.
func (s *Apartment) Validate() error {
allErrors := ""
if s.Name == "" {
allErrors += "Name can't be empty\n"
}
if s.FloorAreaMeters <= 0 {
allErrors += "Floor Area must be greater than 0\n"
}
if s.PricePerMonthUsd <= 0 {
allErrors += "Price per month must be greater than 0\n"
}
if s.RoomCount <= 0 {
allErrors += "Room count must be greater than 0\n"
}
if s.Latitude < -90 || s.Latitude > 90 {
allErrors += "Latitude must be in the range [-90.0, 90.0]\n"
}
if s.Longitude < -180 || s.Longitude > 180 {
allErrors += "Longitude must be in the range [-180.0, 180.0]\n"
}
if allErrors == "" {
return nil
}
return errors.New("\n" + allErrors)
}
type ApartmentService interface {
Create(ApartmentCreateInput) (*ApartmentCreateOutput, error)
Read(ApartmentReadInput) (*ApartmentReadOutput, error)
Find(ApartmentFindInput) (*ApartmentFindOutput, error)
Update(ApartmentUpdateInput) (*ApartmentUpdateOutput, error)
Delete(ApartmentDeleteInput) (*ApartmentDeleteOutput, error)
}
type ApartmentCreateInput struct {
Apartment
}
type ApartmentCreateOutput struct {
Apartment
}
type ApartmentReadInput struct {
// ID to lookup the apartment
Id string
}
type ApartmentReadOutput struct {
Apartment
}
type ApartmentFindInput struct {
Query string
}
type ApartmentFindOutput struct {
Apartments []Apartment
}
func (o *ApartmentFindOutput) Public() interface{} {
return o.Apartments
}
type ApartmentUpdateInput struct {
Id string
Data map[string]interface{}
}
type ApartmentUpdateOutput struct {
Apartment
}
type ApartmentDeleteInput struct {
Id string
}
type ApartmentDeleteOutput struct {
Message string
}