Skip to content

Commit

Permalink
[feat] 年度別に協賛活動を取得するAPIの作成
Browse files Browse the repository at this point in the history
  • Loading branch information
hikahana committed Jan 30, 2024
1 parent 95f20b0 commit 87600bb
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ const docTemplate = `{
}
},
},
"/activities/details/{year}": {
"get": {
tags: ["activity"],
"description": "年度で指定されたactivitiesとsponsor,sponsorStyle,userの一覧を取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "年度で指定されたactivitiesとsponsor,sponsorStyle,userの一覧を取得",
}
}
},
},
"/activities/{id}": {
"get": {
tags: ["activity"],
Expand Down
11 changes: 11 additions & 0 deletions api/externals/controller/activity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ActivityController interface {
UpdateActivity(echo.Context) error
DestroyActivity(echo.Context) error
IndexActivityDetail(echo.Context) error
IndexActivityDetailsByPeriod(echo.Context) error
}

func NewActivityController(u usecase.ActivityUseCase) ActivityController {
Expand Down Expand Up @@ -93,3 +94,13 @@ func (a *activityController) IndexActivityDetail(c echo.Context) error {
}
return c.JSON(http.StatusOK, activities)
}

// 年度で指定されたactivitiesとsponsor,sponsorStyle,userの一覧を取得
func (a *activityController) IndexActivityDetailsByPeriod(c echo.Context) error {
year := c.Param("year")
activities, err := a.u.GetActivityDetailsByPeriod(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, activities)
}
37 changes: 37 additions & 0 deletions api/externals/repository/activity_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ActivityRepository interface {
FindDetail(context.Context) (*sql.Rows, error)
FindLatestRecord(c context.Context) (*sql.Row, error)
FindSponsorStyle(context.Context, string) (*sql.Rows, error)
AllDetailsByPeriod(context.Context, string) (*sql.Rows, error)
}

func NewActivityRepository(c db.Client, ac abstract.Crud) ActivityRepository {
Expand Down Expand Up @@ -98,6 +99,7 @@ func (ar *activityRepository) Destroy(c context.Context, id string) error {
return ar.crud.UpdateDB(c, query)
}

// activityに紐づくsponserとusersを取得する
func (ar *activityRepository) FindDetail(c context.Context) (*sql.Rows, error) {
query := `
SELECT * FROM
Expand All @@ -114,6 +116,7 @@ func (ar *activityRepository) FindDetail(c context.Context) (*sql.Rows, error) {
return ar.crud.Read(c, query)
}

// 最新のレコードを取得
func (ar *activityRepository) FindLatestRecord(c context.Context) (*sql.Row, error) {
query := `
SELECT
Expand Down Expand Up @@ -141,3 +144,37 @@ func (ar *activityRepository) FindSponsorStyle(c context.Context, sponsorStyleID
WHERE activity_styles.activity_id = ` + sponsorStyleID
return ar.crud.Read(c, query)
}

// 年度別のactivityに紐づくsponserとusersを取得する
func (ar *activityRepository) AllDetailsByPeriod(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
activities.*,
sponsors.*,
users.*
FROM
activities
INNER JOIN
sponsors
ON
activities.sponsor_id = sponsors.id
INNER JOIN
users
ON
activities.user_id = users.id
INNER JOIN
year_periods
ON
activities.created_at > year_periods.started_at
AND
activities.created_at < year_periods.ended_at
INNER JOIN
years
ON
year_periods.year_id = years.id
WHERE
years.year = ` + year +
" ORDER BY activities.id;"

return ar.crud.Read(c, query)
}
74 changes: 73 additions & 1 deletion api/internals/usecase/activity_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ActivityUseCase interface {
UpdateActivity(context.Context, string, string, string, string, string, string, string, string, string) (domain.Activity, error)
DestroyActivity(context.Context, string) error
GetActivityDetail(context.Context) ([]domain.ActivityDetail, error)
GetActivityDetailsByPeriod(context.Context, string) ([]domain.ActivityDetail, error)
}

func NewActivityUseCase(rep rep.ActivityRepository) ActivityUseCase {
Expand Down Expand Up @@ -144,7 +145,7 @@ func (a *activityUseCase) UpdateActivity(
&updatedActivity.Expense,
&updatedActivity.Remark,
&updatedActivity.Design,
&updatedActivity.Url,
&updatedActivity.Url,
&updatedActivity.CreatedAt,
&updatedActivity.UpdatedAt,
)
Expand Down Expand Up @@ -229,3 +230,74 @@ func (a *activityUseCase) GetActivityDetail(c context.Context) ([]domain.Activit
}
return activities, nil
}

func (a *activityUseCase) GetActivityDetailsByPeriod(c context.Context, year string) ([]domain.ActivityDetail, error) {

activity := domain.ActivityDetail{}
var activities []domain.ActivityDetail
styleDetail := domain.StyleDetail{}
var styleDetails []domain.StyleDetail
// クエリー実行
rows, err := a.rep.AllDetailsByPeriod(c, year)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
err := rows.Scan(
&activity.Activity.ID,
&activity.Activity.UserID,
&activity.Activity.IsDone,
&activity.Activity.SponsorID,
&activity.Activity.Feature,
&activity.Activity.Expense,
&activity.Activity.Remark,
&activity.Activity.Design,
&activity.Activity.Url,
&activity.Activity.CreatedAt,
&activity.Activity.UpdatedAt,
&activity.Sponsor.ID,
&activity.Sponsor.Name,
&activity.Sponsor.Tel,
&activity.Sponsor.Email,
&activity.Sponsor.Address,
&activity.Sponsor.Representative,
&activity.Sponsor.CreatedAt,
&activity.Sponsor.UpdatedAt,
&activity.User.ID,
&activity.User.Name,
&activity.User.BureauID,
&activity.User.RoleID,
&activity.User.CreatedAt,
&activity.User.UpdatedAt,
)
if err != nil {
return nil, errors.Wrapf(err, "cannot connect SQL")
}
rows, err := a.rep.FindSponsorStyle(c,strconv.Itoa(int(activity.Activity.ID)))
for rows.Next(){
err := rows.Scan(
&styleDetail.ActivityStyle.ID,
&styleDetail.ActivityStyle.ActivityID,
&styleDetail.ActivityStyle.SponsoStyleID,
&styleDetail.ActivityStyle.CreatedAt,
&styleDetail.ActivityStyle.UpdatedAt,
&styleDetail.SponsorStyle.ID,
&styleDetail.SponsorStyle.Style,
&styleDetail.SponsorStyle.Feature,
&styleDetail.SponsorStyle.Price,
&styleDetail.SponsorStyle.CreatedAt,
&styleDetail.SponsorStyle.UpdatedAt,
)
if err != nil {
return nil, err
}
styleDetails = append(styleDetails, styleDetail)
}
activity.StyleDetail = styleDetails
activities = append(activities, activity)
styleDetails = nil
}
return activities, nil
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.PUT("/activities/:id", r.activityController.UpdateActivity)
e.DELETE("/activities/:id", r.activityController.DestroyActivity)
e.GET("/activities/details", r.activityController.IndexActivityDetail)
e.GET("/activities/details/:year",r.activityController.IndexActivityDetailsByPeriod)

// activityStyleのRoute
e.GET("/activity_styles", r.activityStyleController.IndexActivityStyle)
Expand Down

0 comments on commit 87600bb

Please sign in to comment.