Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]予算支出ページの年度切り替え実装 #681

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,26 @@ const docTemplate = `{
}
},
},
"/budgets/details/{year}": {
"get": {
tags: ["budget"],
"description": "年度で指定されたbudgetsに紐づく年度とソースを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "年度で指定されたbudgetsに紐づく年度とソースを取得",
}
}
},
},
"/budgets/{id}": {
"get": {
tags: ["budget"],
Expand Down
11 changes: 11 additions & 0 deletions api/externals/controller/budget_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BudgetController interface {
DestroyBudget(echo.Context) error
ShowBudgetDetailById(echo.Context) error
ShowBudgetDetails(echo.Context) error
ShowBudgetDetailsByPeriods(echo.Context) error
}

func NewBudgetController(u usecase.BudgetUseCase) BudgetController {
Expand Down Expand Up @@ -97,3 +98,13 @@ func (b *budgetController) ShowBudgetDetails(c echo.Context) error {
}
return c.JSON(http.StatusOK, budgetDetails)
}

// 年度ごとにBudgetと紐づくデータ全件取得
func (b *budgetController) ShowBudgetDetailsByPeriods(c echo.Context) error {
year := c.Param("year")
budgetDetails, err := b.u.GetBudgetDetailsByPeriod(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, budgetDetails)
}
23 changes: 23 additions & 0 deletions api/externals/repository/budget_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BudgetRepository interface {
FindDetailByID(context.Context, string) (*sql.Row, error)
FindLatestRecord(context.Context) (*sql.Row, error)
FindDetail(context.Context) (*sql.Rows, error)
FindDetailsByPeriod(context.Context, string) (*sql.Rows, error)
}

func NewBudgetRepository(c db.Client, ac abstract.Crud) BudgetRepository {
Expand Down Expand Up @@ -128,3 +129,25 @@ func (br *budgetRepository) FindLatestRecord(c context.Context) (*sql.Row, error
`
return br.crud.ReadByID(c, query)
}

// yearで切り替えるBudgetに紐づくyearとsourceを全件取得する
func (br *budgetRepository) FindDetailsByPeriod(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
*
FROM
budgets
INNER JOIN
years
ON
budgets.year_id = years.id
INNER JOIN
sources
ON
budgets.source_id = sources.id
WHERE
years.year = ` + year +
" ORDER BY budgets.id;"

return br.crud.Read(c, query)
}
37 changes: 37 additions & 0 deletions api/internals/usecase/budget_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type BudgetUseCase interface {
DestroyBudget(context.Context, string) error
GetBudgetDetailByID(context.Context, string) (domain.BudgetDetail, error)
GetBudgetDetails(c context.Context) ([]domain.BudgetDetail, error)
GetBudgetDetailsByPeriod(context.Context, string) ([]domain.BudgetDetail, error)
}

func NewBudgetUseCase(rep rep.BudgetRepository) BudgetUseCase {
Expand Down Expand Up @@ -175,3 +176,39 @@ func (b *budgetUseCase) GetBudgetDetails(c context.Context) ([]domain.BudgetDeta
}
return budgetDetails, nil
}


func (b *budgetUseCase) GetBudgetDetailsByPeriod(c context.Context, year string) ([]domain.BudgetDetail, error) {
budgetDetail := domain.BudgetDetail{}
var budgetDetails []domain.BudgetDetail

rows, err := b.rep.FindDetailsByPeriod(c, year)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
err := rows.Scan(
&budgetDetail.Budget.ID,
&budgetDetail.Budget.Price,
&budgetDetail.Budget.YearID,
&budgetDetail.Budget.SourceID,
&budgetDetail.Budget.CreatedAt,
&budgetDetail.Budget.UpdatedAt,
&budgetDetail.Year.ID,
&budgetDetail.Year.Year,
&budgetDetail.Year.CreatedAt,
&budgetDetail.Year.UpdatedAt,
&budgetDetail.Source.ID,
&budgetDetail.Source.Name,
&budgetDetail.Source.CreatedAt,
&budgetDetail.Source.UpdatedAt,
)
if err != nil {
return nil, errors.Wrapf(err, "cannot connect SQL")
}
budgetDetails = append(budgetDetails, budgetDetail)
}
return budgetDetails, nil
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.DELETE("/budgets/:id", r.budgetController.DestroyBudget)
e.GET("/budgets/:id/details", r.budgetController.ShowBudgetDetailById)
e.GET("/budgets/details", r.budgetController.ShowBudgetDetails)
e.GET("/budgets/details/:year", r.budgetController.ShowBudgetDetailsByPeriods)

//bureauのRoute
e.GET("/bureaus", r.bureauController.IndexBureau)
Expand Down
Loading
Loading