-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
55 lines (49 loc) · 1.89 KB
/
models.go
File metadata and controls
55 lines (49 loc) · 1.89 KB
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
package main
import (
"time"
"gorm.io/gorm"
)
// User represents a user in the system.
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Name string `json:"name"`
Email string `gorm:"uniqueIndex" json:"email"`
PasswordHash string `json:"-"`
IsAdmin bool `json:"is_admin"`
Orders []Order `json:"-"`
}
// Product represents a product sold in the store.
type Product struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Name string `json:"name"`
Description string `json:"description"`
PriceCents int64 `json:"price_cents"` // price in cents to avoid float issues
Stock int `json:"stock"`
}
// Order represents a user's order.
type Order struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
UserID uint `json:"user_id"`
User User `json:"-"`
TotalCents int64 `json:"total_cents"`
Items []OrderItem `json:"items" gorm:"constraint:OnDelete:CASCADE;"`
Status string `json:"status"` // e.g., pending, paid, shipped
}
// OrderItem represents an item within an order.
type OrderItem struct {
ID uint `gorm:"primaryKey" json:"id"`
OrderID uint `json:"order_id"`
ProductID uint `json:"product_id"`
Product Product
Quantity int `json:"quantity"`
PriceCents int64 `json:"price_cents"` // snapshot of product price at order time
}