Skip to content

Commit

Permalink
[feat]フィルタありのAPI実装
Browse files Browse the repository at this point in the history
  • Loading branch information
KazumaSun committed Jul 2, 2024
1 parent d34943f commit afe460e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ const docTemplate = `{
}
},
},
"/activities/filtered_details": {
"get": {
tags: ["activity"],
"description": "activitiesとsponsor,sponsorStyle,userの一覧を取得",
"parameters": [
{
"in": "query",
"name": "is_done",
"type": "boolean"
},
{
"in": "query",
"name": "sponsor_style",
"type": "integer"
},
{
"in": "query",
"name": "keyword",
"type": "string"
},
],
"responses": {
"200": {
"description": "activitiesとsponsor,sponsorStyle,userの一覧の取得",
}
}
},
},
"/activities/{id}": {
"get": {
tags: ["activity"],
Expand Down
12 changes: 12 additions & 0 deletions api/externals/controller/activity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ActivityController interface {
DestroyActivity(echo.Context) error
IndexActivityDetail(echo.Context) error
IndexActivityDetailsByPeriod(echo.Context) error
IndexFilteredActivityDetail(echo.Context) error
}

func NewActivityController(u usecase.ActivityUseCase) ActivityController {
Expand Down Expand Up @@ -104,3 +105,14 @@ func (a *activityController) IndexActivityDetailsByPeriod(c echo.Context) error
}
return c.JSON(http.StatusOK, activities)
}

func (a *activityController) IndexFilteredActivityDetail(c echo.Context) error {
isDone := c.QueryParam("is_done")
sponsorStyle := c.QueryParam("sponsor_style")
keyword := c.QueryParam("keyword")
activities, err := a.u.GetFilteredActivityDetail(c.Request().Context(), isDone, sponsorStyle, keyword)
if err != nil {
return err
}
return c.JSON(http.StatusOK, activities)
}
24 changes: 24 additions & 0 deletions api/externals/repository/activity_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ActivityRepository interface {
FindSponsorStyle(context.Context, string) (*sql.Rows, error)
AllDetailsByPeriod(context.Context, string) (*sql.Rows, error)
FindActivityInformation(context.Context, string) (*sql.Rows, error)
FindFilteredDetail(context.Context, string, string, string) (*sql.Rows, error)
}

func NewActivityRepository(c db.Client, ac abstract.Crud) ActivityRepository {
Expand Down Expand Up @@ -185,3 +186,26 @@ func (ar *activityRepository) AllDetailsByPeriod(c context.Context, year string)

return ar.crud.Read(c, query)
}

// activityに紐づくsponserとusersをフィルタを考慮して取得する
func (ar *activityRepository) FindFilteredDetail(c context.Context, isDone string, sponsorStyle string, keyword string) (*sql.Rows, error) {
query := `
SELECT * FROM
activities
INNER JOIN
sponsors
ON
activities.sponsor_id = sponsors.id
INNER JOIN
users
ON
activities.user_id = users.id
WHERE
sponsors.name LIKE '%` + keyword + `%'
AND
sponsors.id = ` + sponsorStyle + `
AND
activities.is_done = ` + isDone

return ar.crud.Read(c, query)
}
100 changes: 100 additions & 0 deletions api/internals/usecase/activity_usecase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usecase

import (
"fmt"
"context"
"strconv"

Expand All @@ -21,6 +22,7 @@ type ActivityUseCase interface {
DestroyActivity(context.Context, string) error
GetActivityDetail(context.Context) ([]domain.ActivityDetail, error)
GetActivityDetailsByPeriod(context.Context, string) ([]domain.ActivityDetail, error)
GetFilteredActivityDetail(context.Context, string, string, string) ([]domain.ActivityDetail, error)
}

func NewActivityUseCase(rep rep.ActivityRepository) ActivityUseCase {
Expand Down Expand Up @@ -353,3 +355,101 @@ func (a *activityUseCase) GetActivityDetailsByPeriod(c context.Context, year str
}
return activities, nil
}

func (a *activityUseCase) GetFilteredActivityDetail(c context.Context, isDone string, sponsorStyle string, keyword string) ([]domain.ActivityDetail, error) {

activity := domain.ActivityDetail{}
var activities []domain.ActivityDetail
styleDetail := domain.StyleDetail{}
var styleDetails []domain.StyleDetail
activityInformation := domain.ActivityInformation{}
var activityInformations []domain.ActivityInformation

// クエリー実行
rows, err := a.rep.FindFilteredDetail(c, isDone, sponsorStyle, keyword)
if err != nil {
fmt.Println(err)
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.IsDeleted,
&activity.User.CreatedAt,
&activity.User.UpdatedAt,
)
if err != nil {
return nil, errors.Wrapf(err, "cannot connect SQL")
}

rows, err := a.rep.FindActivityInformation(c, strconv.Itoa(int(activity.Activity.ID)))
for rows.Next(){
err := rows.Scan(
&activityInformation.ID,
&activityInformation.ActivityId,
&activityInformation.BucketName,
&activityInformation.FileName,
&activityInformation.FileType,
&activityInformation.DesignProgress,
&activityInformation.FileInformation,
&activityInformation.CreatedAt,
&activityInformation.UpdatedAt,
)
if err != nil {
return nil, err
}
activityInformations = append(activityInformations, activityInformation)
}
activity.ActivityInformation = activityInformations
activityInformations = nil

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 @@ -90,6 +90,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.DELETE("/activities/:id", r.activityController.DestroyActivity)
e.GET("/activities/details", r.activityController.IndexActivityDetail)
e.GET("/activities/details/:year",r.activityController.IndexActivityDetailsByPeriod)
e.GET("/activities/filtered_details", r.activityController.IndexFilteredActivityDetail)

// activityInformationsのRoute
e.GET("/activity_informations", r.activityInformationController.IndexActivityInformation)
Expand Down

0 comments on commit afe460e

Please sign in to comment.