-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.go
65 lines (53 loc) · 1.45 KB
/
addresses.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
package models
import (
u "../utils"
"github.com/jinzhu/gorm"
)
type Address struct {
gorm.Model
Number string `json:"number"`
FirstLine string `json:"first_line"`
Town string `json:"town"`
PostalCode string `json:"postal_code"`
Province string `json:"province"`
Country string `json:"country"`
UserId uint `json:"user_id"` //The user that this contact belongs to
}
func (address *Address) ValidateAddress() (map[string] interface{}, bool) {
if address.PostalCode == "" {
return u.Message(false, "Postal code should be on the payload"), false
}
if address.Country == "" {
return u.Message(false, "Country should be on the payload"), false
}
if address.UserId <= 0 {
return u.Message(false, "User is not recognized"), false
}
//All the required parameters are present
return u.Message(true, "success"), true
}
func (address *Address) CreateAddress()(map[string]interface{}){
if resp, ok := address.ValidateAddress(); !ok {
return resp
}
GetDB().Create(address)
resp := u.Message(true, "success")
resp["address"] = address
return resp
}
func GetAddressByID(id uint)(*Address){
address := &Address{}
err := GetDB().Table("addresses").Where("id = ?", id).First(address).Error
if err != nil {
return nil
}
return address
}
func GetAddressByUserID(user uint)([]*Address){
address := make([]*Address, 0)
err := GetDB().Table("addresses").Where("user_id = ?", user).First(address).Error
if err != nil {
return nil
}
return address
}