Skip to content

Commit

Permalink
Merge pull request #812 from NUTFes/feat/sato/800-fitering-sponsor-ac…
Browse files Browse the repository at this point in the history
…tivites-api

[feat]フィルタありのactivitesのAPI実装
  • Loading branch information
Kubosaka authored Jul 6, 2024
2 parents 717094d + 98177fa commit 6152f32
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 0 deletions.
101 changes: 101 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,107 @@ const docTemplate = `{
}
},
},
"/activities/filtered_details": {
"get": {
tags: ["activity"],
"description": "activitiesとsponsor,sponsorStyle,userの一覧を取得",
"parameters": [
{
"name": "is_done",
"in": "query",
"description": "Filter by done status.[true, false, all]",
"required": false,
"schema": {
"type": "string",
"enum": ["true", "false", "all"]
}
},
{
"name": "sponsor_style_id",
"in": "query",
"description": "Filter by sponsor style IDs. ex.) 1,3,6,...",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "integer"
}
},
"style": "form",
"explode": true
},
{
"name": "keyword",
"in": "query",
"description": "Filter by keyword",
"required": false,
"schema": {
"type": "string"
}
},
],
"responses": {
"200": {
"description": "activitiesとsponsor,sponsorStyle,userの一覧の取得",
}
}
},
},
"/activities/filtered_details/{year}": {
"get": {
tags: ["activity"],
"description": "activitiesとsponsor,sponsorStyle,userの一覧を取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "Filter by year.",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "is_done",
"in": "query",
"description": "Filter by done status.[true, false, all]",
"required": false,
"schema": {
"type": "string",
"enum": ["true", "false", "all"]
}
},
{
"name": "sponsor_style_id",
"in": "query",
"description": "Filter by sponsor style IDs. ex.) 1,3,6,...",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "integer"
}
},
"style": "form",
"explode": true
},
{
"name": "keyword",
"in": "query",
"description": "Filter by keyword",
"required": false,
"schema": {
"type": "string"
}
},
],
"responses": {
"200": {
"description": "activitiesとsponsor,sponsorStyle,userの一覧の取得",
}
}
},
},
"/activities/{id}": {
"get": {
tags: ["activity"],
Expand Down
25 changes: 25 additions & 0 deletions api/externals/controller/activity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type ActivityController interface {
DestroyActivity(echo.Context) error
IndexActivityDetail(echo.Context) error
IndexActivityDetailsByPeriod(echo.Context) error
IndexFilteredActivityDetail(echo.Context) error
IndexFilteredActivityDetailByPeriod(echo.Context) error
}

func NewActivityController(u usecase.ActivityUseCase) ActivityController {
Expand Down Expand Up @@ -104,3 +106,26 @@ 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")
sponsorStyleIDs := c.QueryParams()["sponsor_style_id"]
keyword := c.QueryParam("keyword")
activities, err := a.u.GetFilteredActivityDetail(c.Request().Context(), isDone, sponsorStyleIDs, keyword)
if err != nil {
return err
}
return c.JSON(http.StatusOK, activities)
}

func (a *activityController) IndexFilteredActivityDetailByPeriod(c echo.Context) error {
isDone := c.QueryParam("is_done")
sponsorStyleIDs := c.QueryParams()["sponsor_style_id"]
year := c.Param("year")
keyword := c.QueryParam("keyword")
activities, err := a.u.GetFilteredActivityDetailByPeriod(c.Request().Context(), isDone, sponsorStyleIDs, year, keyword)
if err != nil {
return err
}
return c.JSON(http.StatusOK, activities)
}
161 changes: 161 additions & 0 deletions api/externals/repository/activity_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"context"
"database/sql"
"strings"

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
Expand All @@ -24,6 +25,8 @@ 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)
FindFilteredDetailByPeriod(context.Context, string, []string, string, string) (*sql.Rows, error)
}

func NewActivityRepository(c db.Client, ac abstract.Crud) ActivityRepository {
Expand Down Expand Up @@ -185,3 +188,161 @@ 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, sponsorStyleIDs []string, keyword string) (*sql.Rows, error) {
query := `
SELECT DISTINCT
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
activity_styles
ON
activities.id = activity_styles.activity_id
INNER JOIN
sponsor_styles
ON
activity_styles.sponsor_style_id = sponsor_styles.id`

optionQuery := ""

// keywordフィルタを追加
if keyword != "" {
if optionQuery != "" {
optionQuery += ` AND
sponsors.name LIKE '%` + keyword + `%'`
} else {
optionQuery += ` sponsors.name LIKE '%` + keyword + `%'`
}
}

// isDoneフィルタを追加
if isDone != "" {
if isDone != "all" {
if optionQuery != "" {
optionQuery += ` AND
activities.is_done = ` + isDone
} else {
optionQuery += ` activities.is_done = ` + isDone
}
}
}

// sponsorStyleIDsフィルタを追加
if len(sponsorStyleIDs) > 0 {
// プレースホルダーを生成
placeholders := strings.Join(sponsorStyleIDs, ",")
if optionQuery != "" {
optionQuery += ` AND
sponsor_styles.id IN (` + placeholders + `)`
} else {
optionQuery += ` sponsor_styles.id IN (` + placeholders + `)`
}
}

if optionQuery != "" {
query += (" WHERE" + optionQuery)
}

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

// activityに紐づくsponserとusersをフィルタを考慮して取得する
func (ar *activityRepository) FindFilteredDetailByPeriod(c context.Context, isDone string, sponsorStyleIDs []string, year string, keyword string) (*sql.Rows, error) {
query := `
SELECT DISTINCT
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
activity_styles
ON
activities.id = activity_styles.activity_id
INNER JOIN
sponsor_styles
ON
activity_styles.sponsor_style_id = sponsor_styles.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`

optionQuery := ""

// keywordフィルタを追加
if keyword != "" {
if optionQuery != "" {
optionQuery += ` AND
sponsors.name LIKE '%` + keyword + `%'`
} else {
optionQuery += ` sponsors.name LIKE '%` + keyword + `%'`
}
}

// isDoneフィルタを追加
if isDone != "" {
if isDone != "all" {
if optionQuery != "" {
optionQuery += ` AND
activities.is_done = ` + isDone
} else {
optionQuery += ` activities.is_done = ` + isDone
}
}
}

// sponsorStyleIDsフィルタを追加
if len(sponsorStyleIDs) > 0 {
// プレースホルダーを生成
placeholders := strings.Join(sponsorStyleIDs, ",")
if optionQuery != "" {
optionQuery += ` AND
sponsor_styles.id IN (` + placeholders + `)`
} else {
optionQuery += ` sponsor_styles.id IN (` + placeholders + `)`
}
}

// yearのフィルタ追加
if year != "" {
if optionQuery != "" {
optionQuery += ` AND
years.year = ` + year + " ORDER BY activities.updated_at DESC"
} else {
optionQuery += ` years.year = ` + year + " ORDER BY activities.updated_at DESC"
}
}

if optionQuery != "" {
query += (" WHERE" + optionQuery)
}

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

0 comments on commit 6152f32

Please sign in to comment.