/
example.go
88 lines (78 loc) · 2.24 KB
/
example.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
/*
import (
"github.com/gin-gonic/gin"
)
type ExampleManufacturerRequests struct {
}
func (m ExampleManufacturerRequests) RegisterItem(ctx *gin.Context) error {
return nil
}
func (m ExampleManufacturerRequests) UpdateItem(ctx *gin.Context) error {
return nil
}
func (m ExampleManufacturerRequests) DeleteItem(ctx *gin.Context) error {
return nil
}
*/
package manufacturer
import (
"encoding/json"
"log"
"github.com/charisworks/charisworks-backend/internal/items"
"github.com/charisworks/charisworks-backend/internal/utils"
"gorm.io/gorm"
)
type TestRepository struct {
DB *gorm.DB
}
func (r TestRepository) Register(itemId string, i RegisterPayload, userId string) error {
json, err := json.Marshal(i.Details.Tags)
if err != nil {
return &utils.InternalError{Message: utils.InternalErrorInvalidPayload}
}
item := utils.Item{
Id: itemId,
ManufacturerUserId: userId,
Name: i.Name,
Price: i.Price,
Status: string(items.Ready),
Stock: i.Details.Stock,
Size: i.Details.Size,
Description: i.Details.Description,
Tags: string(json),
}
if err := r.DB.Create(&item).Error; err != nil {
log.Print("DB error: ", err)
if err.Error() == "record not found" {
err = &utils.InternalError{Message: utils.InternalErrorNotFound}
} else {
err = &utils.InternalError{Message: utils.InternalErrorDB}
}
return err
}
return nil
}
func (r TestRepository) Update(i map[string]interface{}, itemId string) error {
if err := r.DB.Table("items").Where("id = ?", itemId).Updates(i).Error; err != nil {
log.Print("DB error: ", err)
if err.Error() == "record not found" {
err = &utils.InternalError{Message: utils.InternalErrorNotFound}
} else {
err = &utils.InternalError{Message: utils.InternalErrorDB}
}
return err
}
return nil
}
func (r TestRepository) Delete(itemId string) error {
if err := r.DB.Table("items").Where("id = ?", itemId).Delete(&utils.Item{}).Error; err != nil {
log.Print("DB error: ", err)
if err.Error() == "record not found" {
err = &utils.InternalError{Message: utils.InternalErrorNotFound}
} else {
err = &utils.InternalError{Message: utils.InternalErrorDB}
}
return err
}
return nil
}