-
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
feat: 学内募金APIを追加 #1085
Changes from 1 commit
3d589e9
d50f1d6
96cdc4a
b76ccee
9884a74
68db011
bd46f7f
d15d5b0
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| ) | ||
|
|
||
| // router.GET(baseURL+"/campus_donations/years/:year/buildings/:building_id/floors/:floor_number", wrapper.GetCampusDonationsYearsYearBuildingsBuildingIdFloorsFloorNumber) | ||
| func (h *Handler) GetCampusDonationsYearsYearBuildingsBuildingIdFloorsFloorNumber( | ||
| c echo.Context, | ||
| year int, | ||
| buildingId int, | ||
| floorNumber string, | ||
| ) error { | ||
| buildingFloors, err := h.campusDonationUseCase.GetBuildingFloorDonationsByYear( | ||
| c.Request().Context(), | ||
| year, | ||
| buildingId, | ||
| floorNumber, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, buildingFloors) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
|
|
||
| "github.com/NUTFes/FinanSu/api/drivers/db" | ||
| goqu "github.com/doug-martin/goqu/v9" | ||
| ) | ||
|
|
||
| type campusDonationRepository struct { | ||
| client db.Client | ||
| } | ||
|
|
||
| type CampusDonationRepository interface { | ||
| GetBuildingFloorDonationsByYear(context.Context, int, int, string) (*sql.Rows, error) | ||
| } | ||
|
|
||
| func NewCampusDonationRepository(c db.Client) CampusDonationRepository { | ||
| return &campusDonationRepository{client: c} | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) GetBuildingFloorDonationsByYear( | ||
| ctx context.Context, | ||
| year int, | ||
| buildingID int, | ||
| 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")) | ||
|
|
||
| query, args, err := dialect. | ||
| From(goqu.T("buildings")). | ||
| InnerJoin( | ||
| goqu.T("buildings").As("selected_building"), | ||
| goqu.On( | ||
| goqu.I("buildings.name").Eq(goqu.I("selected_building.name")), | ||
|
Contributor
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. Joining on |
||
| goqu.I("selected_building.id").Eq(buildingID), | ||
| ), | ||
| ). | ||
| 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"), | ||
| ). | ||
| Where(goqu.I("rooms.floor_number").Eq(floorNumber)). | ||
| Order( | ||
| goqu.I("buildings.unit_number").Asc(), | ||
| goqu.I("rooms.room_name").Asc(), | ||
| goqu.I("teachers.name").Asc(), | ||
| ). | ||
| ToSQL() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return cdr.client.DB().QueryContext(ctx, query, args...) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package domain | ||
|
|
||
| type CampusDonationBuildingFloorRow struct { | ||
| BuildingID int | ||
| BuildingName string | ||
| UnitNumber int | ||
| FloorNumber string | ||
| RoomName string | ||
| TeacherID int | ||
| TeacherName string | ||
| TotalPrice int | ||
| IsBlack bool | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package usecase | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| rep "github.com/NUTFes/FinanSu/api/externals/repository" | ||
| "github.com/NUTFes/FinanSu/api/generated" | ||
| "github.com/NUTFes/FinanSu/api/internals/domain" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| type campusDonationUseCase struct { | ||
| rep rep.CampusDonationRepository | ||
| } | ||
|
|
||
| type CampusDonationUseCase interface { | ||
| GetBuildingFloorDonationsByYear(context.Context, int, int, string) ([]generated.CampusDonationBuildingFloor, error) | ||
| } | ||
|
|
||
| func NewCampusDonationUseCase(rep rep.CampusDonationRepository) CampusDonationUseCase { | ||
| return &campusDonationUseCase{rep: rep} | ||
| } | ||
|
|
||
| func (cdu *campusDonationUseCase) GetBuildingFloorDonationsByYear( | ||
| ctx context.Context, | ||
| year int, | ||
| buildingID int, | ||
| floorNumber string, | ||
| ) ([]generated.CampusDonationBuildingFloor, error) { | ||
| rows, err := cdu.rep.GetBuildingFloorDonationsByYear(ctx, year, buildingID, floorNumber) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| if err := rows.Close(); err != nil { | ||
| log.Println(err) | ||
| } | ||
| }() | ||
|
|
||
| buildingFloors := make([]generated.CampusDonationBuildingFloor, 0) | ||
| buildingIndexByID := make(map[int]int) | ||
|
|
||
| for rows.Next() { | ||
| var row domain.CampusDonationBuildingFloorRow | ||
| if err := rows.Scan( | ||
| &row.BuildingID, | ||
| &row.BuildingName, | ||
| &row.UnitNumber, | ||
| &row.FloorNumber, | ||
| &row.RoomName, | ||
| &row.TeacherID, | ||
| &row.TeacherName, | ||
| &row.TotalPrice, | ||
| &row.IsBlack, | ||
| ); err != nil { | ||
| return nil, errors.Wrap(err, "failed to scan campus donation building floor row") | ||
| } | ||
|
|
||
| index, ok := buildingIndexByID[row.BuildingID] | ||
| if !ok { | ||
| buildingFloors = append(buildingFloors, generated.CampusDonationBuildingFloor{ | ||
| BuildingId: row.BuildingID, | ||
| BuildingName: row.BuildingName, | ||
| UnitNumber: row.UnitNumber, | ||
| FloorNumber: row.FloorNumber, | ||
| Donations: []generated.CampusDonationTeacher{}, | ||
| }) | ||
| index = len(buildingFloors) - 1 | ||
| buildingIndexByID[row.BuildingID] = index | ||
| } | ||
|
|
||
| buildingFloors[index].Donations = append( | ||
| buildingFloors[index].Donations, | ||
| generated.CampusDonationTeacher{ | ||
| RoomName: row.RoomName, | ||
| TeacherId: row.TeacherID, | ||
| TeacherName: row.TeacherName, | ||
| TotalPrice: row.TotalPrice, | ||
| IsBlack: row.IsBlack, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| if err := rows.Err(); err != nil { | ||
| return nil, errors.Wrap(err, "failed to iterate campus donation building floors") | ||
| } | ||
|
|
||
| return buildingFloors, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| - id: 1 | ||
| name: 学内募金API確認棟 | ||
| unit_number: 1 | ||
|
|
||
| - id: 2 | ||
| name: 学内募金API確認棟 | ||
| unit_number: 2 | ||
|
|
||
| - id: 3 | ||
| name: 学内募金API確認別棟 | ||
| unit_number: 1 |
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.