-
Notifications
You must be signed in to change notification settings - Fork 1
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]フィルタありのactivitesのAPI実装 #812
Changes from 3 commits
afe460e
7e9db5b
a4f1ee5
069d5ca
bbb6bdb
a5f211a
bed0ae5
d25183b
d0aa597
34faead
98177fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
|
@@ -24,6 +25,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 { | ||||||
|
@@ -185,3 +187,56 @@ 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 | ||||||
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 | ||||||
WHERE | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
スペースを消してください |
||||||
1=1 ` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここっているんですかね? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回フィルタに使用している要素がすべて任意で外すことができるため、どの要素からWHEREが始まるかわからないため、1=1の常にTRUEの条件を設定し、そのあとにそれぞれのフィルタを加えるように実装しています。 |
||||||
|
||||||
// keywordフィルタを追加 | ||||||
if keyword != "" { | ||||||
query += ` AND | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
sponsors.name LIKE '%` + keyword + `%'` | ||||||
} | ||||||
|
||||||
// isDoneフィルタを追加 | ||||||
if isDone != "" { | ||||||
if isDone != "all" { | ||||||
query += ` AND | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
activities.is_done = ` + isDone | ||||||
} | ||||||
} | ||||||
|
||||||
// sponsorStyleIDsフィルタを追加 | ||||||
if len(sponsorStyleIDs) > 0 { | ||||||
// プレースホルダーを生成 | ||||||
placeholders := strings.Join(sponsorStyleIDs, ",") | ||||||
query += ` AND | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
sponsor_styles.id IN (` + placeholders + `)` | ||||||
} | ||||||
|
||||||
return ar.crud.Read(c, query) | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.