-
Notifications
You must be signed in to change notification settings - Fork 2
各棟の各階の教員情報を取得するAPI #987
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
base: develop
Are you sure you want to change the base?
各棟の各階の教員情報を取得するAPI #987
Changes from 5 commits
0e24cbc
f8ffb0a
c070fa4
4215d21
09f14b4
836d96f
4704f36
9d2e24b
b74016a
048ceec
c69626e
9b45028
7503c95
5b2c327
6ee1b32
538175a
606ac41
a0d1d9d
b64c089
25534d4
eb1ff5d
79f58aa
5d859d4
1f6fa2d
3efc607
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,37 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/NUTFes/FinanSu/api/internals/usecase" | ||
| "github.com/labstack/echo/v4" | ||
| ) | ||
|
|
||
| type campusDonationController struct { | ||
| u usecase.CampusDonationUseCase | ||
| } | ||
|
|
||
| type CampusDonationController interface { | ||
| IndexCampusDonationByFloor(echo.Context) error | ||
| } | ||
|
|
||
| func NewCampusDonationController(u usecase.CampusDonationUseCase) CampusDonationController { | ||
| return &campusDonationController{u} | ||
| } | ||
|
|
||
| func (cdc *campusDonationController) IndexCampusDonationByFloor(c echo.Context) error { | ||
| ctx := c.Request().Context() | ||
| buildingId := c.Param("building_id") | ||
| floorId := c.Param("floor_id") | ||
|
|
||
| if floorId == "" { | ||
| return c.String(http.StatusBadRequest, "floor_id is required") | ||
| } | ||
|
|
||
| campusDonationByFloors, err := cdc.u.GetCampusDonationByFloors(ctx, buildingId, floorId) | ||
| if err != nil { | ||
| return c.String(http.StatusBadRequest, "failed to buy_reports") | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, campusDonationByFloors) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "log" | ||
|
|
||
| "github.com/NUTFes/FinanSu/api/drivers/db" | ||
| "github.com/NUTFes/FinanSu/api/externals/repository/abstract" | ||
| goqu "github.com/doug-martin/goqu/v9" | ||
| ) | ||
|
|
||
| type campusDonationRepository struct { | ||
| client db.Client | ||
| crud abstract.Crud | ||
| } | ||
|
|
||
| type CampusDonationRepository interface { | ||
| AllCampusDonationByFloor(context.Context, string, string) (*sql.Rows, error) | ||
| } | ||
|
|
||
| func NewCampusDonationRepository(c db.Client, ac abstract.Crud) CampusDonationRepository { | ||
| return &campusDonationRepository{c, ac} | ||
| } | ||
|
|
||
| func (cdr *campusDonationRepository) AllCampusDonationByFloor(c context.Context, buildingId string, floorId string) (*sql.Rows, error) { | ||
|
|
||
| query, _, err := dialect.From("buildings"). | ||
| Join( | ||
| goqu.T("building_units"), | ||
| goqu.On(goqu.Ex{"building_units.building_id": goqu.I("buildings.id")}), | ||
| ). | ||
| Join( | ||
| goqu.T("floors"), | ||
| goqu.On(goqu.Ex{"floors.building_unit_id": goqu.I("building_units.id")}), | ||
| ). | ||
| Join( | ||
| goqu.T("rooms"), | ||
| goqu.On(goqu.Ex{"rooms.floor_id": goqu.I("floors.id")}), | ||
| ). | ||
| Join( | ||
| goqu.T("room_teachers"), | ||
| goqu.On(goqu.Ex{"room_teachers.room_id": goqu.I("rooms.id")}), | ||
| ). | ||
| Join( | ||
| goqu.T("teachers"), | ||
| goqu.On(goqu.Ex{"teachers.id": goqu.I("room_teachers.teacher_id")}), | ||
| ). | ||
| LeftJoin( | ||
| goqu.T("fund_informations"), | ||
| goqu.On(goqu.Ex{"fund_informations.teacher_id": goqu.I("teachers.id")}), | ||
| ). | ||
| Select( | ||
| goqu.I("buildings.name"), | ||
| goqu.I("building_units.unit_number"), | ||
| goqu.I("floors.floor_number"), | ||
| goqu.I("rooms.room_name"), | ||
| goqu.I("teachers.id"), | ||
| goqu.I("teachers.name"), | ||
| goqu.I("teachers.is_black"), | ||
| goqu.COALESCE(goqu.SUM(goqu.I("fund_informations.price")), 0), | ||
| ). | ||
| Where( | ||
| goqu.Ex{"buildings.id": buildingId, "floors.id": floorId}, | ||
| ). | ||
| GroupBy( | ||
| goqu.I("buildings.name"), | ||
| goqu.I("building_units.unit_number"), | ||
| goqu.I("floors.floor_number"), | ||
| goqu.I("rooms.room_name"), | ||
| goqu.I("teachers.id"), | ||
| goqu.I("teachers.name"), | ||
| goqu.I("teachers.is_black"), | ||
| ). | ||
|
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. [ask] このGroup Byってどういう意味で使ってる?
Collaborator
Author
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. なりましたね。priceをsumで返してたのでgroupByが必要になってたです。 |
||
| Order( | ||
| goqu.I("building_units.unit_number").Asc(), | ||
| goqu.I("floors.floor_number").Asc(), | ||
| goqu.I("rooms.room_name").Asc(), | ||
| goqu.I("teachers.name").Asc(), | ||
| ). | ||
| ToSQL() | ||
|
|
||
| if err != nil { | ||
| // エラーハンドリング | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| return cdr.crud.Read(c, query) | ||
|
|
||
| } | ||
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,59 @@ | ||
| package usecase | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| rep "github.com/NUTFes/FinanSu/api/externals/repository" | ||
| "github.com/NUTFes/FinanSu/api/generated" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| type campusDonationUseCase struct { | ||
| rep rep.CampusDonationRepository | ||
| } | ||
|
|
||
| type CampusDonationUseCase interface { | ||
| GetCampusDonationByFloors(context.Context, string, string) ([]CampusDonationByFloor, error) | ||
| } | ||
|
|
||
| func NewCampusDonationUseCase(rep rep.CampusDonationRepository) CampusDonationUseCase { | ||
| return &campusDonationUseCase{rep} | ||
| } | ||
|
|
||
| func (cdu *campusDonationUseCase) GetCampusDonationByFloors(c context.Context, buildingId string, floorId string) ([]CampusDonationByFloor, error) { | ||
| var campusDonation CampusDonationByFloor | ||
| var campusDonations []CampusDonationByFloor | ||
|
|
||
| //クエリ実行 | ||
| rows, err := cdu.rep.AllCampusDonationByFloor(c, buildingId, floorId) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| if err := rows.Close(); err != nil { | ||
| log.Println(err) | ||
| } | ||
| }() | ||
|
|
||
| for rows.Next() { | ||
| err := rows.Scan( | ||
| &campusDonation.BuildingName, | ||
| &campusDonation.UnitNumber, | ||
| &campusDonation.FloorNumber, | ||
| &campusDonation.RoomName, | ||
| &campusDonation.TeacherId, | ||
| &campusDonation.TeacherName, | ||
| &campusDonation.IsBlack, | ||
| &campusDonation.Price, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "can not connect SQL") | ||
| } | ||
| campusDonations = append(campusDonations, campusDonation) | ||
| } | ||
| return campusDonations, nil | ||
| } | ||
|
|
||
| type CampusDonationByFloor = generated.CampusDonationByFloor |
Uh oh!
There was an error while loading. Please reload this page.