/
customer.go
44 lines (34 loc) · 948 Bytes
/
customer.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
package models
import (
"gorm.io/gorm"
"time"
)
type Customer struct {
Model
FirstName string `json:"firstName" validate:"required" gorm:"not null"`
LastName string `json:"lastName" validate:"required" gorm:"not null"`
Title string `json:"title" validate:"required" gorm:"not null"`
CreatedBy string `json:"createdBy"`
ModifiedBy string `json:"modifiedBy"`
}
func GetCustomerById(id int) (*Customer, error) {
var customer Customer
err := db.Where("id = ?", id).First(&customer).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return &customer, nil
}
func AddNewCustomer(customer Customer) (*Customer, error) {
customer.CreatedOn = time.Now()
customer.ModifiedOn = time.Now()
db.Save(&customer)
//if err := db.Create(&customer).Error; err != nil {
// return err, GetCustomerById()
//}
res, err := GetCustomerById(customer.ID)
if err != nil {
return nil, err
}
return res, nil
}