Skip to content

Commit

Permalink
[支出]年度別取得APIの作成
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Jan 28, 2024
1 parent 95f20b0 commit da7cc2e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
14 changes: 14 additions & 0 deletions api/externals/controller/expense_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions api/externals/repository/expense_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions api/internals/domain/expense.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
83 changes: 83 additions & 0 deletions api/internals/usecase/expense_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit da7cc2e

Please sign in to comment.