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

Commit

Permalink
Add is_deleted column to money_pool table and
Browse files Browse the repository at this point in the history
update queries accordingly
  • Loading branch information
walnuts1018 committed Nov 6, 2023
1 parent 12ee6c6 commit eaf73fd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion back/domain/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "time"
// It is used to avoid repetition in public methods.
func (d *dbImpl) getMoneyPoolBalanceInternal(moneyPoolID string, date *time.Time, includePlanned bool) (float64, error) {
var balance float64
query := `SELECT COALESCE(SUM(amount), 0) FROM payment WHERE money_pool_id = $1`
query := `SELECT COALESCE(SUM(amount), 0) FROM payment WHERE money_pool_id = $1 AND is_deleted = false`
args := []interface{}{moneyPoolID}

// Add date condition if it is provided
Expand Down
14 changes: 7 additions & 7 deletions back/domain/moneypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func (d *dbImpl) NewMoneyPool(moneyPool MoneyPool) (MoneyPool, error) {
// クエリ文字列で位置パラメータを使用します。
query := `INSERT INTO money_pool (name, description, type, owner_id, emoji)
VALUES ($1, $2, $3, $4, $5)
query := `INSERT INTO money_pool (name, description, type, owner_id, emoji, is_deleted)
VALUES ($1, $2, $3, $4, $5, false)
RETURNING id`
// QueryRowを使用してIDを取得します。
var returnedID int64
Expand All @@ -27,7 +27,7 @@ func (d *dbImpl) NewMoneyPool(moneyPool MoneyPool) (MoneyPool, error) {

func (d *dbImpl) GetMoneyPool(id string) (MoneyPool, error) {
var moneyPool MoneyPool
query := `SELECT * FROM money_pool WHERE id = $1`
query := `SELECT * FROM money_pool WHERE id = $1 AND is_deleted = false`
err := d.db.Get(&moneyPool, query, id)
if err != nil {
return MoneyPool{}, fmt.Errorf("could not find money pool: %v", err)
Expand All @@ -37,7 +37,7 @@ func (d *dbImpl) GetMoneyPool(id string) (MoneyPool, error) {

func (d *dbImpl) GetMoneyPoolsByUserID(userID string) ([]MoneyPool, error) {
var moneyPools []MoneyPool
query := `SELECT * FROM money_pool WHERE owner_id = $1`
query := `SELECT * FROM money_pool WHERE owner_id = $1 AND is_deleted = false`
err := d.db.Select(&moneyPools, query, userID)
if err != nil {
return nil, fmt.Errorf("could not find money pools for user: %v", err)
Expand All @@ -52,15 +52,15 @@ func (d *dbImpl) UpdateMoneyPool(moneyPool MoneyPool) error {
}

var currentType string
// idに対して位置パラメータを使用して現在のタイプを取得します
// 公開タイプを取得
err = tx.Get(&currentType, "SELECT type FROM money_pool WHERE id = $1", moneyPool.ID)
if err != nil {
tx.Rollback()
return err
}

// if the current type is restricted and the new type is not restricted, delete the restricted publication scope table
if currentType == "restricted" && moneyPool.Type != "restricted" {
// pool_idに対して位置パラメータを使用してrestricted_publication_scopeから削除します
_, err := tx.Exec("DELETE FROM restricted_publication_scope WHERE pool_id = $1", moneyPool.ID)
if err != nil {
tx.Rollback()
Expand Down Expand Up @@ -116,7 +116,7 @@ func (d *dbImpl) ShareMoneyPoolWithUserGroups(moneyPoolID string, shareUserGroup
}

func (d *dbImpl) DeleteMoneyPool(id string) error {
query := `DELETE FROM money_pool WHERE id = $1`
query := `UPDATE money_pool SET is_deleted = true WHERE id = $1`
result, err := d.db.Exec(query, id)
if err != nil {
return fmt.Errorf("could not delete money pool: %v", err)
Expand Down
1 change: 1 addition & 0 deletions back/infra/psql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CREATE TABLE IF NOT EXISTS money_pool (
type VARCHAR(50) NOT NULL,
owner_id BIGINT NOT NULL,
emoji VARCHAR(255) NOT NULL,
is_deleted BOOLEAN NOT NULL,
FOREIGN KEY (owner_id) REFERENCES users(id)
);

Expand Down

0 comments on commit eaf73fd

Please sign in to comment.