Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[予算管理]局APIの作成 #907

Merged
merged 10 commits into from
Jan 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get全件データの取得
Kubosaka committed Jan 8, 2025
commit 3b8e9bbc61b4ce1af10ea38a71294ef2a8decf69
16 changes: 14 additions & 2 deletions api/externals/controller/financial_record_controller.go
Original file line number Diff line number Diff line change
@@ -24,11 +24,23 @@ func NewFinancialRecordController(u usecase.FinancialRecordUseCase) FinancialRec
}

func (f *financialRecordController) IndexFinancialRecords(c echo.Context) error {
bureaus, err := f.u.GetFinancialRecords(c.Request().Context())
year := c.QueryParam("year")
var financialRecordDetails generated.FinancialRecordDetails
var err error

if year != "" {
financialRecordDetails, err = f.u.GetFinancialRecordsByYears(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, financialRecordDetails)
}

financialRecordDetails, err = f.u.GetFinancialRecords(c.Request().Context())
if err != nil {
return err
}
return c.JSON(http.StatusOK, bureaus)
return c.JSON(http.StatusOK, financialRecordDetails)
}

func (f *financialRecordController) CreateFinancialRecord(c echo.Context) error {
34 changes: 28 additions & 6 deletions api/externals/repository/financial_record_repository.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,21 @@ func NewFinancialRecordRepository(c db.Client, ac abstract.Crud) FinancialRecord
func (frr *financialRecordRepository) All(
c context.Context,
) (*sql.Rows, error) {
query, _, err := dialect.Select("sponsors.*").From("sponsors").ToSQL()
query, _, err := dialect.Select(
"financial_records.id",
"financial_records.name", "years.year",
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"),
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"),
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")).
Comment on lines +39 to +41
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

データがNULLの時に0にしたいため、COALESCEを使いました

From("financial_records").
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))).
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))).
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))).
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))).
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))).
GroupBy("financial_records.id").
ToSQL()

