-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
121 lines (99 loc) · 2.67 KB
/
main.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
package main
import (
"github.com/google/uuid"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"os"
"time"
)
//
//`id` int NOT NULL AUTO_INCREMENT,
//`category_id` int DEFAULT NULL,
//`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
//`image` json DEFAULT NULL,
//`type` enum('drink','food','topping') NOT NULL DEFAULT 'drink',
//`description` text,
//`status` enum('activated','deactivated','out_of_stock') DEFAULT 'activated',
//`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
//`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
type BaseModel struct {
Id uuid.UUID `gorm:"column:id;"`
Status string `gorm:"column:status;"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}
type Product struct {
BaseModel
CategoryId int `gorm:"column:category_id"`
Name string `gorm:"column:name"`
//Image any `gorm:"column:image"`
Type string `gorm:"column:type"`
Description string `gorm:"column:description"`
}
type ProductUpdate struct {
Name *string `gorm:"column:name"`
CategoryId *int `gorm:"column:category_id"`
Status *string `gorm:"column:status;"`
Type *string `gorm:"column:type"`
Description *string `gorm:"column:description"`
}
func (Product) TableName() string { return "products" }
func main() {
dsn := os.Getenv("DB_DSN")
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalln(err)
}
now := time.Now().UTC()
newId, _ := uuid.NewV7()
newProd := Product{
BaseModel: BaseModel{
Id: newId,
Status: "activated",
CreatedAt: now,
UpdatedAt: now,
},
CategoryId: 1,
Name: "Latte",
//Image: nil,
Type: "drink",
Description: "",
}
if err := db.Table("products").Create(&newProd).Error; err != nil {
log.Println(err)
}
var oldProduct Product
if err := db.
Table(Product{}.TableName()).
//Where("id = ?", 4).
First(&oldProduct).Error; err != nil {
log.Println(err)
}
log.Println("Product:", oldProduct)
var prods []Product
if err := db.
Table(Product{}.TableName()).
Where("status not in (?)", []string{"deactivated"}).
Limit(10).
Offset(10).
Order("id desc").
Find(&prods).Error; err != nil {
log.Println(err)
}
log.Println("Products:", prods)
//oldProduct.Name = ""
//emptyStr := "Latte"
//if err := db.
// Table(Product{}.TableName()).
// Where("id = ?", 4).
// Updates(ProductUpdate{Name: &emptyStr}).Error; err != nil {
// log.Println(err)
//}
//if err := db.
// Table(Product{}.TableName()).
// Where("id = ?", 4).
// Delete(nil).Error; err != nil {
// log.Println(err)
//}
}