-
Notifications
You must be signed in to change notification settings - Fork 2
各棟ごとの合計募金額を取得するAPIの作成 #986
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: feat/hikahana/get-rooms-by-building
Are you sure you want to change the base?
Changes from 24 commits
5414852
75405b6
034c108
090464d
c9bd752
e15837d
6566275
ecc2d2e
5a5fd28
e2384e4
cfa968a
318ba7c
63262c4
3a658a9
942562f
bf18310
1ffe7f0
dc21071
a0cccf1
b5c4ed3
65f1478
296d728
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,48 @@ | ||
| 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 | ||
| IndexCampusDonationBuildingByPeriod(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) | ||
| } | ||
|
|
||
| func (f *campusDonationController) IndexCampusDonationBuildingByPeriod(c echo.Context) error { | ||
| ctx := c.Request().Context() | ||
| year := c.Param("year") | ||
| fundInformationBuildingByPeriod, err := f.u.GetCampusDonationBuildingByPeriod(ctx, year) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return c.JSON(http.StatusOK, fundInformationBuildingByPeriod) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| 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) | ||
| AllBuildingsByPeriod(context.Context, 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.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("campus_donations.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"), | ||
| ). | ||
| 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) | ||
|
|
||
| } | ||
|
|
||
| func (fir *campusDonationRepository) AllBuildingsByPeriod(c context.Context, year string) (*sql.Rows, error) { | ||
|
|
||
| db := goqu.Dialect("mysql") | ||
|
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. dialectはどっかで定義してると思う |
||
|
|
||
| ds := db.From("campus_donations"). | ||
| Select( | ||
| goqu.I("buildings.id"), | ||
| goqu.I("buildings.name"), | ||
| goqu.I("campus_donations.price"), | ||
|
Comment on lines
+86
to
+88
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. sumとか集計関数つかったらusecaseの処理楽になるのかな、まあどっちでも
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. 棟ごとに分けるの実装するのめんどみがあるからusecaseで処理させます |
||
| ). | ||
| Join(goqu.T("teachers"), goqu.On(goqu.Ex{ | ||
| "campus_donations.teacher_id": goqu.I("teachers.id"), | ||
| })). | ||
| Join(goqu.T("users"), goqu.On(goqu.Ex{ | ||
| "campus_donations.user_id": goqu.I("users.id"), | ||
| })). | ||
| Join(goqu.T("departments"), goqu.On(goqu.Ex{ | ||
| "teachers.department_id": goqu.I("departments.id"), | ||
| })). | ||
| Join(goqu.T("room_teachers"), goqu.On(goqu.Ex{ | ||
| "room_teachers.teacher_id": goqu.I("teachers.id"), | ||
| })). | ||
| Join(goqu.T("rooms"), goqu.On(goqu.Ex{ | ||
| "room_teachers.room_id": goqu.I("rooms.id"), | ||
| })). | ||
| Join(goqu.T("floors"), goqu.On(goqu.Ex{ | ||
| "rooms.floor_id": goqu.I("floors.id"), | ||
| })). | ||
| Join(goqu.T("building_units"), goqu.On(goqu.Ex{ | ||
| "floors.building_unit_id": goqu.I("building_units.id"), | ||
| })). | ||
| Join(goqu.T("buildings"), goqu.On(goqu.Ex{ | ||
| "building_units.building_id": goqu.I("buildings.id"), | ||
| })). | ||
| Join(goqu.T("year_periods"), | ||
| goqu.On(goqu.And( | ||
| goqu.I("campus_donations.created_at").Gt(goqu.I("year_periods.started_at")), | ||
| goqu.I("campus_donations.created_at").Lt(goqu.I("year_periods.ended_at")), | ||
| )), | ||
| ). | ||
| Join(goqu.T("years"), goqu.On(goqu.Ex{ | ||
| "year_periods.year_id": goqu.I("years.id"), | ||
| })). | ||
| Where(goqu.Ex{"years.year": year}). | ||
| Order(goqu.I("campus_donations.updated_at").Desc()) | ||
|
|
||
| query, _, err := ds.ToSQL() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| return fir.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,7 @@ | ||
| package domain | ||
|
|
||
| type CampusDonationBuilding struct { | ||
| Id int `json:"id"` | ||
| Name string `json:"name"` | ||
| Price int `json:"price"` | ||
| } |
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.
file名タイポってる
cmapus_donation_controller.go