Compare commits
No commits in common. "29633e0e9559ca2afe090244fe7462ccc5ac65af" and "3b18a15494f2f0c669ee4704a29573a474921133" have entirely different histories.
29633e0e95
...
3b18a15494
@ -50,8 +50,8 @@ func NewUserRepository(db *sql.DB) repo.UserRepository {
|
|||||||
func (ur *userRepository) Create(
|
func (ur *userRepository) Create(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
transaction interface{},
|
transaction interface{},
|
||||||
u *model.UserPO,
|
u *model.User,
|
||||||
) (*model.UserPO, error) {
|
) (*model.User, error) {
|
||||||
timeoutCtx, cancel := context.WithTimeout(ctx, insertTimeout)
|
timeoutCtx, cancel := context.WithTimeout(ctx, insertTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ func (ur *userRepository) Create(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.UserPO{
|
return &model.User{
|
||||||
ID: int(userDB.ID),
|
ID: int(userDB.ID),
|
||||||
Email: userDB.Email,
|
Email: userDB.Email,
|
||||||
FirstName: userDB.FirstName,
|
FirstName: userDB.FirstName,
|
||||||
@ -88,7 +88,7 @@ func (ur *userRepository) Create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByEmail if not found, return nil for user but not error.
|
// GetByEmail if not found, return nil for user but not error.
|
||||||
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.UserPO, error) {
|
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.User, error) {
|
||||||
queries := sqlc.New(ur.db)
|
queries := sqlc.New(ur.db)
|
||||||
userDB, err := queries.GetUserByEmail(ctx, email)
|
userDB, err := queries.GetUserByEmail(ctx, email)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
@ -98,7 +98,7 @@ func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.UserPO{
|
return &model.User{
|
||||||
ID: int(userDB.ID),
|
ID: int(userDB.ID),
|
||||||
Email: userDB.Email,
|
Email: userDB.Email,
|
||||||
FirstName: userDB.FirstName,
|
FirstName: userDB.FirstName,
|
||||||
|
@ -24,40 +24,15 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type EventCreateDTO struct {
|
type Event struct {
|
||||||
Name string `json:"name" binding:"requiered"`
|
ID int
|
||||||
Description string `json:"description"`
|
|
||||||
OwnerID int `json:"owner_id" binding:"requiered,number"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventPO struct {
|
|
||||||
ID int
|
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description string
|
||||||
TotalAmount int
|
Users []*User
|
||||||
DefaultCurrency string
|
Expenses []*Expense
|
||||||
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
|
TotalAmount Money
|
||||||
DefaultCurrency Currency
|
DefaultCurrency Currency
|
||||||
Owner User
|
CreatedBy User
|
||||||
|
CreatedAt time.Time
|
||||||
CreatedAt time.Time
|
UpdatedAt time.Time
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
}
|
||||||
|
@ -24,48 +24,19 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type ExpenseDTO struct {
|
type ExpenseDetail 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
|
||||||
EventID int
|
Detail ExpenseDetail
|
||||||
Detail ExpenseDetail
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
}
|
||||||
|
@ -36,8 +36,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Money struct {
|
type Money struct {
|
||||||
Amount int `json:"amount" binding:"required,number"`
|
ammount int
|
||||||
Currency Currency `json:"currency" binding:"required"`
|
currency Currency
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Amount += m.Amount
|
sum.ammount += m.ammount
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Amount = money1.Amount - money2.Amount
|
diff.ammount = money1.ammount - money2.ammount
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
}
|
}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
|
||||||
// copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type ParticipationPO Participation
|
|
||||||
|
|
||||||
// Participation is the association between Users and Events
|
|
||||||
type Participation struct {
|
|
||||||
ID int
|
|
||||||
|
|
||||||
UserID int
|
|
||||||
EventID int
|
|
||||||
InvitedByUserID int
|
|
||||||
|
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in all
|
|
||||||
// copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type TransactionPO Transaction
|
|
||||||
|
|
||||||
// Transaction is the association between Expenses and Users
|
|
||||||
type Transaction struct {
|
|
||||||
ID int
|
|
||||||
|
|
||||||
ExpenseID Expense
|
|
||||||
UserID int
|
|
||||||
Amount int
|
|
||||||
Currency string
|
|
||||||
IsIncome bool
|
|
||||||
|
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
|
@ -36,30 +36,13 @@ type UserExistDTO struct {
|
|||||||
Password string `json:"password" binding:"required"`
|
Password string `json:"password" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserPO struct {
|
|
||||||
ID int
|
|
||||||
|
|
||||||
Email string
|
|
||||||
FirstName string
|
|
||||||
LastName string
|
|
||||||
Password string
|
|
||||||
|
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
// User model
|
// User model
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int
|
ID int
|
||||||
|
|
||||||
Email string
|
Email string
|
||||||
FirstName string
|
FirstName string
|
||||||
LastName string
|
LastName string
|
||||||
Password string
|
Password string
|
||||||
|
|
||||||
// Lazy aggregate with the Participation join
|
|
||||||
EventIDs []int
|
|
||||||
|
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UserRepository interface {
|
type UserRepository interface {
|
||||||
Create(ctx context.Context, transaction interface{}, u *model.UserPO) (*model.UserPO, error)
|
Create(ctx context.Context, transaction interface{}, u *model.User) (*model.User, error)
|
||||||
GetByEmail(ctx context.Context, email string) (*model.UserPO, error)
|
GetByEmail(ctx context.Context, email string) (*model.User, error)
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,8 @@ type TestUserRepository struct{}
|
|||||||
func (tur *TestUserRepository) Create(
|
func (tur *TestUserRepository) Create(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
transaction interface{},
|
transaction interface{},
|
||||||
u *model.UserPO,
|
u *model.User,
|
||||||
) (*model.UserPO, error) {
|
) (*model.User, error) {
|
||||||
user := *u
|
user := *u
|
||||||
|
|
||||||
user.ID = 123
|
user.ID = 123
|
||||||
@ -50,11 +50,11 @@ func (tur *TestUserRepository) Create(
|
|||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ur *TestUserRepository) GetByEmail(ctx context.Context, email string) (*model.UserPO, error) {
|
func (ur *TestUserRepository) GetByEmail(ctx context.Context, email string) (*model.User, error) {
|
||||||
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte("strongHashed"), 12)
|
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte("strongHashed"), 12)
|
||||||
switch email {
|
switch email {
|
||||||
case "a@b.c":
|
case "a@b.c":
|
||||||
return &model.UserPO{
|
return &model.User{
|
||||||
ID: 123,
|
ID: 123,
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
Password: string(hashedPwd),
|
Password: string(hashedPwd),
|
||||||
|
@ -82,7 +82,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
|
|||||||
data, err := uuc.dbRepo.Transaction(
|
data, err := uuc.dbRepo.Transaction(
|
||||||
ctx,
|
ctx,
|
||||||
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
||||||
created, err := uuc.userRepo.Create(txCtx, tx, &model.UserPO{
|
created, err := uuc.userRepo.Create(txCtx, tx, &model.User{
|
||||||
Email: u.Email,
|
Email: u.Email,
|
||||||
Password: u.Password,
|
Password: u.Password,
|
||||||
FirstName: u.FirstName,
|
FirstName: u.FirstName,
|
||||||
@ -113,20 +113,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userPO := data.(*model.UserPO)
|
user := data.(*model.User)
|
||||||
|
|
||||||
user := &model.User{
|
|
||||||
ID: userPO.ID,
|
|
||||||
Email: userPO.Email,
|
|
||||||
Password: userPO.Password,
|
|
||||||
FirstName: userPO.FirstName,
|
|
||||||
LastName: userPO.LastName,
|
|
||||||
|
|
||||||
EventIDs: []int{}, // Unfilled
|
|
||||||
|
|
||||||
CreatedAt: userPO.CreatedAt,
|
|
||||||
UpdatedAt: userPO.UpdatedAt,
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user