if err != nil {
return nil, err
}
@@ -45,12 +59,20 @@ func (frr *financialRecordRepository) AllByPeriod(
c context.Context,
year string,
) (*sql.Rows, error) {
query, _, err := dialect.Select("sponsors.*").
From("sponsors").
InnerJoin(goqu.I("year_periods"), goqu.On(goqu.I("sponsors.created_at").Gt(goqu.I("year_periods.started_at")), goqu.I("sponsors.created_at").Lt(goqu.I("year_periods.ended_at")))).
InnerJoin(goqu.I("years"), goqu.On(goqu.I("year_periods.year_id").Eq(goqu.I("years.id")))).
query, _, err := dialect.Select(
"financial_records.id",
"financial_records.name", "years.year",
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"),
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"),
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")).
From("financial_records").
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))).
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))).
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))).
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))).
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))).
GroupBy("financial_records.id").
Where(goqu.Ex{"years.year": year}).
Order(goqu.I("sponsors.id").Desc()).
ToSQL()
if err != nil {
return nil, err
119 changes: 104 additions & 15 deletions api/internals/usecase/financial_record_usecase.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ import (

rep "github.com/NUTFes/FinanSu/api/externals/repository"
"github.com/NUTFes/FinanSu/api/generated"
"github.com/NUTFes/FinanSu/api/internals/domain"
"github.com/pkg/errors"
)

@@ -14,7 +13,8 @@ type financialRecordUseCase struct {
}

type FinancialRecordUseCase interface {
GetFinancialRecords(context.Context) ([]domain.Bureau, error)
GetFinancialRecords(context.Context) (generated.FinancialRecordDetails, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[imo]
何回も呼ばれるから一番下とかにgenerated省略したのて定義したい派閥です。

type FinancialRecordDetails = generated.FinancialRecordDetails

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに👍
型名省略

GetFinancialRecordsByYears(context.Context, string) (generated.FinancialRecordDetails, error)
CreateFinancialRecord(
context.Context,
generated.FinancialRecord,
@@ -31,31 +31,111 @@ func NewFinancialRecordUseCase(rep rep.FinancialRecordRepository) FinancialRecor
return &financialRecordUseCase{rep}
}

func (fru *financialRecordUseCase) GetFinancialRecords(c context.Context) ([]domain.Bureau, error) {
bureau := domain.Bureau{}
var bureaus []domain.Bureau
func (fru *financialRecordUseCase) GetFinancialRecords(
c context.Context,
) (generated.FinancialRecordDetails, error) {
var financialRecordDetails generated.FinancialRecordDetails
var financialRecordList []generated.FinancialRecordWithBalance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

financialRecordBalancesfinancialRecordsとかでも伝わりそう

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var total generated.Total

rows, err := fru.rep.All(c)
if err != nil {
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL")
}

defer rows.Close()

for rows.Next() {
var financialRecord generated.FinancialRecordWithBalance
err := rows.Scan(
&financialRecord.Id,
&financialRecord.Name,
&financialRecord.Year,
&financialRecord.Budget,
&financialRecord.Expense,
&financialRecord.Balance,
)

if err != nil {
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL")
}
financialRecordList = append(financialRecordList, financialRecord)
}

// totalを求める
budgetTotal := 0
expenseTotal := 0
balanceTotal := 0

for _, financialRecord := range financialRecordList {
budgetTotal += *financialRecord.Budget
expenseTotal += *financialRecord.Expense
balanceTotal += *financialRecord.Balance
}

total.Budget = &budgetTotal
total.Expense = &expenseTotal
total.Balance = &balanceTotal

// financialRecordDetails.FinancialRecords = &financialRecordList
financialRecordDetails.Total = &total
financialRecordDetails.FinancialRecords = &financialRecordList

return financialRecordDetails, err
}

func (fru *financialRecordUseCase) GetFinancialRecordsByYears(
c context.Context,
year string,
) (generated.FinancialRecordDetails, error) {
var financialRecordDetails generated.FinancialRecordDetails
var financialRecordList []generated.FinancialRecordWithBalance
var total generated.Total

year := "2021"
//クエリ実行
rows, err := fru.rep.AllByPeriod(c, year)
if err != nil {
return nil, err
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL")
}

defer rows.Close()

for rows.Next() {
var financialRecord generated.FinancialRecordWithBalance
err := rows.Scan(
&bureau.ID,
&bureau.Name,
&bureau.CreatedAt,
&bureau.UpdatedAt,
&financialRecord.Id,
&financialRecord.Name,
&financialRecord.Year,
&financialRecord.Budget,
&financialRecord.Expense,
&financialRecord.Balance,
)

if err != nil {
return nil, errors.Wrapf(err, "can not connect SQL")
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL")
}
bureaus = append(bureaus, bureau)
financialRecordList = append(financialRecordList, financialRecord)
}
return bureaus, nil

// totalを求める
budgetTotal := 0
expenseTotal := 0
balanceTotal := 0

for _, financialRecord := range financialRecordList {
budgetTotal += *financialRecord.Budget
expenseTotal += *financialRecord.Expense
balanceTotal += *financialRecord.Balance
}

total.Budget = &budgetTotal
total.Expense = &expenseTotal
total.Balance = &balanceTotal

// financialRecordDetails.FinancialRecords = &financialRecordList
financialRecordDetails.Total = &total
financialRecordDetails.FinancialRecords = &financialRecordList

return financialRecordDetails, err
}

func (fru *financialRecordUseCase) CreateFinancialRecord(
@@ -113,3 +193,12 @@ func (fru *financialRecordUseCase) DestroyFinancialRecord(c context.Context, id
err := fru.rep.Delete(c, id)
return err
}

type FinancialRecordDetailColumn struct {
Id *string
Name *string
Year *string
Budget *int
Expense *int
Balance *int
}