From f0be41b38222da58ee8b17ec19ec3f3679d5acf7 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Wed, 8 Jan 2025 17:14:44 +0900 Subject: [PATCH 01/19] =?UTF-8?q?=E3=83=AB=E3=83=BC=E3=83=86=E3=82=A3?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/festival_item_controller.go | 82 ++++++++ .../repository/festival_item_repository.go | 175 ++++++++++++++++++ api/internals/di/di.go | 4 + .../usecase/festival_item_usecase.go | 108 +++++++++++ api/router/router.go | 9 + 5 files changed, 378 insertions(+) create mode 100644 api/externals/controller/festival_item_controller.go create mode 100644 api/externals/repository/festival_item_repository.go create mode 100644 api/internals/usecase/festival_item_usecase.go diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go new file mode 100644 index 00000000..8906d0d6 --- /dev/null +++ b/api/externals/controller/festival_item_controller.go @@ -0,0 +1,82 @@ +package controller + +import ( + "net/http" + + "github.com/NUTFes/FinanSu/api/generated" + "github.com/NUTFes/FinanSu/api/internals/usecase" + "github.com/labstack/echo/v4" +) + +type festivalItemController struct { + u usecase.FestivalItemUseCase +} + +type FestivalItemController interface { + IndexFestivalItems(echo.Context) error + CreateFestivalItem(echo.Context) error + UpdateFestivalItem(echo.Context) error + DestroyFestivalItem(echo.Context) error +} + +func NewFestivalItemController(u usecase.FestivalItemUseCase) FestivalItemController { + return &festivalItemController{u} +} + +func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { + year := c.QueryParam("year") + var festivalItemDetails generated.FestivalItemDetails + var err error + + if year != "" { + festivalItemDetails, err = f.u.GetFestivalItemsByYears(c.Request().Context(), year) + if err != nil { + return err + } + return c.JSON(http.StatusOK, festivalItemDetails) + } + + festivalItemDetails, err = f.u.GetFestivalItems(c.Request().Context()) + if err != nil { + return err + } + return c.JSON(http.StatusOK, festivalItemDetails) +} + +func (f *festivalItemController) CreateFestivalItem(c echo.Context) error { + festivalItem := new(generated.FestivalItem) + if err := c.Bind(festivalItem); err != nil { + return c.String(http.StatusBadRequest, "Bad Request") + } + latastFestivalItem, err := f.u.CreateFestivalItem(c.Request().Context(), *festivalItem) + if err != nil { + return err + } + return c.JSON(http.StatusOK, latastFestivalItem) +} + +func (f *festivalItemController) UpdateFestivalItem(c echo.Context) error { + id := c.Param("id") + festivalItem := new(generated.FestivalItem) + if err := c.Bind(festivalItem); err != nil { + return c.String(http.StatusBadRequest, "Bad Request") + } + updatedFestivalItem, err := f.u.UpdateFestivalItem( + c.Request().Context(), + id, + *festivalItem, + ) + if err != nil { + return err + } + return c.JSON(http.StatusOK, updatedFestivalItem) +} + +func (f *festivalItemController) DestroyFestivalItem(c echo.Context) error { + id := c.Param("id") + err := f.u.DestroyFestivalItem(c.Request().Context(), id) + if err != nil { + return err + } + return c.String(http.StatusOK, "Destroy Festival Item") +} diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go new file mode 100644 index 00000000..2cc9a483 --- /dev/null +++ b/api/externals/repository/festival_item_repository.go @@ -0,0 +1,175 @@ +package repository + +import ( + "context" + "database/sql" + + "github.com/NUTFes/FinanSu/api/drivers/db" + "github.com/NUTFes/FinanSu/api/externals/repository/abstract" + "github.com/NUTFes/FinanSu/api/generated" + goqu "github.com/doug-martin/goqu/v9" +) + +type festivalItemRepository struct { + client db.Client + crud abstract.Crud +} + +type FestivalItemRepository interface { + All(context.Context) (*sql.Rows, error) + AllByPeriod(context.Context, string) (*sql.Rows, error) + GetById(context.Context, string) (*sql.Row, error) + Create(context.Context, generated.FestivalItem) error + Update(context.Context, string, generated.FestivalItem) error + Delete(context.Context, string) error + FindLatestRecord(context.Context) (*sql.Row, error) +} + +func NewFestivalItemRepository(c db.Client, ac abstract.Crud) FestivalItemRepository { + return &festivalItemRepository{c, ac} +} + +// 年度別に取得 +func (fir *festivalItemRepository) All( + c context.Context, +) (*sql.Rows, error) { + 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"). + ToSQL() + + if err != nil { + return nil, err + } + return fir.crud.Read(c, query) +} + +// 年度別に取得 +func (fir *festivalItemRepository) AllByPeriod( + c context.Context, + year string, +) (*sql.Rows, error) { + 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}). + ToSQL() + if err != nil { + return nil, err + } + return fir.crud.Read(c, query) +} + +// IDで取得 +func (fir *festivalItemRepository) GetById( + c context.Context, + id string, +) (*sql.Row, error) { + 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{"financial_records.id": id}). + ToSQL() + if err != nil { + return nil, err + } + return fir.crud.ReadByID(c, query) +} + +// 作成 +func (fir *festivalItemRepository) Create( + c context.Context, + festivalItem generated.FestivalItem, +) error { + ds := dialect.Insert("financial_records"). + Rows(goqu.Record{"name": festivalItem.Name, "year_id": festivalItem.Memo}) + query, _, err := ds.ToSQL() + if err != nil { + return err + } + return fir.crud.UpdateDB(c, query) +} + +// 編集 +func (fir *festivalItemRepository) Update( + c context.Context, + id string, + festivalItem generated.FestivalItem, +) error { + ds := dialect.Update("financial_records"). + Set(goqu.Record{"name": festivalItem.Name, "year_id": festivalItem.Memo}). + Where(goqu.Ex{"id": id}) + query, _, err := ds.ToSQL() + if err != nil { + return err + } + return fir.crud.UpdateDB(c, query) +} + +// 削除 +func (fir *festivalItemRepository) Delete( + c context.Context, + id string, +) error { + ds := dialect.Delete("financial_records").Where(goqu.Ex{"id": id}) + query, _, err := ds.ToSQL() + if err != nil { + return err + } + return fir.crud.UpdateDB(c, query) + +} + +// 最新のfestivalItemを取得する +func (fir *festivalItemRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { + 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"). + Order(goqu.I("id").Desc()). + Limit(1). + ToSQL() + if err != nil { + return nil, err + } + return fir.crud.ReadByID(c, query) +} diff --git a/api/internals/di/di.go b/api/internals/di/di.go index c9ce3006..d3b24fb3 100644 --- a/api/internals/di/di.go +++ b/api/internals/di/di.go @@ -31,6 +31,7 @@ func InitializeServer() db.Client { bureauRepository := repository.NewBureauRepository(client, crud) departmentRepository := repository.NewDepartmentRepository(client, crud) expenseRepository := repository.NewExpenseRepository(client, crud) + festivalItemRepository := repository.NewFestivalItemRepository(client, crud) financialRecordRepository := repository.NewFinancialRecordRepository(client, crud) fundInformationRepository := repository.NewFundInformationRepository(client, crud) mailAuthRepository := repository.NewMailAuthRepository(client, crud) @@ -58,6 +59,7 @@ func InitializeServer() db.Client { bureauUseCase := usecase.NewBureauUseCase(bureauRepository) departmentUseCase := usecase.NewDepartmentUseCase(departmentRepository) expenseUseCase := usecase.NewExpenseUseCase(expenseRepository) + festivalUseCase := usecase.NewFestivalItemUseCase(festivalItemRepository) financialRecordUseCase := usecase.NewFinancialRecordUseCase(financialRecordRepository) fundInformationUseCase := usecase.NewFundInformationUseCase(fundInformationRepository) mailAuthUseCase := usecase.NewMailAuthUseCase(mailAuthRepository, sessionRepository) @@ -92,6 +94,7 @@ func InitializeServer() db.Client { bureauController := controller.NewBureauController(bureauUseCase) departmentController := controller.NewDepartmentController(departmentUseCase) expenseController := controller.NewExpenseController(expenseUseCase) + festivalItemController := controller.NewFestivalItemController(festivalUseCase) financialRecordController := controller.NewFinancialRecordController(financialRecordUseCase) fundInformationController := controller.NewFundInformationController(fundInformationUseCase) healthcheckController := controller.NewHealthCheckController() @@ -120,6 +123,7 @@ func InitializeServer() db.Client { bureauController, departmentController, expenseController, + festivalItemController, financialRecordController, fundInformationController, healthcheckController, diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go new file mode 100644 index 00000000..b614bdab --- /dev/null +++ b/api/internals/usecase/festival_item_usecase.go @@ -0,0 +1,108 @@ +package usecase + +import ( + "context" + + rep "github.com/NUTFes/FinanSu/api/externals/repository" + "github.com/NUTFes/FinanSu/api/generated" +) + +type festivalItemUseCase struct { + rep rep.FestivalItemRepository +} + +type FestivalItemUseCase interface { + GetFestivalItems(context.Context) (generated.FestivalItemDetails, error) + GetFestivalItemsByYears(context.Context, string) (generated.FestivalItemDetails, error) + CreateFestivalItem( + context.Context, + generated.FestivalItem, + ) (generated.FestivalItemWithBalance, error) + UpdateFestivalItem( + context.Context, + string, + generated.FestivalItem, + ) (generated.FestivalItemWithBalance, error) + DestroyFestivalItem(context.Context, string) error +} + +func NewFestivalItemUseCase(rep rep.FestivalItemRepository) FestivalItemUseCase { + return &festivalItemUseCase{rep} +} + +func (fiu *festivalItemUseCase) GetFestivalItems( + c context.Context, +) (generated.FestivalItemDetails, error) { + var festivalItemDetails generated.FestivalItemDetails + return festivalItemDetails, nil +} + +func (fiu *festivalItemUseCase) GetFestivalItemsByYears( + c context.Context, + year string, +) (generated.FestivalItemDetails, error) { + var festivalItemDetails generated.FestivalItemDetails + return festivalItemDetails, nil +} + +func (fiu *festivalItemUseCase) CreateFestivalItem( + c context.Context, + festivalItem generated.FestivalItem, +) (generated.FestivalItemWithBalance, error) { + latastFestivalItemWithBalance := generated.FestivalItemWithBalance{} + // err := fiu.rep.Create(c, financialRecord) + // if err != nil { + // return latastFinancialRecordWithBalance, err + // } + // row, err := fiu.rep.FindLatestRecord(c) + // if err != nil { + // return latastFinancialRecordWithBalance, err + // } + // err = row.Scan( + // &latastFinancialRecordWithBalance.Id, + // &latastFinancialRecordWithBalance.Name, + // &latastFinancialRecordWithBalance.Year, + // &latastFinancialRecordWithBalance.Budget, + // &latastFinancialRecordWithBalance.Expense, + // &latastFinancialRecordWithBalance.Balance, + // ) + // if err != nil { + // return latastFinancialRecordWithBalance, err + // } + return latastFestivalItemWithBalance, nil +} + +func (fiu *festivalItemUseCase) UpdateFestivalItem( + c context.Context, + id string, + festivalItem generated.FestivalItem, +) (generated.FestivalItemWithBalance, error) { + updateFestivalItem := generated.FestivalItemWithBalance{} + + // if err := fiu.rep.Update(c, id, festivalItem); err != nil { + // return updateFestivalItem, err + // } + + // row, err := fiu.rep.GetById(c, id) + // if err != nil { + // return updateFestivalItem, err + // } + + // if err = row.Scan( + // &updateFinancialRecord.Id, + // &updateFinancialRecord.Name, + // &updateFinancialRecord.Year, + // &updateFinancialRecord.Budget, + // &updateFinancialRecord.Expense, + // &updateFinancialRecord.Balance, + // ); err != nil { + // return updateFinancialRecord, err + // } + + return updateFestivalItem, nil +} + +func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string) error { + err := fiu.rep.Delete(c, id) + return err +} diff --git a/api/router/router.go b/api/router/router.go index f8d3c735..12898e62 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -13,6 +13,7 @@ type router struct { bureauController controller.BureauController departmentController controller.DepartmentController expenseController controller.ExpenseController + festivalItemController controller.FestivalItemController financialRecordController controller.FinancialRecordController fundInformationController controller.FundInformationController healthcheckController controller.HealthcheckController @@ -42,6 +43,7 @@ func NewRouter( bureauController controller.BureauController, departmentController controller.DepartmentController, expenseController controller.ExpenseController, + festivalItemController controller.FestivalItemController, financialRecordController controller.FinancialRecordController, fundInformationController controller.FundInformationController, healthController controller.HealthcheckController, @@ -66,6 +68,7 @@ func NewRouter( bureauController, departmentController, expenseController, + festivalItemController, financialRecordController, fundInformationController, healthController, @@ -158,6 +161,12 @@ func (r router) ProvideRouter(e *echo.Echo) { e.PUT("/expenses/:id", r.expenseController.UpdateExpense) e.DELETE("/expenses/:id", r.expenseController.DestroyExpense) + // festival items + e.GET("/festival_items", r.festivalItemController.IndexFestivalItems) + e.POST("/festival_items", r.festivalItemController.CreateFestivalItem) + e.PUT("/festival_items/:id", r.festivalItemController.UpdateFestivalItem) + e.DELETE("/festival_items/:id", r.festivalItemController.DestroyFestivalItem) + // financial_records e.GET("/financial_records", r.financialRecordController.IndexFinancialRecords) e.POST("/financial_records", r.financialRecordController.CreateFinancialRecord) From 6a35037bb8b132d8c623c709ef2ba28318bb312f Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Wed, 8 Jan 2025 17:18:37 +0900 Subject: [PATCH 02/19] =?UTF-8?q?openapi=E3=81=AByear=E3=83=91=E3=83=A9?= =?UTF-8?q?=E3=83=A1=E3=83=BC=E3=82=BF=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/generated/openapi_gen.go | 19 +++++++++++++-- openapi/openapi.yaml | 6 +++++ view/next-project/src/generated/hooks.ts | 24 ++++++++++++------- .../next-project/src/generated/model/index.ts | 1 + 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/api/generated/openapi_gen.go b/api/generated/openapi_gen.go index 7883cf9f..076f7f7e 100644 --- a/api/generated/openapi_gen.go +++ b/api/generated/openapi_gen.go @@ -276,6 +276,12 @@ type PutExpensesIdParams struct { YearId *string `form:"year_id,omitempty" json:"year_id,omitempty"` } +// GetFestivalItemsParams defines parameters for GetFestivalItems. +type GetFestivalItemsParams struct { + // Year year + Year *int `form:"year,omitempty" json:"year,omitempty"` +} + // GetFinancialRecordsParams defines parameters for GetFinancialRecords. type GetFinancialRecordsParams struct { // Year year @@ -652,7 +658,7 @@ type ServerInterface interface { GetExpensesIdDetails(ctx echo.Context, id int) error // (GET /festival_items) - GetFestivalItems(ctx echo.Context) error + GetFestivalItems(ctx echo.Context, params GetFestivalItemsParams) error // (POST /festival_items) PostFestivalItems(ctx echo.Context) error @@ -1670,8 +1676,17 @@ func (w *ServerInterfaceWrapper) GetExpensesIdDetails(ctx echo.Context) error { func (w *ServerInterfaceWrapper) GetFestivalItems(ctx echo.Context) error { var err error + // Parameter object where we will unmarshal all parameters from the context + var params GetFestivalItemsParams + // ------------- Optional query parameter "year" ------------- + + err = runtime.BindQueryParameter("form", true, false, "year", ctx.QueryParams(), ¶ms.Year) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter year: %s", err)) + } + // Invoke the callback with all the unmarshaled arguments - err = w.Handler.GetFestivalItems(ctx) + err = w.Handler.GetFestivalItems(ctx, params) return err } diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index 3501e6ce..90414c7f 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -1007,6 +1007,12 @@ paths: tags: - festival_item description: festival_itemの一覧の取得 + parameters: + - name: year + in: query + description: year + schema: + type: integer responses: "200": description: festival_itemの一覧を取得 diff --git a/view/next-project/src/generated/hooks.ts b/view/next-project/src/generated/hooks.ts index cd2aadb1..deb2b5c0 100644 --- a/view/next-project/src/generated/hooks.ts +++ b/view/next-project/src/generated/hooks.ts @@ -78,6 +78,7 @@ import type { GetExpensesFiscalyearYear200, GetExpensesId200, GetExpensesIdDetails200, + GetFestivalItemsParams, GetFinancialRecordsParams, GetFundInformations200, GetFundInformationsDetails200, @@ -2951,15 +2952,22 @@ export type getFestivalItemsResponse = { headers: Headers; } -export const getGetFestivalItemsUrl = () => { +export const getGetFestivalItemsUrl = (params?: GetFestivalItemsParams,) => { + const normalizedParams = new URLSearchParams(); + Object.entries(params || {}).forEach(([key, value]) => { + + if (value !== undefined) { + normalizedParams.append(key, value === null ? 'null' : value.toString()) + } + }); - return `/festival_items` + return normalizedParams.size ? `/festival_items?${normalizedParams.toString()}` : `/festival_items` } -export const getFestivalItems = async ( options?: RequestInit): Promise => { +export const getFestivalItems = async (params?: GetFestivalItemsParams, options?: RequestInit): Promise => { - return customFetch>(getGetFestivalItemsUrl(), + return customFetch>(getGetFestivalItemsUrl(params), { ...options, method: 'GET' @@ -2971,19 +2979,19 @@ export const getFestivalItems = async ( options?: RequestInit): Promise [`/festival_items`] as const; +export const getGetFestivalItemsKey = (params?: GetFestivalItemsParams,) => [`/festival_items`, ...(params ? [params]: [])] as const; export type GetFestivalItemsQueryResult = NonNullable>> export type GetFestivalItemsQueryError = unknown export const useGetFestivalItems = ( - options?: { swr?:SWRConfiguration>, TError> & { swrKey?: Key, enabled?: boolean }, request?: SecondParameter } + params?: GetFestivalItemsParams, options?: { swr?:SWRConfiguration>, TError> & { swrKey?: Key, enabled?: boolean }, request?: SecondParameter } ) => { const {swr: swrOptions, request: requestOptions} = options ?? {} const isEnabled = swrOptions?.enabled !== false - const swrKey = swrOptions?.swrKey ?? (() => isEnabled ? getGetFestivalItemsKey() : null); - const swrFn = () => getFestivalItems(requestOptions) + const swrKey = swrOptions?.swrKey ?? (() => isEnabled ? getGetFestivalItemsKey(params) : null); + const swrFn = () => getFestivalItems(params, requestOptions) const query = useSwr>, TError>(swrKey, swrFn, swrOptions) diff --git a/view/next-project/src/generated/model/index.ts b/view/next-project/src/generated/model/index.ts index db4a8cc4..7858bbb1 100644 --- a/view/next-project/src/generated/model/index.ts +++ b/view/next-project/src/generated/model/index.ts @@ -71,6 +71,7 @@ export * from './getExpensesDetailsYear200'; export * from './getExpensesFiscalyearYear200'; export * from './getExpensesId200'; export * from './getExpensesIdDetails200'; +export * from './getFestivalItemsParams'; export * from './getFinancialRecordsParams'; export * from './getFundInformations200'; export * from './getFundInformationsDetails200'; From 60d2150e3ddf8f54dce0c5561e826ebff143f069 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Wed, 8 Jan 2025 23:19:13 +0900 Subject: [PATCH 03/19] =?UTF-8?q?postapi=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/drivers/db/db.go | 3 +- .../abstract/abstract_repository.go | 10 +++ .../repository/festival_item_repository.go | 77 ++++++++++++++----- .../usecase/festival_item_usecase.go | 60 ++++++++++----- 4 files changed, 110 insertions(+), 40 deletions(-) diff --git a/api/drivers/db/db.go b/api/drivers/db/db.go index 2504c587..e2aa9dc0 100644 --- a/api/drivers/db/db.go +++ b/api/drivers/db/db.go @@ -3,9 +3,10 @@ package db import ( "database/sql" "fmt" + "os" + _ "github.com/go-sql-driver/mysql" "github.com/joho/godotenv" - "os" ) type client struct { diff --git a/api/externals/repository/abstract/abstract_repository.go b/api/externals/repository/abstract/abstract_repository.go index 458cf149..790a41ee 100644 --- a/api/externals/repository/abstract/abstract_repository.go +++ b/api/externals/repository/abstract/abstract_repository.go @@ -17,6 +17,8 @@ type Crud interface { Read(context.Context, string) (*sql.Rows, error) ReadByID(context.Context, string) (*sql.Row, error) UpdateDB(context.Context, string) error + StartTransaction(context.Context) (*sql.Tx, error) + Commit(context.Context, *sql.Tx) error } func NewCrud(client db.Client) Crud { @@ -47,3 +49,11 @@ func (a abstractRepository) UpdateDB(ctx context.Context, query string) error { fmt.Printf("\x1b[36m%s\n", query) return err } + +func (a abstractRepository) StartTransaction(ctx context.Context) (*sql.Tx, error) { + return a.client.DB().BeginTx(ctx, nil) +} + +func (a abstractRepository) Commit(ctx context.Context, tx *sql.Tx) error { + return tx.Commit() +} diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 2cc9a483..3ec99b00 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -19,10 +19,14 @@ type FestivalItemRepository interface { All(context.Context) (*sql.Rows, error) AllByPeriod(context.Context, string) (*sql.Rows, error) GetById(context.Context, string) (*sql.Row, error) - Create(context.Context, generated.FestivalItem) error - Update(context.Context, string, generated.FestivalItem) error - Delete(context.Context, string) error + CreateFestivalItem(context.Context, *sql.Tx, generated.FestivalItem) error + CreateItemBudget(context.Context, *sql.Tx, generated.FestivalItem) error + UpdateFestivalItem(context.Context, string, generated.FestivalItem) error + DeleteFestivalItem(context.Context, string) error FindLatestRecord(context.Context) (*sql.Row, error) + StartTransaction(context.Context) (*sql.Tx, error) + RollBack(context.Context, *sql.Tx) + Commit(context.Context, *sql.Tx) error } func NewFestivalItemRepository(c db.Client, ac abstract.Crud) FestivalItemRepository { @@ -106,22 +110,41 @@ func (fir *festivalItemRepository) GetById( return fir.crud.ReadByID(c, query) } -// 作成 -func (fir *festivalItemRepository) Create( +// 購入物品作成 +func (fir *festivalItemRepository) CreateFestivalItem( c context.Context, + tx *sql.Tx, festivalItem generated.FestivalItem, ) error { - ds := dialect.Insert("financial_records"). - Rows(goqu.Record{"name": festivalItem.Name, "year_id": festivalItem.Memo}) + ds := dialect.Insert("festival_items"). + Rows(goqu.Record{"name": festivalItem.Name, "memo": festivalItem.Memo, "division_id": festivalItem.DivisionId}) query, _, err := ds.ToSQL() if err != nil { return err } - return fir.crud.UpdateDB(c, query) + _, err = tx.Exec(query) + + return err +} + +func (fir *festivalItemRepository) CreateItemBudget( + c context.Context, + tx *sql.Tx, + festivalItem generated.FestivalItem, +) error { + ds := dialect.Insert("item_budgets"). + Rows(goqu.Record{"amount": festivalItem.Amount, "festival_item_id": goqu.L("LAST_INSERT_ID()")}) + query, _, err := ds.ToSQL() + if err != nil { + return err + } + _, err = tx.Exec(query) + + return err } // 編集 -func (fir *festivalItemRepository) Update( +func (fir *festivalItemRepository) UpdateFestivalItem( c context.Context, id string, festivalItem generated.FestivalItem, @@ -137,7 +160,7 @@ func (fir *festivalItemRepository) Update( } // 削除 -func (fir *festivalItemRepository) Delete( +func (fir *festivalItemRepository) DeleteFestivalItem( c context.Context, id string, ) error { @@ -153,23 +176,39 @@ func (fir *festivalItemRepository) Delete( // 最新のfestivalItemを取得する func (fir *festivalItemRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { query, _, err := dialect.Select( - "financial_records.id", - "financial_records.name", "years.year", - goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), + "festival_items.id", + "festival_items.name", + "festival_items.memo", + "financial_records.name", + "divisions.name", + "item_budgets.amount", 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"). + 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("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"). - Order(goqu.I("id").Desc()). + GroupBy("festival_items.id", "item_budgets.amount"). + Order(goqu.I("festival_items.id").Desc()). Limit(1). ToSQL() + if err != nil { return nil, err } return fir.crud.ReadByID(c, query) } + +func (fir *festivalItemRepository) StartTransaction(c context.Context) (*sql.Tx, error) { + return fir.crud.StartTransaction(c) +} + +func (fir *festivalItemRepository) RollBack(c context.Context, tx *sql.Tx) { + tx.Rollback() +} + +func (fir *festivalItemRepository) Commit(c context.Context, tx *sql.Tx) error { + return tx.Commit() +} diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index b614bdab..60510c02 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -50,25 +50,45 @@ func (fiu *festivalItemUseCase) CreateFestivalItem( festivalItem generated.FestivalItem, ) (generated.FestivalItemWithBalance, error) { latastFestivalItemWithBalance := generated.FestivalItemWithBalance{} - // err := fiu.rep.Create(c, financialRecord) - // if err != nil { - // return latastFinancialRecordWithBalance, err - // } - // row, err := fiu.rep.FindLatestRecord(c) - // if err != nil { - // return latastFinancialRecordWithBalance, err - // } - // err = row.Scan( - // &latastFinancialRecordWithBalance.Id, - // &latastFinancialRecordWithBalance.Name, - // &latastFinancialRecordWithBalance.Year, - // &latastFinancialRecordWithBalance.Budget, - // &latastFinancialRecordWithBalance.Expense, - // &latastFinancialRecordWithBalance.Balance, - // ) - // if err != nil { - // return latastFinancialRecordWithBalance, err - // } + + // トランザクションスタート + tx, _ := fiu.rep.StartTransaction(c) + + if err := fiu.rep.CreateFestivalItem(c, tx, festivalItem); err != nil { + // エラーが発生時はロールバック + fiu.rep.RollBack(c, tx) + return latastFestivalItemWithBalance, err + } + + if err := fiu.rep.CreateItemBudget(c, tx, festivalItem); err != nil { + // エラーが発生時はロールバック + fiu.rep.RollBack(c, tx) + return latastFestivalItemWithBalance, err + } + + // コミットしてトランザクション終了 + if err := fiu.rep.Commit(c, tx); err != nil { + return latastFestivalItemWithBalance, err + } + + row, err := fiu.rep.FindLatestRecord(c) + if err != nil { + return latastFestivalItemWithBalance, err + } + err = row.Scan( + &latastFestivalItemWithBalance.Id, + &latastFestivalItemWithBalance.Name, + &latastFestivalItemWithBalance.Memo, + &latastFestivalItemWithBalance.FinancialRecord, + &latastFestivalItemWithBalance.Division, + &latastFestivalItemWithBalance.Budget, + &latastFestivalItemWithBalance.Expense, + &latastFestivalItemWithBalance.Balance, + ) + if err != nil { + return latastFestivalItemWithBalance, err + } + return latastFestivalItemWithBalance, nil } @@ -103,6 +123,6 @@ func (fiu *festivalItemUseCase) UpdateFestivalItem( } func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string) error { - err := fiu.rep.Delete(c, id) + err := fiu.rep.DeleteFestivalItem(c, id) return err } From 35125b33910be8efbdb133cd278ba7353b9dcc2f Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Wed, 8 Jan 2025 23:34:32 +0900 Subject: [PATCH 04/19] =?UTF-8?q?put=E3=81=AEapi=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 55 ++++++++++++---- .../usecase/festival_item_usecase.go | 64 ++++++++++++------- 2 files changed, 82 insertions(+), 37 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 3ec99b00..9e2c7c3b 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -3,6 +3,7 @@ package repository import ( "context" "database/sql" + "fmt" "github.com/NUTFes/FinanSu/api/drivers/db" "github.com/NUTFes/FinanSu/api/externals/repository/abstract" @@ -21,7 +22,8 @@ type FestivalItemRepository interface { GetById(context.Context, string) (*sql.Row, error) CreateFestivalItem(context.Context, *sql.Tx, generated.FestivalItem) error CreateItemBudget(context.Context, *sql.Tx, generated.FestivalItem) error - UpdateFestivalItem(context.Context, string, generated.FestivalItem) error + UpdateFestivalItem(context.Context, *sql.Tx, string, generated.FestivalItem) error + UpdateItemBudget(context.Context, *sql.Tx, string, generated.FestivalItem) error DeleteFestivalItem(context.Context, string) error FindLatestRecord(context.Context) (*sql.Row, error) StartTransaction(context.Context) (*sql.Tx, error) @@ -90,20 +92,24 @@ func (fir *festivalItemRepository) GetById( id string, ) (*sql.Row, error) { query, _, err := dialect.Select( - "financial_records.id", - "financial_records.name", "years.year", - goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), + "festival_items.id", + "festival_items.name", + "festival_items.memo", + "financial_records.name", + "divisions.name", + "item_budgets.amount", 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"). + 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("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{"financial_records.id": id}). + GroupBy("festival_items.id", "item_budgets.amount"). + Where(goqu.Ex{"festival_items.id": id}). ToSQL() + if err != nil { return nil, err } @@ -143,20 +149,41 @@ func (fir *festivalItemRepository) CreateItemBudget( return err } -// 編集 +// festivalItem編集 func (fir *festivalItemRepository) UpdateFestivalItem( c context.Context, + tx *sql.Tx, id string, festivalItem generated.FestivalItem, ) error { - ds := dialect.Update("financial_records"). - Set(goqu.Record{"name": festivalItem.Name, "year_id": festivalItem.Memo}). + ds := dialect.Update("festival_items"). + Set(goqu.Record{"name": festivalItem.Name, "memo": festivalItem.Memo, "division_id": festivalItem.DivisionId}). Where(goqu.Ex{"id": id}) query, _, err := ds.ToSQL() if err != nil { return err } - return fir.crud.UpdateDB(c, query) + _, err = tx.Exec(query) + return err +} + +// itemBudget編集 +func (fir *festivalItemRepository) UpdateItemBudget( + c context.Context, + tx *sql.Tx, + id string, + festivalItem generated.FestivalItem, +) error { + ds := dialect.Update("item_budgets"). + Set(goqu.Record{"amount": festivalItem.Amount}). + Where(goqu.Ex{"festival_item_id": id}) + query, _, err := ds.ToSQL() + fmt.Println(query) + if err != nil { + return err + } + _, err = tx.Exec(query) + return err } // 削除 diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 60510c02..61e066ba 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -97,29 +97,47 @@ func (fiu *festivalItemUseCase) UpdateFestivalItem( id string, festivalItem generated.FestivalItem, ) (generated.FestivalItemWithBalance, error) { - updateFestivalItem := generated.FestivalItemWithBalance{} - - // if err := fiu.rep.Update(c, id, festivalItem); err != nil { - // return updateFestivalItem, err - // } - - // row, err := fiu.rep.GetById(c, id) - // if err != nil { - // return updateFestivalItem, err - // } - - // if err = row.Scan( - // &updateFinancialRecord.Id, - // &updateFinancialRecord.Name, - // &updateFinancialRecord.Year, - // &updateFinancialRecord.Budget, - // &updateFinancialRecord.Expense, - // &updateFinancialRecord.Balance, - // ); err != nil { - // return updateFinancialRecord, err - // } - - return updateFestivalItem, nil + updateFestivalItemWithBalance := generated.FestivalItemWithBalance{} + + // トランザクションスタート + tx, _ := fiu.rep.StartTransaction(c) + + if err := fiu.rep.UpdateFestivalItem(c, tx, id, festivalItem); err != nil { + // エラーが発生時はロールバック + fiu.rep.RollBack(c, tx) + return updateFestivalItemWithBalance, err + } + + if err := fiu.rep.UpdateItemBudget(c, tx, id, festivalItem); err != nil { + // エラーが発生時はロールバック + fiu.rep.RollBack(c, tx) + return updateFestivalItemWithBalance, err + } + + // コミットしてトランザクション終了 + if err := fiu.rep.Commit(c, tx); err != nil { + return updateFestivalItemWithBalance, err + } + + row, err := fiu.rep.GetById(c, id) + if err != nil { + return updateFestivalItemWithBalance, err + } + err = row.Scan( + &updateFestivalItemWithBalance.Id, + &updateFestivalItemWithBalance.Name, + &updateFestivalItemWithBalance.Memo, + &updateFestivalItemWithBalance.FinancialRecord, + &updateFestivalItemWithBalance.Division, + &updateFestivalItemWithBalance.Budget, + &updateFestivalItemWithBalance.Expense, + &updateFestivalItemWithBalance.Balance, + ) + if err != nil { + return updateFestivalItemWithBalance, err + } + + return updateFestivalItemWithBalance, nil } func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string) error { From 3ca385395993bc9f0fd3309cbb396a72450809ff Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Wed, 8 Jan 2025 23:45:27 +0900 Subject: [PATCH 05/19] =?UTF-8?q?delete=E3=81=AEapi=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 26 ++++++++++++++++--- .../usecase/festival_item_usecase.go | 24 +++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 9e2c7c3b..32489409 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -24,7 +24,8 @@ type FestivalItemRepository interface { CreateItemBudget(context.Context, *sql.Tx, generated.FestivalItem) error UpdateFestivalItem(context.Context, *sql.Tx, string, generated.FestivalItem) error UpdateItemBudget(context.Context, *sql.Tx, string, generated.FestivalItem) error - DeleteFestivalItem(context.Context, string) error + DeleteFestivalItem(context.Context, *sql.Tx, string) error + DeleteItemBudget(context.Context, *sql.Tx, string) error FindLatestRecord(context.Context) (*sql.Row, error) StartTransaction(context.Context) (*sql.Tx, error) RollBack(context.Context, *sql.Tx) @@ -186,18 +187,35 @@ func (fir *festivalItemRepository) UpdateItemBudget( return err } -// 削除 +// 購入物品削除 func (fir *festivalItemRepository) DeleteFestivalItem( c context.Context, + tx *sql.Tx, id string, ) error { - ds := dialect.Delete("financial_records").Where(goqu.Ex{"id": id}) + ds := dialect.Delete("festival_items").Where(goqu.Ex{"id": id}) query, _, err := ds.ToSQL() if err != nil { return err } - return fir.crud.UpdateDB(c, query) + fmt.Println(query) + _, err = tx.Exec(query) + return err +} +// 予算削除 +func (fir *festivalItemRepository) DeleteItemBudget( + c context.Context, + tx *sql.Tx, + id string, +) error { + ds := dialect.Delete("item_budgets").Where(goqu.Ex{"festival_item_id": id}) + query, _, err := ds.ToSQL() + if err != nil { + return err + } + _, err = tx.Exec(query) + return err } // 最新のfestivalItemを取得する diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 61e066ba..762e7d08 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -141,6 +141,26 @@ func (fiu *festivalItemUseCase) UpdateFestivalItem( } func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string) error { - err := fiu.rep.DeleteFestivalItem(c, id) - return err + // トランザクションスタート + tx, _ := fiu.rep.StartTransaction(c) + + // 先に紐づく予算を削除 + err := fiu.rep.DeleteItemBudget(c, tx, id) + if err != nil { + fiu.rep.RollBack(c, tx) + } + + // 購入物品を削除 + err = fiu.rep.DeleteFestivalItem(c, tx, id) + if err != nil { + fiu.rep.RollBack(c, tx) + return err + } + + // コミットしてトランザクション終了 + if err = fiu.rep.Commit(c, tx); err != nil { + return err + } + + return nil } From b4eab42c442ba7fa6f3d4043c187f73a5603cc21 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Thu, 9 Jan 2025 00:03:57 +0900 Subject: [PATCH 06/19] =?UTF-8?q?get=E3=81=AEapi=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 41 +++++---- .../usecase/festival_item_usecase.go | 90 +++++++++++++++++++ 2 files changed, 114 insertions(+), 17 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 32489409..230807b6 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -41,20 +41,23 @@ func (fir *festivalItemRepository) All( c context.Context, ) (*sql.Rows, error) { query, _, err := dialect.Select( - "financial_records.id", - "financial_records.name", "years.year", - goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), + "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("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). - From("financial_records"). + 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("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"). + GroupBy("festival_items.id", "item_budgets.amount"). + Order(goqu.I("festival_items.id").Desc()). ToSQL() - if err != nil { return nil, err } @@ -67,18 +70,22 @@ func (fir *festivalItemRepository) AllByPeriod( year string, ) (*sql.Rows, error) { query, _, err := dialect.Select( - "financial_records.id", - "financial_records.name", "years.year", - goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), + "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("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). - From("financial_records"). + 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("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"). + GroupBy("festival_items.id", "item_budgets.amount"). + Order(goqu.I("festival_items.id").Desc()). Where(goqu.Ex{"years.year": year}). ToSQL() if err != nil { diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 762e7d08..8db570f7 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -34,6 +34,51 @@ func (fiu *festivalItemUseCase) GetFestivalItems( c context.Context, ) (generated.FestivalItemDetails, error) { var festivalItemDetails generated.FestivalItemDetails + var festivalItems []generated.FestivalItemWithBalance + + rows, err := fiu.rep.All(c) + 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 } @@ -42,6 +87,51 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYears( year string, ) (generated.FestivalItemDetails, error) { var festivalItemDetails generated.FestivalItemDetails + var festivalItems []generated.FestivalItemWithBalance + + rows, err := fiu.rep.AllByPeriod(c, year) + 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 } From 1918da32b8ce037ebc2b811747da8baeb95389a3 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Thu, 9 Jan 2025 00:18:21 +0900 Subject: [PATCH 07/19] =?UTF-8?q?get=E3=81=ABdivision=5Fid=E3=81=AE?= =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=82=BF=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/festival_item_controller.go | 9 +++ .../repository/festival_item_repository.go | 34 ++++++++++++ api/generated/openapi_gen.go | 10 ++++ .../usecase/festival_item_usecase.go | 55 +++++++++++++++++++ openapi/openapi.yaml | 5 ++ 5 files changed, 113 insertions(+) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 8906d0d6..5357b16d 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -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 { diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 230807b6..63a216b2 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -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 @@ -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, diff --git a/api/generated/openapi_gen.go b/api/generated/openapi_gen.go index 076f7f7e..4133a930 100644 --- a/api/generated/openapi_gen.go +++ b/api/generated/openapi_gen.go @@ -280,6 +280,9 @@ type PutExpensesIdParams struct { type GetFestivalItemsParams struct { // Year year Year *int `form:"year,omitempty" json:"year,omitempty"` + + // DivisionId division_id + DivisionId *int `form:"division_id,omitempty" json:"division_id,omitempty"` } // GetFinancialRecordsParams defines parameters for GetFinancialRecords. @@ -1685,6 +1688,13 @@ func (w *ServerInterfaceWrapper) GetFestivalItems(ctx echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter year: %s", err)) } + // ------------- Optional query parameter "division_id" ------------- + + err = runtime.BindQueryParameter("form", true, false, "division_id", ctx.QueryParams(), ¶ms.DivisionId) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter division_id: %s", err)) + } + // Invoke the callback with all the unmarshaled arguments err = w.Handler.GetFestivalItems(ctx, params) return err diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 8db570f7..563a5b35 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -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, @@ -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, diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index 90414c7f..527082d1 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -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の一覧を取得 From 767b068ba6bb0d1266094991a5380893181bd0df Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Thu, 9 Jan 2025 00:43:00 +0900 Subject: [PATCH 08/19] =?UTF-8?q?=E5=9E=8B=E5=90=8D=E7=9C=81=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 17 +++---- .../usecase/financial_record_usecase.go | 47 ++++++++++--------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 63a216b2..43edac5d 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -16,15 +16,16 @@ type festivalItemRepository struct { crud abstract.Crud } +type FestivalItem = generated.FestivalItem 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 - UpdateFestivalItem(context.Context, *sql.Tx, string, generated.FestivalItem) error - UpdateItemBudget(context.Context, *sql.Tx, string, generated.FestivalItem) error + CreateFestivalItem(context.Context, *sql.Tx, FestivalItem) error + CreateItemBudget(context.Context, *sql.Tx, FestivalItem) error + UpdateFestivalItem(context.Context, *sql.Tx, string, FestivalItem) error + UpdateItemBudget(context.Context, *sql.Tx, string, FestivalItem) error DeleteFestivalItem(context.Context, *sql.Tx, string) error DeleteItemBudget(context.Context, *sql.Tx, string) error FindLatestRecord(context.Context) (*sql.Row, error) @@ -162,7 +163,7 @@ func (fir *festivalItemRepository) GetById( func (fir *festivalItemRepository) CreateFestivalItem( c context.Context, tx *sql.Tx, - festivalItem generated.FestivalItem, + festivalItem FestivalItem, ) error { ds := dialect.Insert("festival_items"). Rows(goqu.Record{"name": festivalItem.Name, "memo": festivalItem.Memo, "division_id": festivalItem.DivisionId}) @@ -178,7 +179,7 @@ func (fir *festivalItemRepository) CreateFestivalItem( func (fir *festivalItemRepository) CreateItemBudget( c context.Context, tx *sql.Tx, - festivalItem generated.FestivalItem, + festivalItem FestivalItem, ) error { ds := dialect.Insert("item_budgets"). Rows(goqu.Record{"amount": festivalItem.Amount, "festival_item_id": goqu.L("LAST_INSERT_ID()")}) @@ -196,7 +197,7 @@ func (fir *festivalItemRepository) UpdateFestivalItem( c context.Context, tx *sql.Tx, id string, - festivalItem generated.FestivalItem, + festivalItem FestivalItem, ) error { ds := dialect.Update("festival_items"). Set(goqu.Record{"name": festivalItem.Name, "memo": festivalItem.Memo, "division_id": festivalItem.DivisionId}). @@ -214,7 +215,7 @@ func (fir *festivalItemRepository) UpdateItemBudget( c context.Context, tx *sql.Tx, id string, - festivalItem generated.FestivalItem, + festivalItem FestivalItem, ) error { ds := dialect.Update("item_budgets"). Set(goqu.Record{"amount": festivalItem.Amount}). diff --git a/api/internals/usecase/financial_record_usecase.go b/api/internals/usecase/financial_record_usecase.go index d470483e..25d0c6af 100644 --- a/api/internals/usecase/financial_record_usecase.go +++ b/api/internals/usecase/financial_record_usecase.go @@ -12,18 +12,23 @@ type financialRecordUseCase struct { rep rep.FinancialRecordRepository } +type FinancialRecordDetails = generated.FinancialRecordDetails +type FinancialRecord = generated.FinancialRecord +type FinancialRecordWithBalance = generated.FinancialRecordWithBalance +type Total = generated.Total + type FinancialRecordUseCase interface { - GetFinancialRecords(context.Context) (generated.FinancialRecordDetails, error) - GetFinancialRecordsByYears(context.Context, string) (generated.FinancialRecordDetails, error) + GetFinancialRecords(context.Context) (FinancialRecordDetails, error) + GetFinancialRecordsByYears(context.Context, string) (FinancialRecordDetails, error) CreateFinancialRecord( context.Context, - generated.FinancialRecord, - ) (generated.FinancialRecordWithBalance, error) + FinancialRecord, + ) (FinancialRecordWithBalance, error) UpdateFinancialRecord( context.Context, string, - generated.FinancialRecord, - ) (generated.FinancialRecordWithBalance, error) + FinancialRecord, + ) (FinancialRecordWithBalance, error) DestroyFinancialRecord(context.Context, string) error } @@ -33,10 +38,10 @@ func NewFinancialRecordUseCase(rep rep.FinancialRecordRepository) FinancialRecor func (fru *financialRecordUseCase) GetFinancialRecords( c context.Context, -) (generated.FinancialRecordDetails, error) { - var financialRecordDetails generated.FinancialRecordDetails - var financialRecordList []generated.FinancialRecordWithBalance - var total generated.Total +) (FinancialRecordDetails, error) { + var financialRecordDetails FinancialRecordDetails + var financialRecordList []FinancialRecordWithBalance + var total Total rows, err := fru.rep.All(c) if err != nil { @@ -46,7 +51,7 @@ func (fru *financialRecordUseCase) GetFinancialRecords( defer rows.Close() for rows.Next() { - var financialRecord generated.FinancialRecordWithBalance + var financialRecord FinancialRecordWithBalance err := rows.Scan( &financialRecord.Id, &financialRecord.Name, @@ -87,9 +92,9 @@ func (fru *financialRecordUseCase) GetFinancialRecordsByYears( c context.Context, year string, ) (generated.FinancialRecordDetails, error) { - var financialRecordDetails generated.FinancialRecordDetails - var financialRecordList []generated.FinancialRecordWithBalance - var total generated.Total + var financialRecordDetails FinancialRecordDetails + var financialRecordList []FinancialRecordWithBalance + var total Total rows, err := fru.rep.AllByPeriod(c, year) if err != nil { @@ -99,7 +104,7 @@ func (fru *financialRecordUseCase) GetFinancialRecordsByYears( defer rows.Close() for rows.Next() { - var financialRecord generated.FinancialRecordWithBalance + var financialRecord FinancialRecordWithBalance err := rows.Scan( &financialRecord.Id, &financialRecord.Name, @@ -138,9 +143,9 @@ func (fru *financialRecordUseCase) GetFinancialRecordsByYears( func (fru *financialRecordUseCase) CreateFinancialRecord( c context.Context, - financialRecord generated.FinancialRecord, -) (generated.FinancialRecordWithBalance, error) { - latastFinancialRecordWithBalance := generated.FinancialRecordWithBalance{} + financialRecord FinancialRecord, +) (FinancialRecordWithBalance, error) { + latastFinancialRecordWithBalance := FinancialRecordWithBalance{} err := fru.rep.Create(c, financialRecord) if err != nil { return latastFinancialRecordWithBalance, err @@ -166,9 +171,9 @@ func (fru *financialRecordUseCase) CreateFinancialRecord( func (fru *financialRecordUseCase) UpdateFinancialRecord( c context.Context, id string, - financialRecord generated.FinancialRecord, -) (generated.FinancialRecordWithBalance, error) { - updateFinancialRecord := generated.FinancialRecordWithBalance{} + financialRecord FinancialRecord, +) (FinancialRecordWithBalance, error) { + updateFinancialRecord := FinancialRecordWithBalance{} if err := fru.rep.Update(c, id, financialRecord); err != nil { return updateFinancialRecord, err From 164daa4c186e4924830f4fc1de5503bbbf6376b0 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 16:54:16 +0900 Subject: [PATCH 09/19] =?UTF-8?q?=E5=9E=8B=E3=81=AE=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usecase/festival_item_usecase.go | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 563a5b35..82f30d30 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -7,23 +7,27 @@ import ( "github.com/NUTFes/FinanSu/api/generated" ) +type FestivalItemDetails = generated.FestivalItemDetails +type FestivalItem = generated.FestivalItem +type FestivalItemWithBalance = generated.FestivalItemWithBalance + type festivalItemUseCase struct { rep rep.FestivalItemRepository } type FestivalItemUseCase interface { - GetFestivalItems(context.Context) (generated.FestivalItemDetails, error) - GetFestivalItemsByYears(context.Context, string) (generated.FestivalItemDetails, error) - GetFestivalItemsByYearsAndDivision(context.Context, string, string) (generated.FestivalItemDetails, error) + GetFestivalItems(context.Context) (FestivalItemDetails, error) + GetFestivalItemsByYears(context.Context, string) (FestivalItemDetails, error) + GetFestivalItemsByYearsAndDivision(context.Context, string, string) (FestivalItemDetails, error) CreateFestivalItem( context.Context, - generated.FestivalItem, - ) (generated.FestivalItemWithBalance, error) + FestivalItem, + ) (FestivalItemWithBalance, error) UpdateFestivalItem( context.Context, string, - generated.FestivalItem, - ) (generated.FestivalItemWithBalance, error) + FestivalItem, + ) (FestivalItemWithBalance, error) DestroyFestivalItem(context.Context, string) error } @@ -33,9 +37,9 @@ func NewFestivalItemUseCase(rep rep.FestivalItemRepository) FestivalItemUseCase func (fiu *festivalItemUseCase) GetFestivalItems( c context.Context, -) (generated.FestivalItemDetails, error) { - var festivalItemDetails generated.FestivalItemDetails - var festivalItems []generated.FestivalItemWithBalance +) (FestivalItemDetails, error) { + var festivalItemDetails FestivalItemDetails + var festivalItems []FestivalItemWithBalance rows, err := fiu.rep.All(c) if err != nil { @@ -44,7 +48,7 @@ func (fiu *festivalItemUseCase) GetFestivalItems( defer rows.Close() for rows.Next() { - var festivalItem generated.FestivalItemWithBalance + var festivalItem FestivalItemWithBalance err := rows.Scan( &festivalItem.Id, &festivalItem.Name, @@ -62,7 +66,7 @@ func (fiu *festivalItemUseCase) GetFestivalItems( } festivalItemDetails.FestivalItems = &festivalItems - var total generated.Total + var total Total // totalを求める budgetTotal := 0 @@ -86,9 +90,9 @@ func (fiu *festivalItemUseCase) GetFestivalItems( func (fiu *festivalItemUseCase) GetFestivalItemsByYears( c context.Context, year string, -) (generated.FestivalItemDetails, error) { - var festivalItemDetails generated.FestivalItemDetails - var festivalItems []generated.FestivalItemWithBalance +) (FestivalItemDetails, error) { + var festivalItemDetails FestivalItemDetails + var festivalItems []FestivalItemWithBalance rows, err := fiu.rep.AllByPeriod(c, year) if err != nil { @@ -97,7 +101,7 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYears( defer rows.Close() for rows.Next() { - var festivalItem generated.FestivalItemWithBalance + var festivalItem FestivalItemWithBalance err := rows.Scan( &festivalItem.Id, &festivalItem.Name, @@ -115,7 +119,7 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYears( } festivalItemDetails.FestivalItems = &festivalItems - var total generated.Total + var total Total // totalを求める budgetTotal := 0 @@ -140,9 +144,9 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision( c context.Context, year string, divisionId string, -) (generated.FestivalItemDetails, error) { - var festivalItemDetails generated.FestivalItemDetails - var festivalItems []generated.FestivalItemWithBalance +) (FestivalItemDetails, error) { + var festivalItemDetails FestivalItemDetails + var festivalItems []FestivalItemWithBalance rows, err := fiu.rep.AllByPeriodAndDivision(c, year, divisionId) if err != nil { @@ -151,7 +155,7 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision( defer rows.Close() for rows.Next() { - var festivalItem generated.FestivalItemWithBalance + var festivalItem FestivalItemWithBalance err := rows.Scan( &festivalItem.Id, &festivalItem.Name, @@ -169,7 +173,7 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision( } festivalItemDetails.FestivalItems = &festivalItems - var total generated.Total + var total Total // totalを求める budgetTotal := 0 @@ -192,9 +196,9 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision( func (fiu *festivalItemUseCase) CreateFestivalItem( c context.Context, - festivalItem generated.FestivalItem, -) (generated.FestivalItemWithBalance, error) { - latastFestivalItemWithBalance := generated.FestivalItemWithBalance{} + festivalItem FestivalItem, +) (FestivalItemWithBalance, error) { + latastFestivalItemWithBalance := FestivalItemWithBalance{} // トランザクションスタート tx, _ := fiu.rep.StartTransaction(c) @@ -240,9 +244,9 @@ func (fiu *festivalItemUseCase) CreateFestivalItem( func (fiu *festivalItemUseCase) UpdateFestivalItem( c context.Context, id string, - festivalItem generated.FestivalItem, -) (generated.FestivalItemWithBalance, error) { - updateFestivalItemWithBalance := generated.FestivalItemWithBalance{} + festivalItem FestivalItem, +) (FestivalItemWithBalance, error) { + updateFestivalItemWithBalance := FestivalItemWithBalance{} // トランザクションスタート tx, _ := fiu.rep.StartTransaction(c) From 4ae6128b4d53384465a0381e20d02d8885d075a2 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:14:00 +0900 Subject: [PATCH 10/19] =?UTF-8?q?=E5=9E=8B=E3=81=AE=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/externals/controller/festival_item_controller.go | 9 ++++++--- .../controller/financial_record_controller.go | 9 ++++++--- api/internals/usecase/festival_item_usecase.go | 8 ++++---- api/internals/usecase/financial_record_usecase.go | 10 +++++----- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 5357b16d..0dfa25c7 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -26,7 +26,7 @@ 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 festivalItemDetails FestivalItemDetails var err error if year != "" && division_id != "" { @@ -53,7 +53,7 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { } func (f *festivalItemController) CreateFestivalItem(c echo.Context) error { - festivalItem := new(generated.FestivalItem) + festivalItem := new(FestivalItem) if err := c.Bind(festivalItem); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } @@ -66,7 +66,7 @@ func (f *festivalItemController) CreateFestivalItem(c echo.Context) error { func (f *festivalItemController) UpdateFestivalItem(c echo.Context) error { id := c.Param("id") - festivalItem := new(generated.FestivalItem) + festivalItem := new(FestivalItem) if err := c.Bind(festivalItem); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } @@ -89,3 +89,6 @@ func (f *festivalItemController) DestroyFestivalItem(c echo.Context) error { } return c.String(http.StatusOK, "Destroy Festival Item") } + +type FestivalItemDetails = generated.FestivalItemDetails +type FestivalItem = generated.FestivalItem diff --git a/api/externals/controller/financial_record_controller.go b/api/externals/controller/financial_record_controller.go index 4c0ba11c..d8454d1a 100644 --- a/api/externals/controller/financial_record_controller.go +++ b/api/externals/controller/financial_record_controller.go @@ -25,7 +25,7 @@ func NewFinancialRecordController(u usecase.FinancialRecordUseCase) FinancialRec func (f *financialRecordController) IndexFinancialRecords(c echo.Context) error { year := c.QueryParam("year") - var financialRecordDetails generated.FinancialRecordDetails + var financialRecordDetails FinancialRecordDetails var err error if year != "" { @@ -44,7 +44,7 @@ func (f *financialRecordController) IndexFinancialRecords(c echo.Context) error } func (f *financialRecordController) CreateFinancialRecord(c echo.Context) error { - financialRecord := new(generated.FinancialRecord) + financialRecord := new(FinancialRecord) if err := c.Bind(financialRecord); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } @@ -57,7 +57,7 @@ func (f *financialRecordController) CreateFinancialRecord(c echo.Context) error func (f *financialRecordController) UpdateFinancialRecord(c echo.Context) error { id := c.Param("id") - financialRecord := new(generated.FinancialRecord) + financialRecord := new(FinancialRecord) if err := c.Bind(financialRecord); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } @@ -80,3 +80,6 @@ func (f *financialRecordController) DestroyFinancialRecord(c echo.Context) error } return c.String(http.StatusOK, "Destroy FinancialRecord") } + +type FinancialRecordDetails = generated.FinancialRecordDetails +type FinancialRecord = generated.FinancialRecord diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 82f30d30..6a2b6ae0 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -7,10 +7,6 @@ import ( "github.com/NUTFes/FinanSu/api/generated" ) -type FestivalItemDetails = generated.FestivalItemDetails -type FestivalItem = generated.FestivalItem -type FestivalItemWithBalance = generated.FestivalItemWithBalance - type festivalItemUseCase struct { rep rep.FestivalItemRepository } @@ -313,3 +309,7 @@ func (fiu *festivalItemUseCase) DestroyFestivalItem(c context.Context, id string return nil } + +type FestivalItemDetails = generated.FestivalItemDetails +type FestivalItem = generated.FestivalItem +type FestivalItemWithBalance = generated.FestivalItemWithBalance diff --git a/api/internals/usecase/financial_record_usecase.go b/api/internals/usecase/financial_record_usecase.go index 3732732b..0af3eabf 100644 --- a/api/internals/usecase/financial_record_usecase.go +++ b/api/internals/usecase/financial_record_usecase.go @@ -12,11 +12,6 @@ type financialRecordUseCase struct { rep rep.FinancialRecordRepository } -type FinancialRecordDetails = generated.FinancialRecordDetails -type FinancialRecord = generated.FinancialRecord -type FinancialRecordWithBalance = generated.FinancialRecordWithBalance -type Total = generated.Total - type FinancialRecordUseCase interface { GetFinancialRecords(context.Context) (FinancialRecordDetails, error) GetFinancialRecordsByYears(context.Context, string) (FinancialRecordDetails, error) @@ -202,3 +197,8 @@ func (fru *financialRecordUseCase) DestroyFinancialRecord(c context.Context, id err := fru.rep.Delete(c, id) return err } + +type FinancialRecordDetails = generated.FinancialRecordDetails +type FinancialRecord = generated.FinancialRecord +type FinancialRecordWithBalance = generated.FinancialRecordWithBalance +type Total = generated.Total From 4140b8de8999ff368c8cc7b7180903f13392f303 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:14:57 +0900 Subject: [PATCH 11/19] =?UTF-8?q?=E3=82=AD=E3=83=A3=E3=83=A1=E3=83=AB?= =?UTF-8?q?=E3=82=B1=E3=83=BC=E3=82=B9=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/externals/controller/festival_item_controller.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 0dfa25c7..e31b418d 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -25,12 +25,12 @@ func NewFestivalItemController(u usecase.FestivalItemUseCase) FestivalItemContro func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { year := c.QueryParam("year") - division_id := c.QueryParam("division_id") + divisionId := c.QueryParam("division_id") var festivalItemDetails FestivalItemDetails var err error - if year != "" && division_id != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYearsAndDivision(c.Request().Context(), year, division_id) + if year != "" && divisionId != "" { + festivalItemDetails, err = f.u.GetFestivalItemsByYearsAndDivision(c.Request().Context(), year, divisionId) if err != nil { return err } From 36ec9bc0d76b1720b3fb07e73705f0a5a0663d7c Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:16:16 +0900 Subject: [PATCH 12/19] =?UTF-8?q?=E5=B9=B4=E5=BA=A6=E3=81=A7=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/externals/controller/festival_item_controller.go | 4 ++-- api/internals/usecase/festival_item_usecase.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index e31b418d..153bec0c 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -30,7 +30,7 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { var err error if year != "" && divisionId != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYearsAndDivision(c.Request().Context(), year, divisionId) + festivalItemDetails, err = f.u.GetFestivalItemsByYearAndDivision(c.Request().Context(), year, divisionId) if err != nil { return err } @@ -38,7 +38,7 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { } if year != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYears(c.Request().Context(), year) + festivalItemDetails, err = f.u.GetFestivalItemsByYear(c.Request().Context(), year) if err != nil { return err } diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 6a2b6ae0..a3ec8317 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -13,8 +13,8 @@ type festivalItemUseCase struct { type FestivalItemUseCase interface { GetFestivalItems(context.Context) (FestivalItemDetails, error) - GetFestivalItemsByYears(context.Context, string) (FestivalItemDetails, error) - GetFestivalItemsByYearsAndDivision(context.Context, string, string) (FestivalItemDetails, error) + GetFestivalItemsByYear(context.Context, string) (FestivalItemDetails, error) + GetFestivalItemsByYearAndDivision(context.Context, string, string) (FestivalItemDetails, error) CreateFestivalItem( context.Context, FestivalItem, @@ -83,7 +83,7 @@ func (fiu *festivalItemUseCase) GetFestivalItems( return festivalItemDetails, nil } -func (fiu *festivalItemUseCase) GetFestivalItemsByYears( +func (fiu *festivalItemUseCase) GetFestivalItemsByYear( c context.Context, year string, ) (FestivalItemDetails, error) { @@ -136,7 +136,7 @@ func (fiu *festivalItemUseCase) GetFestivalItemsByYears( return festivalItemDetails, nil } -func (fiu *festivalItemUseCase) GetFestivalItemsByYearsAndDivision( +func (fiu *festivalItemUseCase) GetFestivalItemsByYearAndDivision( c context.Context, year string, divisionId string, From 725d9057c0a166a4aae78a2fdc79b25f78ba5258 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:18:49 +0900 Subject: [PATCH 13/19] =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=86=E3=82=AD?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E5=A4=89=E6=95=B0=E3=81=A7=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/externals/controller/festival_item_controller.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 153bec0c..2085c6e8 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -24,13 +24,14 @@ func NewFestivalItemController(u usecase.FestivalItemUseCase) FestivalItemContro } func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { + ctx := c.Request().Context() year := c.QueryParam("year") divisionId := c.QueryParam("division_id") var festivalItemDetails FestivalItemDetails var err error if year != "" && divisionId != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYearAndDivision(c.Request().Context(), year, divisionId) + festivalItemDetails, err = f.u.GetFestivalItemsByYearAndDivision(ctx, year, divisionId) if err != nil { return err } @@ -38,14 +39,14 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { } if year != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYear(c.Request().Context(), year) + festivalItemDetails, err = f.u.GetFestivalItemsByYear(ctx, year) if err != nil { return err } return c.JSON(http.StatusOK, festivalItemDetails) } - festivalItemDetails, err = f.u.GetFestivalItems(c.Request().Context()) + festivalItemDetails, err = f.u.GetFestivalItems(ctx) if err != nil { return err } From ab82295c30a689017bceed661ff8a008e1276aa7 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:31:08 +0900 Subject: [PATCH 14/19] =?UTF-8?q?sql=E3=82=92=E3=83=91=E3=83=A9=E3=83=A1?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=81=AB=E3=82=88=E3=81=A3=E3=81=A6=E6=A7=8B?= =?UTF-8?q?=E7=AF=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/festival_item_controller.go | 19 +-- .../repository/festival_item_repository.go | 18 ++- .../usecase/festival_item_usecase.go | 109 +----------------- 3 files changed, 15 insertions(+), 131 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 2085c6e8..68512aea 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -28,25 +28,8 @@ func (f *festivalItemController) IndexFestivalItems(c echo.Context) error { year := c.QueryParam("year") divisionId := c.QueryParam("division_id") var festivalItemDetails FestivalItemDetails - var err error - if year != "" && divisionId != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYearAndDivision(ctx, year, divisionId) - if err != nil { - return err - } - return c.JSON(http.StatusOK, festivalItemDetails) - } - - if year != "" { - festivalItemDetails, err = f.u.GetFestivalItemsByYear(ctx, year) - if err != nil { - return err - } - return c.JSON(http.StatusOK, festivalItemDetails) - } - - festivalItemDetails, err = f.u.GetFestivalItems(ctx) + festivalItemDetails, err := f.u.GetFestivalItems(ctx, year, divisionId) if err != nil { return err } diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 43edac5d..ee31ffa7 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -102,7 +102,7 @@ func (fir *festivalItemRepository) AllByPeriodAndDivision( year string, divisionId string, ) (*sql.Rows, error) { - query, _, err := dialect.Select( + ds := dialect.Select( "festival_items.id", "festival_items.name", "festival_items.memo", @@ -118,14 +118,22 @@ func (fir *festivalItemRepository) AllByPeriodAndDivision( 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() + Order(goqu.I("festival_items.id").Desc()) + + if divisionId != "" { + ds = ds.Where(goqu.Ex{"divisions.id": divisionId}) + } + + 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) } diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index a3ec8317..8e1ee827 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -12,9 +12,7 @@ type festivalItemUseCase struct { } type FestivalItemUseCase interface { - GetFestivalItems(context.Context) (FestivalItemDetails, error) - GetFestivalItemsByYear(context.Context, string) (FestivalItemDetails, error) - GetFestivalItemsByYearAndDivision(context.Context, string, string) (FestivalItemDetails, error) + GetFestivalItems(context.Context, string, string) (FestivalItemDetails, error) CreateFestivalItem( context.Context, FestivalItem, @@ -33,111 +31,6 @@ func NewFestivalItemUseCase(rep rep.FestivalItemRepository) FestivalItemUseCase func (fiu *festivalItemUseCase) GetFestivalItems( c context.Context, -) (FestivalItemDetails, error) { - var festivalItemDetails FestivalItemDetails - var festivalItems []FestivalItemWithBalance - - rows, err := fiu.rep.All(c) - if err != nil { - return festivalItemDetails, err - } - - defer rows.Close() - for rows.Next() { - var festivalItem 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 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) GetFestivalItemsByYear( - c context.Context, - year string, -) (FestivalItemDetails, error) { - var festivalItemDetails FestivalItemDetails - var festivalItems []FestivalItemWithBalance - - rows, err := fiu.rep.AllByPeriod(c, year) - if err != nil { - return festivalItemDetails, err - } - - defer rows.Close() - for rows.Next() { - var festivalItem 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 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) GetFestivalItemsByYearAndDivision( - c context.Context, year string, divisionId string, ) (FestivalItemDetails, error) { From 533f0763dead20ed63d55151092a99dc022a274a Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 12 Jan 2025 17:31:36 +0900 Subject: [PATCH 15/19] =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=9D=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/externals/controller/festival_item_controller.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/externals/controller/festival_item_controller.go b/api/externals/controller/festival_item_controller.go index 68512aea..5257f2bc 100644 --- a/api/externals/controller/festival_item_controller.go +++ b/api/externals/controller/festival_item_controller.go @@ -41,11 +41,11 @@ func (f *festivalItemController) CreateFestivalItem(c echo.Context) error { if err := c.Bind(festivalItem); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } - latastFestivalItem, err := f.u.CreateFestivalItem(c.Request().Context(), *festivalItem) + latestFestivalItem, err := f.u.CreateFestivalItem(c.Request().Context(), *festivalItem) if err != nil { return err } - return c.JSON(http.StatusOK, latastFestivalItem) + return c.JSON(http.StatusOK, latestFestivalItem) } func (f *festivalItemController) UpdateFestivalItem(c echo.Context) error { From ef3e22734fee473d635c2e8f5a6b8391e2fcccdb Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Tue, 14 Jan 2025 21:37:43 +0900 Subject: [PATCH 16/19] =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E3=82=AF=E3=82=A8?= =?UTF-8?q?=E3=83=AA=E3=81=AE=E3=83=AD=E3=82=B0=E5=87=BA=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../abstract/abstract_repository.go | 16 ++++++++++++ .../repository/festival_item_repository.go | 25 ++++++++----------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/api/externals/repository/abstract/abstract_repository.go b/api/externals/repository/abstract/abstract_repository.go index 790a41ee..b4a2b63a 100644 --- a/api/externals/repository/abstract/abstract_repository.go +++ b/api/externals/repository/abstract/abstract_repository.go @@ -18,7 +18,9 @@ type Crud interface { ReadByID(context.Context, string) (*sql.Row, error) UpdateDB(context.Context, string) error StartTransaction(context.Context) (*sql.Tx, error) + TransactionExec(context.Context, *sql.Tx, string) error Commit(context.Context, *sql.Tx) error + RollBack(context.Context, *sql.Tx) error } func NewCrud(client db.Client) Crud { @@ -51,9 +53,23 @@ func (a abstractRepository) UpdateDB(ctx context.Context, query string) error { } func (a abstractRepository) StartTransaction(ctx context.Context) (*sql.Tx, error) { + fmt.Printf("\x1b[36m%s\n", "TransactionStart") return a.client.DB().BeginTx(ctx, nil) } +func (a abstractRepository) TransactionExec(ctx context.Context, tx *sql.Tx, query string) error { + fmt.Printf("\x1b[36m%s\n", "TransactionExec") + _, err := tx.ExecContext(ctx, query) + fmt.Printf("\x1b[36m%s\n", query) + return err +} + func (a abstractRepository) Commit(ctx context.Context, tx *sql.Tx) error { + fmt.Printf("\x1b[36m%s\n", "Commit") return tx.Commit() } + +func (a abstractRepository) RollBack(ctx context.Context, tx *sql.Tx) error { + fmt.Printf("\x1b[36m%s\n", "RollBack") + return tx.Rollback() +} diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index ee31ffa7..9c5975d4 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -3,7 +3,6 @@ package repository import ( "context" "database/sql" - "fmt" "github.com/NUTFes/FinanSu/api/drivers/db" "github.com/NUTFes/FinanSu/api/externals/repository/abstract" @@ -30,7 +29,7 @@ type FestivalItemRepository interface { DeleteItemBudget(context.Context, *sql.Tx, string) error FindLatestRecord(context.Context) (*sql.Row, error) StartTransaction(context.Context) (*sql.Tx, error) - RollBack(context.Context, *sql.Tx) + RollBack(context.Context, *sql.Tx) error Commit(context.Context, *sql.Tx) error } @@ -179,8 +178,7 @@ func (fir *festivalItemRepository) CreateFestivalItem( if err != nil { return err } - _, err = tx.Exec(query) - + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -195,8 +193,7 @@ func (fir *festivalItemRepository) CreateItemBudget( if err != nil { return err } - _, err = tx.Exec(query) - + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -214,7 +211,7 @@ func (fir *festivalItemRepository) UpdateFestivalItem( if err != nil { return err } - _, err = tx.Exec(query) + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -229,11 +226,10 @@ func (fir *festivalItemRepository) UpdateItemBudget( Set(goqu.Record{"amount": festivalItem.Amount}). Where(goqu.Ex{"festival_item_id": id}) query, _, err := ds.ToSQL() - fmt.Println(query) if err != nil { return err } - _, err = tx.Exec(query) + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -248,8 +244,7 @@ func (fir *festivalItemRepository) DeleteFestivalItem( if err != nil { return err } - fmt.Println(query) - _, err = tx.Exec(query) + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -264,7 +259,7 @@ func (fir *festivalItemRepository) DeleteItemBudget( if err != nil { return err } - _, err = tx.Exec(query) + err = fir.crud.TransactionExec(c, tx, query) return err } @@ -300,10 +295,10 @@ func (fir *festivalItemRepository) StartTransaction(c context.Context) (*sql.Tx, return fir.crud.StartTransaction(c) } -func (fir *festivalItemRepository) RollBack(c context.Context, tx *sql.Tx) { - tx.Rollback() +func (fir *festivalItemRepository) RollBack(c context.Context, tx *sql.Tx) error { + return fir.crud.RollBack(c, tx) } func (fir *festivalItemRepository) Commit(c context.Context, tx *sql.Tx) error { - return tx.Commit() + return fir.crud.Commit(c, tx) } From 6f25503803adda4edfd7e2b6d062a56687af8f9a Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Tue, 14 Jan 2025 21:45:08 +0900 Subject: [PATCH 17/19] =?UTF-8?q?=E3=82=AF=E3=82=A8=E3=83=AA=E3=81=AE?= =?UTF-8?q?=E5=85=B1=E9=80=9A=E9=83=A8=E5=88=86=E3=82=92=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 136 +++--------------- 1 file changed, 21 insertions(+), 115 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 9c5975d4..7ce39697 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -17,8 +17,6 @@ type festivalItemRepository struct { type FestivalItem = generated.FestivalItem 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, FestivalItem) error @@ -37,88 +35,13 @@ func NewFestivalItemRepository(c db.Client, ac abstract.Crud) FestivalItemReposi return &festivalItemRepository{c, ac} } -// 年度別に取得 -func (fir *festivalItemRepository) All( - c context.Context, -) (*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()). - ToSQL() - if err != nil { - return nil, err - } - return fir.crud.Read(c, query) -} - -// 年度別に取得 -func (fir *festivalItemRepository) AllByPeriod( - c context.Context, - year 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{"years.year": year}). - ToSQL() - if err != nil { - return nil, err - } - return fir.crud.Read(c, query) -} - // 年度別と部門で取得 func (fir *festivalItemRepository) AllByPeriodAndDivision( c context.Context, year string, divisionId string, ) (*sql.Rows, error) { - ds := 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()) - + ds := selectFestivalItemQuery if divisionId != "" { ds = ds.Where(goqu.Ex{"divisions.id": divisionId}) } @@ -141,24 +64,7 @@ func (fir *festivalItemRepository) GetById( c context.Context, id string, ) (*sql.Row, error) { - query, _, err := dialect.Select( - "festival_items.id", - "festival_items.name", - "festival_items.memo", - "financial_records.name", - "divisions.name", - "item_budgets.amount", - 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"). - Where(goqu.Ex{"festival_items.id": id}). - ToSQL() + query, _, err := selectFestivalItemQuery.Where(goqu.Ex{"festival_items.id": id}).ToSQL() if err != nil { return nil, err @@ -265,25 +171,7 @@ func (fir *festivalItemRepository) DeleteItemBudget( // 最新のfestivalItemを取得する func (fir *festivalItemRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { - query, _, err := dialect.Select( - "festival_items.id", - "festival_items.name", - "festival_items.memo", - "financial_records.name", - "divisions.name", - "item_budgets.amount", - 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()). - Limit(1). - ToSQL() + query, _, err := selectFestivalItemQuery.Limit(1).ToSQL() if err != nil { return nil, err @@ -302,3 +190,21 @@ func (fir *festivalItemRepository) RollBack(c context.Context, tx *sql.Tx) error func (fir *festivalItemRepository) Commit(c context.Context, tx *sql.Tx) error { return fir.crud.Commit(c, tx) } + +var selectFestivalItemQuery = 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()) From 6e5ef9ebd6833a0b5b0279be6977586d067bceac Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Tue, 14 Jan 2025 21:48:34 +0900 Subject: [PATCH 18/19] =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=9D=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/financial_record_controller.go | 4 +-- .../usecase/festival_item_usecase.go | 30 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/api/externals/controller/financial_record_controller.go b/api/externals/controller/financial_record_controller.go index d8454d1a..17037b01 100644 --- a/api/externals/controller/financial_record_controller.go +++ b/api/externals/controller/financial_record_controller.go @@ -48,11 +48,11 @@ func (f *financialRecordController) CreateFinancialRecord(c echo.Context) error if err := c.Bind(financialRecord); err != nil { return c.String(http.StatusBadRequest, "Bad Request") } - latastFinancialRecord, err := f.u.CreateFinancialRecord(c.Request().Context(), *financialRecord) + latestFinancialRecord, err := f.u.CreateFinancialRecord(c.Request().Context(), *financialRecord) if err != nil { return err } - return c.JSON(http.StatusOK, latastFinancialRecord) + return c.JSON(http.StatusOK, latestFinancialRecord) } func (f *financialRecordController) UpdateFinancialRecord(c echo.Context) error { diff --git a/api/internals/usecase/festival_item_usecase.go b/api/internals/usecase/festival_item_usecase.go index 8e1ee827..6252c198 100644 --- a/api/internals/usecase/festival_item_usecase.go +++ b/api/internals/usecase/festival_item_usecase.go @@ -87,7 +87,7 @@ func (fiu *festivalItemUseCase) CreateFestivalItem( c context.Context, festivalItem FestivalItem, ) (FestivalItemWithBalance, error) { - latastFestivalItemWithBalance := FestivalItemWithBalance{} + latestFestivalItemWithBalance := FestivalItemWithBalance{} // トランザクションスタート tx, _ := fiu.rep.StartTransaction(c) @@ -95,39 +95,39 @@ func (fiu *festivalItemUseCase) CreateFestivalItem( if err := fiu.rep.CreateFestivalItem(c, tx, festivalItem); err != nil { // エラーが発生時はロールバック fiu.rep.RollBack(c, tx) - return latastFestivalItemWithBalance, err + return latestFestivalItemWithBalance, err } if err := fiu.rep.CreateItemBudget(c, tx, festivalItem); err != nil { // エラーが発生時はロールバック fiu.rep.RollBack(c, tx) - return latastFestivalItemWithBalance, err + return latestFestivalItemWithBalance, err } // コミットしてトランザクション終了 if err := fiu.rep.Commit(c, tx); err != nil { - return latastFestivalItemWithBalance, err + return latestFestivalItemWithBalance, err } row, err := fiu.rep.FindLatestRecord(c) if err != nil { - return latastFestivalItemWithBalance, err + return latestFestivalItemWithBalance, err } err = row.Scan( - &latastFestivalItemWithBalance.Id, - &latastFestivalItemWithBalance.Name, - &latastFestivalItemWithBalance.Memo, - &latastFestivalItemWithBalance.FinancialRecord, - &latastFestivalItemWithBalance.Division, - &latastFestivalItemWithBalance.Budget, - &latastFestivalItemWithBalance.Expense, - &latastFestivalItemWithBalance.Balance, + &latestFestivalItemWithBalance.Id, + &latestFestivalItemWithBalance.Name, + &latestFestivalItemWithBalance.Memo, + &latestFestivalItemWithBalance.FinancialRecord, + &latestFestivalItemWithBalance.Division, + &latestFestivalItemWithBalance.Budget, + &latestFestivalItemWithBalance.Expense, + &latestFestivalItemWithBalance.Balance, ) if err != nil { - return latastFestivalItemWithBalance, err + return latestFestivalItemWithBalance, err } - return latastFestivalItemWithBalance, nil + return latestFestivalItemWithBalance, nil } func (fiu *festivalItemUseCase) UpdateFestivalItem( From ad0eb98e4165505023e4d39a8b70d41a302bb718 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Tue, 14 Jan 2025 22:42:45 +0900 Subject: [PATCH 19/19] =?UTF-8?q?=E3=82=AF=E3=82=A8=E3=83=AA=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/festival_item_repository.go | 2 +- .../repository/financial_record_repository.go | 70 +++++-------------- 2 files changed, 19 insertions(+), 53 deletions(-) diff --git a/api/externals/repository/festival_item_repository.go b/api/externals/repository/festival_item_repository.go index 7ce39697..cd116889 100644 --- a/api/externals/repository/festival_item_repository.go +++ b/api/externals/repository/festival_item_repository.go @@ -199,7 +199,7 @@ var selectFestivalItemQuery = dialect.Select( "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")). + goqu.L("COALESCE(SUM(item_budgets.amount), 0) - COALESCE(SUM(buy_reports.amount), 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")))). diff --git a/api/externals/repository/financial_record_repository.go b/api/externals/repository/financial_record_repository.go index 02d0ea5e..3fb39ab8 100644 --- a/api/externals/repository/financial_record_repository.go +++ b/api/externals/repository/financial_record_repository.go @@ -33,19 +33,7 @@ func NewFinancialRecordRepository(c db.Client, ac abstract.Crud) FinancialRecord func (frr *financialRecordRepository) All( c context.Context, ) (*sql.Rows, error) { - 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"). + query, _, err := selectFinancialRecordQuery. ToSQL() if err != nil { @@ -59,19 +47,7 @@ func (frr *financialRecordRepository) AllByPeriod( c context.Context, year string, ) (*sql.Rows, error) { - 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"). + query, _, err := selectFinancialRecordQuery. Where(goqu.Ex{"years.year": year}). ToSQL() if err != nil { @@ -85,19 +61,7 @@ func (frr *financialRecordRepository) GetById( c context.Context, id string, ) (*sql.Row, error) { - 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"). + query, _, err := selectFinancialRecordQuery. Where(goqu.Ex{"financial_records.id": id}). ToSQL() if err != nil { @@ -152,19 +116,7 @@ func (frr *financialRecordRepository) Delete( // 最新のsponcerを取得する func (frr *financialRecordRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { - 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"). + query, _, err := selectFinancialRecordQuery. Order(goqu.I("id").Desc()). Limit(1). ToSQL() @@ -173,3 +125,17 @@ func (frr *financialRecordRepository) FindLatestRecord(c context.Context) (*sql. } return frr.crud.ReadByID(c, query) } + +var selectFinancialRecordQuery = 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.L("COALESCE(SUM(item_budgets.amount), 0) - COALESCE(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")