Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
walnuts1018 committed Nov 1, 2023
1 parent a8b5bc5 commit 3129575
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions back/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package usecase
import (
"encoding/base64"
"fmt"
"math/rand"
"time"

"github.com/walnuts1018/openchokin/back/domain"
Expand All @@ -18,8 +19,7 @@ func NewUsecase(db domain.DB) *Usecase {
}
}

func (u Usecase) NewUser(userName, email string) (domain.User, error) {
userid := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v%v", userName, email)))
func (u Usecase) NewUser(userid string) (domain.User, error) {
user := domain.User{
ID: userid,
}
Expand All @@ -34,19 +34,33 @@ func (u Usecase) GetUser(userID string) (domain.User, error) {
return u.db.GetUser(userID)
}

func (u Usecase) NewMoneyPool(moneyPool domain.MoneyPool, user domain.User) error {
func (u Usecase) NewMoneyPool(moneyPoolName, moneyPoolColor, userID string, isWorldPublic bool) (domain.MoneyPool, error) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
moneyPoolID := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%d%d%d", moneyPoolName, userID, r.Int63())))
moneyPool := domain.MoneyPool{
ID: moneyPoolID,
Name: moneyPoolName,
Color: moneyPoolColor,
IsWorldPublic: isWorldPublic,
ShareUserIDs: []string{userID},
}

err := u.db.NewMoneyPool(moneyPool)
if err != nil {
return fmt.Errorf("failed to create money pool: %w", err)
return domain.MoneyPool{}, fmt.Errorf("failed to create money pool: %w", err)
}

user, err := u.db.GetUser(userID)
if err != nil {
return domain.MoneyPool{}, fmt.Errorf("failed to get user: %w", err)
}

user.MoneyPoolIDs = append(user.MoneyPoolIDs, moneyPool.ID)
err = u.db.UpdateUser(user)
if err != nil {
return fmt.Errorf("failed to update user: %w", err)
return domain.MoneyPool{}, fmt.Errorf("failed to update user: %w", err)
}
return nil

return moneyPool, nil
}

func (u Usecase) GetMoneyPool(moneyPoolID string) (domain.MoneyPool, error) {
Expand All @@ -62,7 +76,7 @@ func (u Usecase) UpdateMoneyPool(moneyPool domain.MoneyPool) error {
}

func (u Usecase) NewTransaction(transaction domain.Transaction, user domain.User) error {

return u.db.NewTransaction(transaction)
}

func (u Usecase) GetTransactionsByTimeRange(UserID string, moneyPoolID string, from time.Time, to time.Time) ([]domain.Transaction, error) {
Expand Down

0 comments on commit 3129575

Please sign in to comment.