Skip to content

Commit

Permalink
getにdivision_idのパラメータ追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Jan 8, 2025
1 parent b4eab42 commit 1918da3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/externals/controller/festival_item_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ func NewFestivalItemController(u usecase.FestivalItemUseCase) FestivalItemContro

func (f *festivalItemController) IndexFestivalItems(c echo.Context) error {
year := c.QueryParam("year")
division_id := c.QueryParam("division_id")
var festivalItemDetails generated.FestivalItemDetails
var err error

if year != "" && division_id != "" {
festivalItemDetails, err = f.u.GetFestivalItemsByYearsAndDivision(c.Request().Context(), year, division_id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, festivalItemDetails)
}

if year != "" {
festivalItemDetails, err = f.u.GetFestivalItemsByYears(c.Request().Context(), year)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions api/externals/repository/festival_item_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type festivalItemRepository struct {
type FestivalItemRepository interface {
All(context.Context) (*sql.Rows, error)
AllByPeriod(context.Context, string) (*sql.Rows, error)
AllByPeriodAndDivision(context.Context, string, string) (*sql.Rows, error)
GetById(context.Context, string) (*sql.Row, error)
CreateFestivalItem(context.Context, *sql.Tx, generated.FestivalItem) error
CreateItemBudget(context.Context, *sql.Tx, generated.FestivalItem) error
Expand Down Expand Up @@ -94,6 +95,39 @@ func (fir *festivalItemRepository) AllByPeriod(
return fir.crud.Read(c, query)
}

// 年度別と部門で取得
func (fir *festivalItemRepository) AllByPeriodAndDivision(
c context.Context,
year string,
divisionId string,
) (*sql.Rows, error) {
query, _, err := dialect.Select(
"festival_items.id",
"festival_items.name",
"festival_items.memo",
"financial_records.name",
"divisions.name",
goqu.L("COALESCE(`item_budgets`.`amount`, 0)").As("budget"),
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"),
goqu.COALESCE(goqu.L("item_budgets.amount - COALESCE(SUM(`buy_reports`.`amount`), 0)"), 0).As("balance")).
From("festival_items").
InnerJoin(goqu.I("divisions"), goqu.On(goqu.I("festival_items.division_id").Eq(goqu.I("divisions.id")))).
InnerJoin(goqu.I("financial_records"), goqu.On(goqu.I("divisions.financial_record_id").Eq(goqu.I("financial_records.id")))).
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.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("festival_items.id", "item_budgets.amount").
Order(goqu.I("festival_items.id").Desc()).
Where(goqu.Ex{"divisions.id": divisionId}).
Where(goqu.Ex{"years.year": year}).
ToSQL()

if err != nil {
return nil, err
}
return fir.crud.Read(c, query)
}

// IDで取得
func (fir *festivalItemRepository) GetById(
c context.Context,
Expand Down
10 changes: 10 additions & 0 deletions api/generated/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions api/internals/usecase/festival_item_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type festivalItemUseCase struct {
type FestivalItemUseCase interface {
GetFestivalItems(context.Context) (generated.FestivalItemDetails, error)
GetFestivalItemsByYears(context.Context, string) (generated.FestivalItemDetails, error)
GetFestivalItemsByYearsAndDivision(context.Context, string, string) (generated.FestivalItemDetails, error)
CreateFestivalItem(
context.Context,
generated.FestivalItem,
Expand Down Expand Up @@ -135,6 +136,60 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYears(
return festivalItemDetails, nil
}

func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision(
c context.Context,
year string,
divisionId string,
) (generated.FestivalItemDetails, error) {
var festivalItemDetails generated.FestivalItemDetails
var festivalItems []generated.FestivalItemWithBalance

rows, err := fiu.rep.AllByPeriodAndDivision(c, year, divisionId)
if err != nil {
return festivalItemDetails, err
}

defer rows.Close()
for rows.Next() {
var festivalItem generated.FestivalItemWithBalance
err := rows.Scan(
&festivalItem.Id,
&festivalItem.Name,
&festivalItem.Memo,
&festivalItem.FinancialRecord,
&festivalItem.Division,
&festivalItem.Budget,
&festivalItem.Expense,
&festivalItem.Balance,
)
if err != nil {
return festivalItemDetails, err
}
festivalItems = append(festivalItems, festivalItem)
}
festivalItemDetails.FestivalItems = &festivalItems

var total generated.Total

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

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

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

festivalItemDetails.Total = &total
return festivalItemDetails, nil
}

func (fiu *festivalItemUseCase) CreateFestivalItem(
c context.Context,
festivalItem generated.FestivalItem,
Expand Down
5 changes: 5 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,11 @@ paths:
description: year
schema:
type: integer
- name: division_id
in: query
description: division_id
schema:
type: integer
responses:
"200":
description: festival_itemの一覧を取得
Expand Down

0 comments on commit 1918da3

Please sign in to comment.