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

[マイページ]部門予算・詳細取得 #915

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions api/externals/controller/festival_item_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type festivalItemController struct {

type FestivalItemController interface {
IndexFestivalItems(echo.Context) error
IndexFestivalItemsForMypage(echo.Context) error
CreateFestivalItem(echo.Context) error
UpdateFestivalItem(echo.Context) error
DestroyFestivalItem(echo.Context) error
Expand All @@ -36,6 +37,19 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error {
return c.JSON(http.StatusOK, festivalItemDetails)
}

func (f *festivalItemController) IndexFestivalItemsForMypage(c echo.Context) error {
ctx := c.Request().Context()
userId := c.Param("user_id")
year := c.QueryParam("year")
var festivalItemDetails []FestivalItemsForMyPage

festivalItemDetails, err := f.u.GetFestvalItemsForMypage(ctx, year, userId)
if err != nil {
return err
}
return c.JSON(http.StatusOK, festivalItemDetails)
}

func (f *festivalItemController) CreateFestivalItem(c echo.Context) error {
festivalItem := new(FestivalItem)
if err := c.Bind(festivalItem); err != nil {
Expand Down Expand Up @@ -76,3 +90,4 @@ func (f *festivalItemController) DestroyFestivalItem(c echo.Context) error {

type FestivalItemDetails = generated.FestivalItemDetails
type FestivalItem = generated.FestivalItem
type FestivalItemsForMyPage = generated.FestivalItemsForMyPage
51 changes: 51 additions & 0 deletions api/externals/repository/festival_item_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type FestivalItemRepository interface {
StartTransaction(context.Context) (*sql.Tx, error)
RollBack(context.Context, *sql.Tx) error
Commit(context.Context, *sql.Tx) error
GetDetailByDivisionId(context.Context, string, string) (*sql.Rows, error)
}

func NewFestivalItemRepository(c db.Client, ac abstract.Crud) FestivalItemRepository {
Expand Down Expand Up @@ -191,6 +192,30 @@ func (fir *festivalItemRepository) Commit(c context.Context, tx *sql.Tx) error {
return fir.crud.Commit(c, tx)
}

// 年度別と部門で取得
func (fir *festivalItemRepository) GetDetailByDivisionId(
c context.Context,
year string,
userId string,
) (*sql.Rows, error) {
ds := selectFestivalItemForMypageQuery
if userId != "" {
ds = ds.Where(goqu.Ex{"users.id": userId})
}

if year != "" {
ds = ds.Where(goqu.Ex{"years.year": year})
}

query, _, err := ds.ToSQL()

if err != nil {
return nil, err
}

return fir.crud.Read(c, query)
}

var selectFestivalItemQuery = dialect.Select(
"festival_items.id",
"festival_items.name",
Expand All @@ -208,3 +233,29 @@ var selectFestivalItemQuery = dialect.Select(
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())

var selectFestivalItemForMypageQuery = dialect.Select(
goqu.I("users.name").As("userName"),
goqu.I("financial_records.name").As("financialRecordName"),
goqu.I("divisions.id").As("divisionId"),
goqu.I("divisions.name").As("divisionName"),
goqu.I("festival_items.id").As("festivalItemId"),
goqu.I("festival_items.name").As("festivalItemName"),
goqu.I("years.year"),
goqu.COALESCE(goqu.I("item_budgets.amount"), 0).As("budgetAmount"),
goqu.COALESCE(goqu.I("buy_reports.id"), 0).As("buyReportId"),
goqu.COALESCE(goqu.I("buy_reports.paid_by"), "").As("paidBy"),
goqu.COALESCE(goqu.I("buy_reports.amount"), 0).As("reportAmount"),
goqu.COALESCE(goqu.I("buy_reports.created_at"), "2025-01-29 20:53:44").As("reportDate"),
goqu.COALESCE(goqu.I("buy_statuses.is_packed"), 0).As("isPacked"),
goqu.COALESCE(goqu.I("buy_statuses.is_settled"), 0).As("isSettled")).
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("user_groups"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("user_groups.group_id")))).
InnerJoin(goqu.I("users"), goqu.On(goqu.I("users.id").Eq(goqu.I("user_groups.user_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")))).
LeftJoin(goqu.I("buy_statuses"), goqu.On(goqu.I("buy_reports.id").Eq(goqu.I("buy_statuses.buy_report_id")))).
Order(goqu.I("festival_items.id").Desc())
Comment on lines +237 to +261
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

これで必要なデータとってくる

37 changes: 26 additions & 11 deletions api/generated/openapi_gen.go

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

17 changes: 17 additions & 0 deletions api/internals/domain/festival_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ type FestivalItem struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type FestivalItemForMyPageColumn struct {
UserName string
FinancialRecordName string
DivisionId int
DivisionName string
FestivalItemId int
FestivalItemName string
Year int
BudgetAmount int
BuyReportId int
PaidBy string
ReportAmount int
ReportDate string
IsPacked bool
IsSettled bool
}
Comment on lines +16 to +31
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

取得したdbのデータを入れる用の型

145 changes: 145 additions & 0 deletions api/internals/usecase/festival_item_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package usecase

import (
"context"
"fmt"

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

type festivalItemUseCase struct {
Expand All @@ -13,6 +15,7 @@ type festivalItemUseCase struct {

type FestivalItemUseCase interface {
GetFestivalItems(context.Context, string, string) (FestivalItemDetails, error)
GetFestvalItemsForMypage(context.Context, string, string) ([]FestivalItemDetailsForMypage, error)
CreateFestivalItem(
context.Context,
FestivalItem,
Expand Down Expand Up @@ -203,6 +206,148 @@ func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string
return nil
}

func (fiu *festivalItemUseCase) GetFestvalItemsForMypage(
c context.Context,
year string,
userId string,
) ([]FestivalItemDetailsForMypage, error) {
// var festivalItemDetails FestivalItemDetailsForMypage
var festivalItemDetailsList []FestivalItemDetailsForMypage

var festivalItemForMyPageColumns []domain.FestivalItemForMyPageColumn

rows, err := fiu.rep.GetDetailByDivisionId(c, year, userId)
if err != nil {
return festivalItemDetailsList, err
}

defer rows.Close()
for rows.Next() {
var festivalItemForMyPageColumn domain.FestivalItemForMyPageColumn
err := rows.Scan(
&festivalItemForMyPageColumn.UserName,
&festivalItemForMyPageColumn.FinancialRecordName,
&festivalItemForMyPageColumn.DivisionId,
&festivalItemForMyPageColumn.DivisionName,
&festivalItemForMyPageColumn.FestivalItemId,
&festivalItemForMyPageColumn.FestivalItemName,
&festivalItemForMyPageColumn.Year,
&festivalItemForMyPageColumn.BudgetAmount,
&festivalItemForMyPageColumn.BuyReportId,
&festivalItemForMyPageColumn.PaidBy,
&festivalItemForMyPageColumn.ReportAmount,
&festivalItemForMyPageColumn.ReportDate,
&festivalItemForMyPageColumn.IsPacked,
&festivalItemForMyPageColumn.IsSettled,
)
if err != nil {
fmt.Println(err)
return festivalItemDetailsList, err
}
festivalItemForMyPageColumns = append(festivalItemForMyPageColumns, festivalItemForMyPageColumn)
}

festivalItemDetailsList = convertColumnToGenerated(festivalItemForMyPageColumns)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

この関数内でdbから取得したデータを整形


return festivalItemDetailsList, nil
}

type FestivalItemDetails = generated.FestivalItemDetails
type FestivalItem = generated.FestivalItem
type FestivalItemWithBalance = generated.FestivalItemWithBalance
type FestivalItemDetailsForMypage = generated.FestivalItemsForMyPage
type FestivalItemWithReport = generated.FestivalItemWithReport
type BuyReport = generated.BuyReportInformation

func convertColumnToGenerated(festivalItemForMyPageColumns []domain.FestivalItemForMyPageColumn) []FestivalItemDetailsForMypage {
var festivalItemDetailsList []FestivalItemDetailsForMypage

// 部門ごとにマップを作成
var festivalItemDetailsForMypageMap = make(map[string]FestivalItemDetailsForMypage)
var festivalItemMaps = make(map[string]map[string]FestivalItemWithReport)

for _, festivalItemForMyPageColumn := range festivalItemForMyPageColumns {
festivalItemDetailsForMypage := festivalItemDetailsForMypageMap[festivalItemForMyPageColumn.DivisionName]
// 局と部門名前定義
festivalItemDetailsForMypage.DivisionName = &festivalItemForMyPageColumn.DivisionName
festivalItemDetailsForMypage.FinancialRecordName = &festivalItemForMyPageColumn.FinancialRecordName

// 予算と支出データ集計
festivalItemMap := festivalItemMaps[festivalItemForMyPageColumn.DivisionName]
if festivalItemMap == nil {
festivalItemMap = make(map[string]FestivalItemWithReport)
}
festivalItemWithReport := festivalItemMap[festivalItemForMyPageColumn.FestivalItemName]
festivalItemWithReport.FestivalItemName = &festivalItemForMyPageColumn.FestivalItemName

// totalがなければ定義
if festivalItemWithReport.FestivalItemTotal == nil {
expense, budget, balance := 0, 0, 0
var total Total
total.Expense, total.Budget, total.Balance = &expense, &budget, &balance
festivalItemWithReport.FestivalItemTotal = &total
}

*festivalItemWithReport.FestivalItemTotal.Budget += festivalItemForMyPageColumn.BudgetAmount
*festivalItemWithReport.FestivalItemTotal.Expense += festivalItemForMyPageColumn.ReportAmount
*festivalItemWithReport.FestivalItemTotal.Balance += festivalItemForMyPageColumn.BudgetAmount - festivalItemForMyPageColumn.ReportAmount

buyReports := festivalItemWithReport.BuyReports
if buyReports == nil {
var buyReportSlice []generated.BuyReportInformation
buyReports = &buyReportSlice
}

var buyReport BuyReport
buyReport.Id = &festivalItemForMyPageColumn.BuyReportId
buyReport.BuyReportName = &festivalItemForMyPageColumn.PaidBy
buyReport.Amount = &festivalItemForMyPageColumn.ReportAmount
buyReport.ReportDate = &festivalItemForMyPageColumn.ReportDate

if festivalItemForMyPageColumn.IsSettled {
buyReport.Status = &isSettled
} else if festivalItemForMyPageColumn.IsPacked {
buyReport.Status = &isPacked
} else {
buyReport.Status = &empty
}

if *buyReport.Amount > 0 {
*buyReports = append(*buyReports, buyReport)
}

festivalItemWithReport.BuyReports = buyReports

festivalItemMap[festivalItemForMyPageColumn.FestivalItemName] = festivalItemWithReport
festivalItemMaps[festivalItemForMyPageColumn.DivisionName] = festivalItemMap

// divisionのtotalがなければ定義
if festivalItemDetailsForMypage.DivisionTotal == nil {
expense, budget, balance := 0, 0, 0
var total Total
total.Expense, total.Budget, total.Balance = &expense, &budget, &balance
festivalItemDetailsForMypage.DivisionTotal = &total
}

festivalItemDetailsForMypageMap[festivalItemForMyPageColumn.DivisionName] = festivalItemDetailsForMypage
}

for _, festivalItemDetails := range festivalItemDetailsForMypageMap {
newFestivalItemDetails := festivalItemDetails
festivalItems := festivalItemMaps[*festivalItemDetails.DivisionName]
var festivalItemWithReports []FestivalItemWithReport
for _, festivalItem := range festivalItems {
festivalItemWithReports = append(festivalItemWithReports, festivalItem)
*festivalItemDetails.DivisionTotal.Budget += *festivalItem.FestivalItemTotal.Budget
*festivalItemDetails.DivisionTotal.Expense += *festivalItem.FestivalItemTotal.Expense
*festivalItemDetails.DivisionTotal.Balance += *festivalItem.FestivalItemTotal.Balance
}
newFestivalItemDetails.FestivalItems = &festivalItemWithReports
festivalItemDetailsList = append(festivalItemDetailsList, newFestivalItemDetails)
}
return festivalItemDetailsList
}

var empty = generated.Empty
var isPacked = generated.N1
var isSettled = generated.N2
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (r router) ProvideRouter(e *echo.Echo) {

// festival items
e.GET("/festival_items", r.festivalItemController.IndexFestivalItems)
e.GET("/festival_items/details/:user_id", r.festivalItemController.IndexFestivalItemsForMypage)
e.POST("/festival_items", r.festivalItemController.CreateFestivalItem)
e.PUT("/festival_items/:id", r.festivalItemController.UpdateFestivalItem)
e.DELETE("/festival_items/:id", r.festivalItemController.DestroyFestivalItem)
Expand Down
Loading
Loading