Skip to content

Commit

Permalink
[feat] 年度で指定されたsponsorsを取得する
Browse files Browse the repository at this point in the history
  • Loading branch information
hikahana committed Feb 20, 2024
1 parent 405394f commit 7fedd2f
Show file tree
Hide file tree
Showing 5 changed files with 84 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 @@ -1627,6 +1627,26 @@ const docTemplate = `{
},
},
},
"/sponsors/{year}": {
"get": {
tags: ["sponsor"],
"description": "年度で指定されたsponsorを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "sponsorの取得完了",
}
}
},
},
"/sponsorstyles": {
"get": {
tags: ["sponsorstyle"],
Expand Down
11 changes: 11 additions & 0 deletions api/externals/controller/sponsor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SponsorController interface {
CreateSponsor(echo.Context) error
UpdateSponsor(echo.Context) error
DestroySponsor(echo.Context) error
IndexSponsorByPeriod(echo.Context) error
}

func NewSponsorController(u usecase.SponsorUseCase) SponsorController {
Expand Down Expand Up @@ -90,3 +91,13 @@ func (s *sponsorController) DestroySponsor(c echo.Context) error {
}
return c.String(http.StatusOK, "Destroy Sponsor")
}

//年度別に取得
func (s *sponsorController) IndexSponsorByPeriod(c echo.Context) error {
year := c.Param("year")
sponsors, err := s.u.GetSponsorByPeriod(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, sponsors)
}
24 changes: 24 additions & 0 deletions api/externals/repository/sponsor_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type SponsorRepository interface {
Update(context.Context, string, string, string, string, string, string) error
Delete(context.Context, string) error
FindLatestRecord(context.Context) (*sql.Row, error)
AllByPeriod(context.Context, string) (*sql.Rows, error)
}

func NewSponsorRepository(c db.Client, ac abstract.Crud) SponsorRepository {
Expand Down Expand Up @@ -92,3 +93,26 @@ func (sr *sponsorRepository) FindLatestRecord(c context.Context) (*sql.Row, erro
query := `SELECT * FROM sponsors ORDER BY id DESC LIMIT 1`
return sr.crud.ReadByID(c, query)
}

// 年度別に取得
func (sr *sponsorRepository) AllByPeriod(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
sponsors.*
FROM
sponsors
INNER JOIN
year_periods
ON
sponsors.created_at > year_periods.started_at
AND
sponsors.created_at < year_periods.ended_at
INNER JOIN
years
ON
year_periods.year_id = years.id
WHERE
years.year = ` + year +
" ORDER BY sponsors.id;"
return sr.crud.Read(c, query)
}
28 changes: 28 additions & 0 deletions api/internals/usecase/sponsor_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SponsorUseCase interface {
CreateSponsor(context.Context, string, string, string, string, string) (domain.Sponsor, error)
UpdateSponsor(context.Context, string, string, string, string, string, string) (domain.Sponsor, error)
DestroySponsor(context.Context, string) error
GetSponsorByPeriod(context.Context, string) ([]domain.Sponsor, error)
}

func NewSponsorUseCase(rep rep.SponsorRepository) SponsorUseCase {
Expand Down Expand Up @@ -132,3 +133,30 @@ func (s *sponsorUseCase) DestroySponsor(
err := s.rep.Delete(c, id)
return err
}

// 年度別のsponsorsの取得(GetByPeriod)
func (s *sponsorUseCase) GetSponsorByPeriod(c context.Context, year string) ([]domain.Sponsor, error) {
sponsor := domain.Sponsor{}
var sponsors []domain.Sponsor
rows, err := s.rep.AllByPeriod(c, year)
if err != nil {
return nil, err
}
for rows.Next() {
err := rows.Scan(
&sponsor.ID,
&sponsor.Name,
&sponsor.Tel,
&sponsor.Email,
&sponsor.Address,
&sponsor.Representative,
&sponsor.CreatedAt,
&sponsor.UpdatedAt,
)
if err != nil {
return nil, err
}
sponsors = append(sponsors, sponsor)
}
return sponsors, nil
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.POST("/sponsors", r.sponsorController.CreateSponsor)
e.PUT("/sponsors/:id", r.sponsorController.UpdateSponsor)
e.DELETE("/sponsors/:id", r.sponsorController.DestroySponsor)
e.GET("/sponsors/:year", r.sponsorController.IndexSponsorByPeriod)

// sponsorstylesのRoute
e.GET("/sponsorstyles", r.sponsorStyleController.IndexSponsorStyle)
Expand Down

0 comments on commit 7fedd2f

Please sign in to comment.