-
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 18 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,41 @@ | ||
| 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 buildingId == "" { | ||
| return c.String(http.StatusBadRequest, "building_id is required") | ||
| } | ||
|
|
||
| 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 get campus donation by floor") | ||
| } | ||
|
|
||
| return c.JSON(http.StatusOK, campusDonationByFloors) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| 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("campus_donations"), | ||
| goqu.On(goqu.Ex{"campus_donations.teacher_id": goqu.I("teachers.id")}), | ||
| ). | ||
| Select( | ||
| goqu.I("buildings.id").As("building_id"), | ||
| goqu.I("buildings.name").As("building_name"), | ||
| goqu.I("floors.id").As("floor_id"), | ||
| goqu.I("floors.floor_number").As("floor_number"), | ||
| goqu.I("teachers.id").As("teacher_id"), | ||
| goqu.I("teachers.name").As("teacher_name"), | ||
| goqu.I("rooms.room_name").As("room_name"), | ||
| goqu.I("campus_donations.price").As("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. [ask] これって値入ってない時エラーならない?
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. あ、確かになるかも |
||
| goqu.I("teachers.is_black").As("is_black"), | ||
| ). | ||
| Where( | ||
| goqu.Ex{"buildings.id": buildingId, "floors.id": floorId}, | ||
| ). | ||
| Order( | ||
| goqu.I("building_units.unit_number").Asc(), | ||
| goqu.I("floors.floor_number").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,14 @@ | ||
| package domain | ||
|
|
||
| // CampusDonationRecord は、1行分のフラットな寄付データを表します | ||
| type CampusDonationRecord struct { | ||
| BuildingId int `json:"building_id"` | ||
| BuildingName *string `json:"building_name,omitempty"` | ||
| FloorId *int `json:"floor_id,omitempty"` | ||
| FloorNumber string `json:"floor_number"` | ||
| TeacherId int `json:"teacher_id"` | ||
| TeacherName string `json:"teacher_name"` | ||
| RoomName string `json:"room_name"` | ||
| Price int `json:"price"` | ||
| IsBlack *bool `json:"is_black,omitempty"` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| 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 { | ||
| GetCampusDonationByFloors(context.Context, string, string) ([]CampusDonationByFloorAndBuilding, error) | ||
| } | ||
|
|
||
| func NewCampusDonationUseCase(rep rep.CampusDonationRepository) CampusDonationUseCase { | ||
| return &campusDonationUseCase{rep} | ||
| } | ||
|
|
||
| func (cdu *campusDonationUseCase) GetCampusDonationByFloors(c context.Context, buildingId string, floorId string) ([]CampusDonationByFloorAndBuilding, error) { | ||
|
|
||
| //クエリ実行 | ||
| 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) | ||
| } | ||
| }() | ||
|
|
||
| var campusDonationRecords []domain.CampusDonationRecord | ||
| for rows.Next() { | ||
| var campusDonationRecord domain.CampusDonationRecord | ||
| if err := rows.Scan( | ||
| &campusDonationRecord.BuildingId, | ||
| &campusDonationRecord.BuildingName, | ||
| &campusDonationRecord.FloorId, | ||
| &campusDonationRecord.FloorNumber, | ||
| &campusDonationRecord.TeacherId, | ||
| &campusDonationRecord.TeacherName, | ||
| &campusDonationRecord.RoomName, | ||
| &campusDonationRecord.Price, | ||
| &campusDonationRecord.IsBlack, | ||
| ); err != nil { | ||
| return nil, errors.Wrap(err, "scanning flat campus donation record") | ||
| } | ||
| campusDonationRecords = append(campusDonationRecords, campusDonationRecord) | ||
| } | ||
|
|
||
| // --- (2)ネスト構造にマップ→スライス化 --- | ||
| groupMap := make(map[int]*generated.CampusDonationByFloorAndBuilding) | ||
| for _, r := range campusDonationRecords { | ||
|
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. [imo] 型の変換処置関数に切り分けても良さそう
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. slackで特定の棟をまとめるみたいなのあったけど、ああいうロジック周りを構造体に持たせてメソッドにするといい感じにドメインロジックを集約できそうだなって思った |
||
| buildingGroup, ok := groupMap[r.BuildingId] | ||
| if !ok { | ||
| buildingGroup = &generated.CampusDonationByFloorAndBuilding{ | ||
| BuildingId: r.BuildingId, | ||
| BuildingName: r.BuildingName, | ||
| Floors: []generated.FloorGroup{}, | ||
| } | ||
| groupMap[r.BuildingId] = buildingGroup | ||
| } | ||
| // Floor レベル取得 or 初期化 | ||
| var floorGroup *generated.FloorGroup | ||
| for i := range buildingGroup.Floors { | ||
| if buildingGroup.Floors[i].FloorId != nil && r.FloorId != nil && | ||
| *buildingGroup.Floors[i].FloorId == *r.FloorId { | ||
| floorGroup = &buildingGroup.Floors[i] | ||
| break | ||
| } | ||
| } | ||
| if floorGroup == nil { | ||
| newFg := generated.FloorGroup{ | ||
| FloorId: r.FloorId, | ||
| FloorNumber: r.FloorNumber, | ||
| Donations: []generated.CampusDonation{}, | ||
| } | ||
| buildingGroup.Floors = append(buildingGroup.Floors, newFg) | ||
| floorGroup = &buildingGroup.Floors[len(buildingGroup.Floors)-1] | ||
| } | ||
| // Donation 追加 | ||
| floorGroup.Donations = append(floorGroup.Donations, generated.CampusDonation{ | ||
| TeacherId: r.TeacherId, | ||
| TeacherName: r.TeacherName, | ||
| RoomName: r.RoomName, | ||
| Price: r.Price, | ||
| IsBlack: r.IsBlack, | ||
| }) | ||
| } | ||
| // map→slice | ||
|
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. このパッケージ使ってもいいかも
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. こんな感じなの??なんかうまく使えてない感じする
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. 良さそう |
||
| var result []generated.CampusDonationByFloorAndBuilding | ||
| for _, gb := range groupMap { | ||
| result = append(result, *gb) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| type CampusDonationByFloorAndBuilding = generated.CampusDonationByFloorAndBuilding | ||
| type CampusDonationRecord = domain.CampusDonationRecord | ||
Uh oh!
There was an error while loading. Please reload this page.