feat: rework entities design

This commit is contained in:
Muyao CHEN 2024-10-16 09:49:07 +02:00
parent dfc2d1b2eb
commit 0e05924585
6 changed files with 93 additions and 23 deletions

View File

@ -24,16 +24,39 @@ package model
import "time" import "time"
type Event struct { type EventCreateDTO struct {
Name string `json:"name" binding:"requiered"`
Description string `json:"description"`
OwnerID int `json:"owner_id" binding:"requiered,number"`
}
type EventPO struct {
ID int ID int
Name string Name string
Description string Description string
Users []*User TotalAmount int
Expenses []*Expense DefaultCurrency string
TotalAmount Money OwnerID int
DefaultCurrency Currency
CreatedBy User CreatedAt time.Time
UpdatedAt time.Time
}
type Event struct {
ID int
Name string
Description string
// lazy get using participation join
Users []*User
// lazy get
Expenses []*Expense
TotalAmount Money
DefaultCurrency Currency
Owner User
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time

View File

@ -24,20 +24,47 @@ package model
import "time" import "time"
type ExpenseDetail struct { type ExpenseDTO struct {
Amount Money `json:"money" binding:"required,number"`
PayerIDs []int `json:"payer_ids" binding:"required"`
RecipientIDs []int `json:"recipient_ids" binding:"required"`
EventID int `json:"event_id" binding:"required"`
Detail ExpenseDetail `json:"detail"`
}
type ExpensePO struct {
ID int
Amount int
Currency string
EventID int
// ExpenseDetail
Name string Name string
Place string Place string
CreatedAt time.Time
UpdatedAt time.Time
}
type ExpenseDetail struct {
Name string `json:"name"`
Place string `json:"place"`
} }
type Expense struct { type Expense struct {
ID int ID int
Amount Money Amount Money
Currency Currency
PayerIDs []int // Lazy aggregate using Transaction join
PayerIDs []int
// Lazy aggregate using Transaction join
RecipientIDs []int RecipientIDs []int
EventID int
Detail ExpenseDetail EventID int
Detail ExpenseDetail
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time

View File

@ -36,8 +36,8 @@ const (
) )
type Money struct { type Money struct {
ammount int Amount int `json:"amount" binding:"required,number"`
currency Currency Currency Currency `json:"currency" binding:"required"`
} }
func MakeMoney(amount int, currency Currency) Money { func MakeMoney(amount int, currency Currency) Money {
@ -46,10 +46,10 @@ func MakeMoney(amount int, currency Currency) Money {
func Add(cur Currency, money ...Money) Money { func Add(cur Currency, money ...Money) Money {
var sum Money var sum Money
sum.currency = cur sum.Currency = cur
for _, m := range money { for _, m := range money {
sum.ammount += m.ammount sum.Amount += m.Amount
} }
return sum return sum
@ -58,9 +58,9 @@ func Add(cur Currency, money ...Money) Money {
func Diff(cur Currency, money1 Money, money2 Money) Money { func Diff(cur Currency, money1 Money, money2 Money) Money {
var diff Money var diff Money
diff.currency = cur diff.Currency = cur
diff.ammount = money1.ammount - money2.ammount diff.Amount = money1.Amount - money2.Amount
return diff return diff
} }

View File

@ -24,6 +24,8 @@ package model
import "time" import "time"
type ParticipationPO Participation
// Participation is the association between Users and Events // Participation is the association between Users and Events
type Participation struct { type Participation struct {
ID int ID int

View File

@ -24,14 +24,17 @@ package model
import "time" import "time"
type TransactionPO Transaction
// Transaction is the association between Expenses and Users // Transaction is the association between Expenses and Users
type Transaction struct { type Transaction struct {
ID int ID int
ExpenseID Expense ExpenseID Expense
PayerID int UserID int
Amount Money Amount int
Currency Currency Currency string
IsIncome bool
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time

View File

@ -36,8 +36,7 @@ type UserExistDTO struct {
Password string `json:"password" binding:"required"` Password string `json:"password" binding:"required"`
} }
// User model type UserPO struct {
type User struct {
ID int ID int
Email string Email string
@ -48,3 +47,19 @@ type User struct {
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
// User model
type User struct {
ID int
Email string
FirstName string
LastName string
Password string
// Lazy aggregate with the Participation join
EventIDs []int
CreatedAt time.Time
UpdatedAt time.Time
}