Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0e24cbc
feat: 各階の教師データを取得するOAS定義
hikahana May 3, 2025
f8ffb0a
generated: 各棟の教師データを取得
hikahana May 3, 2025
c070fa4
fix: tags追加
hikahana May 3, 2025
4215d21
generated: description
hikahana May 3, 2025
09f14b4
feat: 各棟、各階の教師情報をパスクエリ形式で取得するAPIの作成
hikahana May 3, 2025
836d96f
fix: building_idの存在チェックを追加
hikahana May 12, 2025
4704f36
[wip]go-migration導入
May 12, 2025
9d2e24b
Revert "[wip]go-migration導入"
May 12, 2025
b74016a
refactor: campusDonationスキーマを更新し、棟ごとの教員情報を追加
hikahana May 12, 2025
048ceec
generated: /campus_donations/building/{building_id}/floor/{floor_id}周…
hikahana May 12, 2025
c69626e
feat: クエリ返り値用の型定義
hikahana May 12, 2025
9b45028
fix: fix error message
hikahana May 12, 2025
7503c95
refactor: クエリの選択項目を整理し、グループ化を削除
hikahana May 12, 2025
5b2c327
refactor: GetCampusDonationByFloorsメソッドを更新し、返り値の型を変更
hikahana May 12, 2025
6ee1b32
Merge branch 'feat/hikahana/get-rooms-by-building' of github:NUTFes/F…
hikahana May 12, 2025
538175a
Merge branch 'fix/hikahana/change-table-name-campus-donations' of git…
hikahana May 15, 2025
606ac41
Merge branch 'fix/hikahana/change-table-name-campus-donations' of git…
hikahana May 15, 2025
a0d1d9d
campus_donationsテーブルに基づいてfund_informationsの参照を更新
hikahana May 15, 2025
b64c089
Merge branch 'fix/hikahana/change-table-name-campus-donations' of git…
hikahana May 24, 2025
25534d4
fix: year_idを考慮する
hikahana May 24, 2025
eb1ff5d
generated: /campus_donations/year/{year_id}/building/{building_id}/fl…
hikahana May 24, 2025
79f58aa
fix: パス修正
hikahana May 24, 2025
5d859d4
refactor: 型変換を関数に切り分け
hikahana May 24, 2025
1f6fa2d
feat: add campus donation controller with endpoint for retrieving don…
hikahana May 25, 2025
3efc607
fix: handle NULL values in campus donations price with COALESCE
hikahana Jun 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions api/externals/controller/cmapus_donation_controller.go
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")
}
Comment thread
Kubosaka marked this conversation as resolved.

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)
}
79 changes: 79 additions & 0 deletions api/externals/repository/campus_donation_repository.go
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"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ask] これって値入ってない時エラーならない?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、確かになるかも
collapseみたいなのも一緒に消しちゃってた

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)

}
51 changes: 51 additions & 0 deletions api/generated/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/internals/di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func InitializeServer() (db.Client, *echo.Echo) {
transactionRepository := repository.NewTransactionRepository(client, crud)
userRepository := repository.NewUserRepository(client, crud)
yearRepository := repository.NewYearRepository(client, crud)
campusDonationRepository := repository.NewCampusDonationRepository(client, crud)
// ↓

// UseCase
Expand Down Expand Up @@ -99,6 +100,7 @@ func InitializeServer() (db.Client, *echo.Echo) {
teacherUseCase := usecase.NewTeacherUseCase(teacherRepository)
userUseCase := usecase.NewUserUseCase(userRepository, sessionRepository)
yearUseCase := usecase.NewYearUseCase(yearRepository)
campusDonationUseCase := usecase.NewCampusDonationUseCase(campusDonationRepository)
// ↓

// Controller
Expand Down Expand Up @@ -133,6 +135,7 @@ func InitializeServer() (db.Client, *echo.Echo) {
teacherController := controller.NewTeacherController(teacherUseCase)
userController := controller.NewUserController(userUseCase)
yearController := controller.NewYearController(yearUseCase)
campusDonationController := controller.NewCampusDonationController(campusDonationUseCase)
// ↓

// router
Expand Down Expand Up @@ -164,6 +167,7 @@ func InitializeServer() (db.Client, *echo.Echo) {
teacherController,
userController,
yearController,
campusDonationController,
)

// ↓
Expand Down
14 changes: 14 additions & 0 deletions api/internals/domain/campus_donation.go
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"`
}
105 changes: 105 additions & 0 deletions api/internals/usecase/campus_donation_usecase.go
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 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[imo] 型の変換処置関数に切り分けても良さそう
時間あればやってもろて

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このパッケージ使ってもいいかも
配列の処理とかいい感じに書ける
https://github.com/samber/lo?tab=readme-ov-file#maptoslice

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5d859d4

こんな感じなの??なんかうまく使えてない感じする

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
6 changes: 6 additions & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type router struct {
teacherController controller.TeacherController
userController controller.UserController
yearController controller.YearController
campusDonationController controller.CampusDonationController
}

type Router interface {
Expand Down Expand Up @@ -67,6 +68,7 @@ func NewRouter(
teacherController controller.TeacherController,
userController controller.UserController,
yearController controller.YearController,
campusDonationController controller.CampusDonationController,
) Router {
return router{
activityController,
Expand Down Expand Up @@ -96,6 +98,7 @@ func NewRouter(
teacherController,
userController,
yearController,
campusDonationController,
}
}

Expand Down Expand Up @@ -159,6 +162,9 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.GET("/buy_reports/details", r.buyReportController.IndexBuyReport)
e.PUT("/buy_report/status/:buy_report_id", r.buyReportController.UpdateBuyReportStatus)

// campus_donationsのRoute
e.GET("/campus_donations/building/:building_id/floor/:floor_id", r.campusDonationController.IndexCampusDonationByFloor)

// current_user
e.GET("/current_user", r.userController.GetCurrentUser)

Expand Down
Loading