2025-02-12 19:29:00 +01:00
|
|
|
package core
|
2024-11-09 19:35:44 +01:00
|
|
|
|
2025-04-09 18:37:02 +02:00
|
|
|
import "github.com/shopspring/decimal"
|
|
|
|
|
|
2024-11-09 19:35:44 +01:00
|
|
|
type User struct {
|
2025-01-14 20:14:07 +01:00
|
|
|
Username string `gorm:"primaryKey" json:"username"`
|
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
OwnedPlans []Plan `gorm:"foreignKey:Owner;references:Username" json:"-"`
|
|
|
|
|
MemberOf []Member `json:"-"`
|
|
|
|
|
Votes []Vote `gorm:"foreignKey:UsernameID" json:"-"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Member struct {
|
2025-02-12 20:12:30 +01:00
|
|
|
ID uint `gorm:"primaryKey;autoIncrement:true" json:"-"`
|
|
|
|
|
PlanID uint `json:"-"`
|
|
|
|
|
Plan Plan `json:"-"`
|
2025-04-09 18:37:02 +02:00
|
|
|
Name string `gorm:"check:user_id IS NOT NULL OR name IS NOT NULL" json:"name,omitempty"`
|
|
|
|
|
UserID string `json:"username,omitempty"`
|
2025-02-12 20:12:30 +01:00
|
|
|
User User `gorm:"foreignKey:UserID" json:"-"`
|
2024-11-09 19:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CREATE TABLE plans(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, owner STRING, FOREIGN KEY(owner) REFERENCES users(username))
|
|
|
|
|
// CREATE TABLE plan_user_relations(username STRING, plan INTEGER, PRIMARY KEY(username, plan), FOREIGN KEY username REFERENCES user(username), FOREIGN KEY plan REFERENCES plans(id))
|
|
|
|
|
type Plan struct {
|
2025-01-14 20:14:07 +01:00
|
|
|
ID uint `gorm:"primaryKey;autoIncrement:true" json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
|
Description string `json:"description"`
|
2025-02-12 20:12:30 +01:00
|
|
|
JoinCode string `gorm:"not null" json:"join_code,omitempty"`
|
2025-01-14 20:14:07 +01:00
|
|
|
Members []Member `json:"-"`
|
|
|
|
|
Polls []Poll `gorm:"foreignKey:PlanID;references:ID" json:"-"`
|
2024-11-09 19:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 18:37:02 +02:00
|
|
|
type Expense struct {
|
|
|
|
|
ID uint `gorm:"primaryKey;autoIncrement:true" json:"id"`
|
|
|
|
|
PlanID uint `json:"-"`
|
|
|
|
|
Plan Plan `json:"-"`
|
|
|
|
|
PayerID uint `json:"-"`
|
|
|
|
|
Payer Member `gorm:"foreignKey:PayerID" json:"-"`
|
|
|
|
|
Amount decimal.Decimal `json:"amount"`
|
|
|
|
|
Debts []Debt `gorm:"foreignKey:ExpenseID" json:"debts,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Debt struct {
|
|
|
|
|
ExpenseID uint `gorm:"primaryKey" json:"-"`
|
|
|
|
|
Expense Expense `gorm:"foreignKey:ExpenseID"`
|
|
|
|
|
DebtorID uint `gorm:"primaryKey" json:"-"`
|
|
|
|
|
Debtor Member `gorm:"foreignKey:DebtorID"`
|
|
|
|
|
Amount decimal.Decimal
|
|
|
|
|
Paid decimal.Decimal
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 19:35:44 +01:00
|
|
|
// CREATE TABLE polls(id INTEGER PRIMARY KEY AUTOINCREMENT, plan INTEGER, name STRING, options JSON, FOREIGN KEY plan REFERENCES plans(id))
|
|
|
|
|
type Poll struct {
|
|
|
|
|
ID uint `gorm:"primaryKey;autoIncrement:true" json:"id"`
|
|
|
|
|
PlanID uint `json:"-"`
|
|
|
|
|
Options string `json:"options"`
|
|
|
|
|
Votes []Vote `gorm:"foreignKey:PollID;references:ID" json:"-"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CREATE TABLE votes(id INTEGER, poll INTEGER, user STRING, value JSON, FOREIGN KEY poll REFERENCES polls(id), FOREIGN KEY user REFERENCES user(username))
|
|
|
|
|
type Vote struct {
|
|
|
|
|
PollID uint `gorm:"primaryKey" json:"-"`
|
|
|
|
|
UsernameID string `gorm:"primaryKey" json:"username_id"`
|
|
|
|
|
Value string `json:"value"`
|
|
|
|
|
}
|