-
Notifications
You must be signed in to change notification settings - Fork 2
feat: 学内募金APIを追加 #1085
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
Merged
hikahana
merged 8 commits into
develop
from
feat/hikahana/campus-donation-building-floors-api
May 5, 2026
Merged
feat: 学内募金APIを追加 #1085
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3d589e9
feat: 各棟の各階の学内募金教員情報取得APIを追加
d50f1d6
キャンパス募金のフロア集計でOpenAPI生成型を使用
96cdc4a
fix: 棟階別募金集計の集約処理にテストを追加
b76ccee
fix: 学内募金階別APIをgroup_key指定に変更
9884a74
feat: 学内募金の登録更新APIを追加
68db011
fix: 学内募金の未回収金額をnullで返す
bd46f7f
merge: developを取り込みコンフリクトを解消
d15d5b0
formatted by workflow
hikahana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "errors" | ||
| "net/http" | ||
|
|
||
| "github.com/NUTFes/FinanSu/api/generated" | ||
| "github.com/labstack/echo/v4" | ||
| ) | ||
|
|
||
| // router.POST(baseURL+"/campus_donations", wrapper.PostCampusDonations) | ||
| func (h *Handler) PostCampusDonations(c echo.Context) error { | ||
| var request generated.PostCampusDonationsJSONRequestBody | ||
| if err := c.Bind(&request); err != nil { | ||
| return c.JSON(http.StatusBadRequest, "failed to bind") | ||
| } | ||
|
|
||
| campusDonation, err := h.campusDonationUseCase.CreateCampusDonation(c.Request().Context(), request) | ||
| if err != nil { | ||
| return c.JSON(http.StatusInternalServerError, err.Error()) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, campusDonation) | ||
| } | ||
|
|
||
| // router.GET(baseURL+"/campus_donations/years/:year/group_keys/:group_key/floors", wrapper.GetCampusDonationsYearsYearGroupKeysGroupKeyFloors) | ||
| func (h *Handler) GetCampusDonationsYearsYearGroupKeysGroupKeyFloors( | ||
| c echo.Context, | ||
| year int, | ||
| groupKey generated.CampusDonationBuildingGroupKey, | ||
| params generated.GetCampusDonationsYearsYearGroupKeysGroupKeyFloorsParams, | ||
| ) error { | ||
| groupKeyValue := string(groupKey) | ||
|
|
||
| buildingFloors, err := h.campusDonationUseCase.GetBuildingFloorDonationsByYear( | ||
| c.Request().Context(), | ||
| year, | ||
| &groupKeyValue, | ||
| params.FloorNumber, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, buildingFloors) | ||
| } | ||
|
|
||
| // router.PUT(baseURL+"/campus_donations/:id", wrapper.PutCampusDonationsId) | ||
| func (h *Handler) PutCampusDonationsId(c echo.Context, id int) error { | ||
| var request generated.PutCampusDonationsIdJSONRequestBody | ||
| if err := c.Bind(&request); err != nil { | ||
| return c.JSON(http.StatusBadRequest, "failed to bind") | ||
| } | ||
|
|
||
| campusDonation, err := h.campusDonationUseCase.UpdateCampusDonation(c.Request().Context(), id, request) | ||
| if err != nil { | ||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return c.JSON(http.StatusNotFound, "campus donation not found") | ||
| } | ||
| return c.JSON(http.StatusInternalServerError, err.Error()) | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, campusDonation) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
|
|
||
| "github.com/NUTFes/FinanSu/api/drivers/db" | ||
| "github.com/NUTFes/FinanSu/api/internals/domain" | ||
| goqu "github.com/doug-martin/goqu/v9" | ||
| ) | ||
|
|
||
| type campusDonationRepository struct { | ||
| client db.Client | ||
| } | ||
|
|
||
| type CampusDonationRepository interface { | ||
| Create(context.Context, domain.CampusDonation) (int, error) | ||
| Update(context.Context, int, domain.CampusDonation) error | ||
| FindByID(context.Context, int) (domain.CampusDonation, error) | ||
| GetBuildingFloorDonationsByYear(context.Context, int, *string, *string) (*sql.Rows, error) | ||
| } | ||
|
|
||
| func NewCampusDonationRepository(c db.Client) CampusDonationRepository { | ||
| return &campusDonationRepository{client: c} | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) Create(ctx context.Context, donation domain.CampusDonation) (int, error) { | ||
| ds := dialect. | ||
| Insert(goqu.T("campus_donations")). | ||
| Rows(goqu.Record{ | ||
| "user_id": donation.UserID, | ||
| "teacher_id": donation.TeacherID, | ||
| "year_id": donation.YearID, | ||
| "price": donation.Price, | ||
| "received_at": donation.ReceivedAt, | ||
| }) | ||
|
|
||
| query, args, err := ds.ToSQL() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| result, err := cdr.client.DB().ExecContext(ctx, query, args...) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| id, err := result.LastInsertId() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| return int(id), nil | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) Update(ctx context.Context, id int, donation domain.CampusDonation) error { | ||
| ds := dialect. | ||
| Update(goqu.T("campus_donations")). | ||
| Set(goqu.Record{ | ||
| "user_id": donation.UserID, | ||
| "teacher_id": donation.TeacherID, | ||
| "year_id": donation.YearID, | ||
| "price": donation.Price, | ||
| "received_at": donation.ReceivedAt, | ||
| }). | ||
| Where(goqu.I("id").Eq(id)) | ||
|
|
||
| query, args, err := ds.ToSQL() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = cdr.client.DB().ExecContext(ctx, query, args...) | ||
| return err | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) FindByID(ctx context.Context, id int) (domain.CampusDonation, error) { | ||
| ds := selectCampusDonationQuery().Where(goqu.I("campus_donations.id").Eq(id)).Limit(1) | ||
|
|
||
| query, args, err := ds.ToSQL() | ||
| if err != nil { | ||
| return domain.CampusDonation{}, err | ||
| } | ||
|
|
||
| var donation domain.CampusDonation | ||
| err = cdr.client.DB().QueryRowContext(ctx, query, args...).Scan( | ||
| &donation.ID, | ||
| &donation.UserID, | ||
| &donation.TeacherID, | ||
| &donation.YearID, | ||
| &donation.Price, | ||
| &donation.ReceivedAt, | ||
| ) | ||
| if err != nil { | ||
| return domain.CampusDonation{}, err | ||
| } | ||
|
|
||
| return donation, nil | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) GetBuildingFloorDonationsByYear( | ||
| ctx context.Context, | ||
| year int, | ||
| groupKey *string, | ||
| floorNumber *string, | ||
| ) (*sql.Rows, error) { | ||
| donationTotalsByTeacher := dialect. | ||
| Select( | ||
| goqu.I("campus_donations.teacher_id").As("teacher_id"), | ||
| goqu.SUM(goqu.I("campus_donations.price")).As("total_price"), | ||
| ). | ||
| From(goqu.T("campus_donations")). | ||
| InnerJoin( | ||
| goqu.T("years"), | ||
| goqu.On(goqu.I("campus_donations.year_id").Eq(goqu.I("years.id"))), | ||
| ). | ||
| Where(goqu.I("years.year").Eq(year)). | ||
| GroupBy(goqu.I("campus_donations.teacher_id")) | ||
|
|
||
| queryDataset := dialect. | ||
| From(goqu.T("buildings")). | ||
| InnerJoin( | ||
| goqu.T("rooms"), | ||
| goqu.On(goqu.I("rooms.building_id").Eq(goqu.I("buildings.id"))), | ||
| ). | ||
| InnerJoin( | ||
| goqu.T("room_teachers"), | ||
| goqu.On(goqu.I("room_teachers.room_id").Eq(goqu.I("rooms.id"))), | ||
| ). | ||
| InnerJoin( | ||
| goqu.T("teachers"), | ||
| goqu.On(goqu.I("teachers.id").Eq(goqu.I("room_teachers.teacher_id"))), | ||
| ). | ||
| LeftJoin( | ||
| donationTotalsByTeacher.As("donation_totals"), | ||
| goqu.On(goqu.I("donation_totals.teacher_id").Eq(goqu.I("teachers.id"))), | ||
| ). | ||
| Select( | ||
| goqu.I("buildings.id").As("building_id"), | ||
| goqu.I("buildings.name").As("building_name"), | ||
| goqu.I("buildings.unit_number").As("unit_number"), | ||
| goqu.I("rooms.floor_number").As("floor_number"), | ||
| goqu.I("rooms.room_name").As("room_name"), | ||
| goqu.I("teachers.id").As("teacher_id"), | ||
| goqu.I("teachers.name").As("teacher_name"), | ||
| goqu.COALESCE(goqu.I("donation_totals.total_price"), 0).As("total_price"), | ||
|
Collaborator
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. 仕様書の みたいに回収済みの0円なのか未回収なのかわかるようにしたいからnullのままはどう? |
||
| goqu.COALESCE(goqu.I("teachers.is_black"), false).As("is_black"), | ||
| ). | ||
| Order( | ||
| goqu.I("buildings.unit_number").Asc(), | ||
| goqu.I("buildings.id").Asc(), | ||
| goqu.I("rooms.floor_number").Asc(), | ||
| goqu.I("rooms.room_name").Asc(), | ||
| goqu.I("teachers.name").Asc(), | ||
| ) | ||
|
|
||
| if groupKey != nil && *groupKey != "" { | ||
| queryDataset = queryDataset.Where(goqu.I("buildings.group_key").Eq(*groupKey)) | ||
| } | ||
|
|
||
| if floorNumber != nil && *floorNumber != "" { | ||
| queryDataset = queryDataset.Where(goqu.I("rooms.floor_number").Eq(*floorNumber)) | ||
| } | ||
|
|
||
| query, args, err := queryDataset.ToSQL() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return cdr.client.DB().QueryContext(ctx, query, args...) | ||
| } | ||
|
|
||
| func selectCampusDonationQuery() *goqu.SelectDataset { | ||
| return dialect. | ||
| From(goqu.T("campus_donations")). | ||
| Select( | ||
| goqu.I("campus_donations.id").As("id"), | ||
| goqu.I("campus_donations.user_id").As("user_id"), | ||
| goqu.I("campus_donations.teacher_id").As("teacher_id"), | ||
| goqu.I("campus_donations.year_id").As("year_id"), | ||
| goqu.I("campus_donations.price").As("price"), | ||
| goqu.L("DATE_FORMAT(campus_donations.received_at, '%Y-%m-%d')").As("received_at"), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The subquery
donationTotalsByTeacherfilters by year but does not filter by building or floor. While this is logically correct because it's joined later onteacher_id, it might be more efficient to include building/floor filters in the subquery if thecampus_donationstable is very large, to reduce the number of rows processed in the aggregation.