From da7cc2ea682b1a92283cc3b6055c65fd83e3d970 Mon Sep 17 00:00:00 2001 From: Kubosaka Date: Sun, 28 Jan 2024 18:24:48 +0900 Subject: [PATCH] =?UTF-8?q?[=E6=94=AF=E5=87=BA]=E5=B9=B4=E5=BA=A6=E5=88=A5?= =?UTF-8?q?=E5=8F=96=E5=BE=97API=E3=81=AE=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/docs/docs.go | 20 +++++ .../controller/expense_controller.go | 14 ++++ .../repository/expense_repository.go | 17 ++++ api/internals/domain/expense.go | 6 ++ api/internals/usecase/expense_usecase.go | 83 +++++++++++++++++++ api/router/router.go | 1 + 6 files changed, 141 insertions(+) diff --git a/api/docs/docs.go b/api/docs/docs.go index deac96a02..158ced16e 100644 --- a/api/docs/docs.go +++ b/api/docs/docs.go @@ -671,6 +671,26 @@ const docTemplate = `{ } }, }, + "/expenses/details/{year}": { + "get": { + tags: ["expense"], + "description": "年度で指定されたexpenseに紐づく購入申請と購入報告を取得", + "parameters": [ + { + "name": "year", + "in": "path", + "description": "year", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "yearで指定されたexpenseに紐づく購入申請と購入報告を取得", + } + } + }, + }, "/fund_informations": { "get": { tags: ["fund_information"], diff --git a/api/externals/controller/expense_controller.go b/api/externals/controller/expense_controller.go index a0e51b73b..5fa2e0138 100644 --- a/api/externals/controller/expense_controller.go +++ b/api/externals/controller/expense_controller.go @@ -20,6 +20,7 @@ type ExpenseController interface { UpdateExpenseTP(echo.Context) error IndexExpenseDetails(echo.Context) error ShowExpenseDetail(echo.Context) error + IndexExpenseDetailsByPeriod(echo.Context) error } func NewExpenseController(u usecase.ExpenseUseCase) ExpenseController { @@ -105,3 +106,16 @@ func (e *expenseController) ShowExpenseDetail(c echo.Context) error { } return c.JSON(http.StatusOK, expenseDetail) } + +func (e *expenseController) IndexExpenseDetailsByPeriod(c echo.Context) error { + year := c.Param("year") + err := e.u.UpdateExpenseTP(c.Request().Context()) + if err != nil { + return err + } + expenseDetails, err := e.u.GetExpenseDetailsByPeriod(c.Request().Context(), year) + if err != nil { + return err + } + return c.JSON(http.StatusOK, expenseDetails) +} diff --git a/api/externals/repository/expense_repository.go b/api/externals/repository/expense_repository.go index d7383a304..788e4a869 100644 --- a/api/externals/repository/expense_repository.go +++ b/api/externals/repository/expense_repository.go @@ -23,6 +23,7 @@ type ExpenseRepository interface { FindLatestRecord(context.Context) (*sql.Row, error) AllItemInfo(context.Context, string) (*sql.Rows, error) AllOrderAndReportInfo(context.Context, string) (*sql.Rows, error) + AllByPeriod(context.Context, string) (*sql.Rows, error) } func NewExpenseRepository(c db.Client, ac abstract.Crud) ExpenseRepository { @@ -107,3 +108,19 @@ func (er *expenseRepository) AllOrderAndReportInfo(c context.Context, expenseID ` return er.crud.Read(c, query) } + +func (er *expenseRepository) AllByPeriod(c context.Context, year string) (*sql.Rows, error) { + query := ` + SELECT + * + FROM + expense + INNER JOIN + years + ON + expense.yearID = years.id + WHERE + years.year = ` + year + + " ORDER BY expense.id;" + return er.crud.Read(c, query) +} diff --git a/api/internals/domain/expense.go b/api/internals/domain/expense.go index e46da2ba1..60055f9bd 100644 --- a/api/internals/domain/expense.go +++ b/api/internals/domain/expense.go @@ -23,3 +23,9 @@ type PurchaseDetail struct { PurchaseReport PurchaseReport `json:"purchaseReport"` PurchaseItems []PurchaseItem `json:"purchaseItems"` } + +type ExpenseDetailsByperiod struct { + Expense Expense `json:"expense"` + Year Year `json:"year"` + PurchaseDetails []PurchaseDetail `json:"purchaseDetails"` +} diff --git a/api/internals/usecase/expense_usecase.go b/api/internals/usecase/expense_usecase.go index 7f3a77fc3..cedb75c87 100644 --- a/api/internals/usecase/expense_usecase.go +++ b/api/internals/usecase/expense_usecase.go @@ -22,6 +22,7 @@ type ExpenseUseCase interface { UpdateExpenseTP(context.Context) error GetExpenseDetails(context.Context) ([]domain.ExpenseDetails, error) GetExpenseDetailByID(context.Context, string) (domain.ExpenseDetails, error) + GetExpenseDetailsByPeriod(context.Context, string) ([]domain.ExpenseDetailsByperiod, error) } func NewExpenseUseCase(rep rep.ExpenseRepository) ExpenseUseCase { @@ -267,3 +268,85 @@ func (e *expenseUseCase) GetExpenseDetailByID(c context.Context, id string) (dom expenseDetail.PurchaseDetails = purchaseDetails return expenseDetail, nil } + +func (e *expenseUseCase) GetExpenseDetailsByPeriod(c context.Context, year string) ([]domain.ExpenseDetailsByperiod, error) { + expenseDetail := domain.ExpenseDetailsByperiod{} + var expenseDetails []domain.ExpenseDetailsByperiod + purchaseDetail := domain.PurchaseDetail{} + var purchaseDetails []domain.PurchaseDetail + purchaseItem := domain.PurchaseItem{} + var purchaseItems []domain.PurchaseItem + + rows, err := e.rep.AllByPeriod(c, year) + if err != nil { + return nil, err + } + for rows.Next() { + err := rows.Scan( + &expenseDetail.Expense.ID, + &expenseDetail.Expense.Name, + &expenseDetail.Expense.TotalPrice, + &expenseDetail.Expense.YearID, + &expenseDetail.Expense.CreatedAt, + &expenseDetail.Expense.UpdatedAt, + &expenseDetail.Year.ID, + &expenseDetail.Year.Year, + &expenseDetail.Year.CreatedAt, + &expenseDetail.Year.UpdatedAt, + ) + if err != nil { + return nil, err + } + rows, err := e.rep.AllOrderAndReportInfo(c, strconv.Itoa(int(expenseDetail.Expense.ID))) + for rows.Next() { + err := rows.Scan( + &purchaseDetail.PurchaseReport.ID, + &purchaseDetail.PurchaseReport.UserID, + &purchaseDetail.PurchaseReport.Discount, + &purchaseDetail.PurchaseReport.Addition, + &purchaseDetail.PurchaseReport.FinanceCheck, + &purchaseDetail.PurchaseReport.PurchaseOrderID, + &purchaseDetail.PurchaseReport.Remark, + &purchaseDetail.PurchaseReport.Buyer, + &purchaseDetail.PurchaseReport.CreatedAt, + &purchaseDetail.PurchaseReport.UpdatedAt, + &purchaseDetail.PurchaseOrder.ID, + &purchaseDetail.PurchaseOrder.DeadLine, + &purchaseDetail.PurchaseOrder.UserID, + &purchaseDetail.PurchaseOrder.ExpenseID, + &purchaseDetail.PurchaseOrder.FinanceCheck, + &purchaseDetail.PurchaseOrder.CreatedAt, + &purchaseDetail.PurchaseOrder.UpdatedAt, + ) + if err != nil { + return nil, err + } + rows, err := e.rep.AllItemInfo(c, strconv.Itoa(int(purchaseDetail.PurchaseOrder.ID))) + for rows.Next() { + err := rows.Scan( + &purchaseItem.ID, + &purchaseItem.Item, + &purchaseItem.Price, + &purchaseItem.Quantity, + &purchaseItem.Detail, + &purchaseItem.Url, + &purchaseItem.PurchaseOrderID, + &purchaseItem.FinanceCheck, + &purchaseItem.CreatedAt, + &purchaseItem.UpdatedAt, + ) + if err != nil { + return nil, err + } + purchaseItems = append(purchaseItems, purchaseItem) + } + purchaseDetail.PurchaseItems = purchaseItems + purchaseItems = nil + purchaseDetails = append(purchaseDetails, purchaseDetail) + } + expenseDetail.PurchaseDetails = purchaseDetails + purchaseDetails = nil + expenseDetails = append(expenseDetails, expenseDetail) + } + return expenseDetails, nil +} diff --git a/api/router/router.go b/api/router/router.go index feacb5514..4d3720996 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -121,6 +121,7 @@ func (r router) ProvideRouter(e *echo.Echo) { e.GET("/expenses", r.expenseController.IndexExpense) e.GET("/expenses/updateTP", r.expenseController.UpdateExpenseTP) e.GET("/expenses/details", r.expenseController.IndexExpenseDetails) + e.GET("/expenses/details/:year", r.expenseController.IndexExpenseDetailsByPeriod) e.GET("/expenses/:id", r.expenseController.ShowExpense) e.GET("/expenses/:id/details", r.expenseController.ShowExpenseDetail) e.POST("/expenses", r.expenseController.CreateExpense)