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"
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
Name string
Description string
Users []*User
Expenses []*Expense
TotalAmount Money
DefaultCurrency Currency
CreatedBy User
TotalAmount int
DefaultCurrency string
OwnerID int
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
UpdatedAt time.Time

View File

@ -24,20 +24,47 @@ package model
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
Place string
CreatedAt time.Time
UpdatedAt time.Time
}
type ExpenseDetail struct {
Name string `json:"name"`
Place string `json:"place"`
}
type Expense struct {
ID int
Amount Money
Currency Currency
PayerIDs []int
Amount Money
// Lazy aggregate using Transaction join
PayerIDs []int
// Lazy aggregate using Transaction join
RecipientIDs []int
EventID int
Detail ExpenseDetail
EventID int
Detail ExpenseDetail
CreatedAt time.Time
UpdatedAt time.Time

View File

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

View File

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

View File

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

View File

@ -36,8 +36,7 @@ type UserExistDTO struct {
Password string `json:"password" binding:"required"`
}
// User model
type User struct {
type UserPO struct {
ID int
Email string
@ -48,3 +47,19 @@ type User struct {
CreatedAt 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
}