Skip to content
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] 年度別で募金登録済みの教員Idを配列で返すAPIの作成 #818

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
21 changes: 20 additions & 1 deletion api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,26 @@ const docTemplate = `{
},
},
},
"/teachers/fundRegistered/{year}": {
"get": {
tags: ["teacher"],
"description": "募金登録済みのteacherのidを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "募金登録済みのteacherのidを取得",
}
}
},
},
"/users": {
"get": {
tags: ["user"],
Expand Down Expand Up @@ -2448,7 +2468,6 @@ const docTemplate = `{
"feature":{
"type": "string",
"example": "なし",

},
"expense":{
"type": "int",
Expand Down
11 changes: 11 additions & 0 deletions api/externals/controller/teacher_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type teacherController struct {

type TeacherController interface {
IndexTeacher(echo.Context) error
IndexFundRegisteredTeacher(echo.Context) error
ShowTeacher(echo.Context) error
CreateTeacher(echo.Context) error
UpdateTeacher(echo.Context) error
Expand All @@ -34,6 +35,16 @@ func (t *teacherController) IndexTeacher(c echo.Context) error {
return c.JSON(http.StatusOK, teachers)
}

// IndexFundRegistered
func (t *teacherController) IndexFundRegisteredTeacher(c echo.Context) error {
year := c.Param("year")
fundRegisteredTeachers, err := t.u.GetFundRegisteredByPeriods(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, fundRegisteredTeachers)
}

// Show
func (t *teacherController) ShowTeacher(c echo.Context) error {
id := c.Param("id")
Expand Down
28 changes: 28 additions & 0 deletions api/externals/repository/teacher_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type teacherRepository struct {

type TeacherRepository interface {
All(context.Context) (*sql.Rows, error)
AllFundRegistered(context.Context, string) (*sql.Rows, error)
Find(context.Context, string) (*sql.Row, error)
Create(context.Context, string, string, string, string, string, string) error
Update(context.Context, string, string, string, string, string, string, string) error
Expand All @@ -33,6 +34,33 @@ func (t *teacherRepository) All(c context.Context) (*sql.Rows, error) {
return t.crud.Read(c, query)
}

func (t *teacherRepository) AllFundRegistered(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
teachers.id
FROM
teachers
INNER JOIN
fund_informations
ON
teachers.id = fund_informations.teacher_id
INNER JOIN
year_periods
ON
fund_informations.created_at > year_periods.started_at
AND
fund_informations.created_at < year_periods.ended_at
INNER JOIN
years
ON
year_periods.year_id = years.id
WHERE
years.year = ` + year + `
ORDER BY
fund_informations.created_at ASC;`
return t.crud.Read(c, query)
}

func (t *teacherRepository) Find(c context.Context, id string) (*sql.Row, error) {
query := "SELECT * FROM teachers WHERE id = " + id
return t.crud.ReadByID(c, query)
Expand Down
24 changes: 24 additions & 0 deletions api/internals/usecase/teacher_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type teacherUseCase struct {

type TeacherUseCase interface {
GetTeachers(context.Context) ([]domain.Teacher, error)
GetFundRegisteredByPeriods(context.Context, string) ([]int, error)
GetTeacherByID(context.Context, string) (domain.Teacher, error)
CreateTeacher(context.Context, string, string, string, string, string, string) (domain.Teacher, error)
UpdateTeacher(context.Context, string, string, string, string, string, string, string) (domain.Teacher, error)
Expand Down Expand Up @@ -60,6 +61,29 @@ func (t *teacherUseCase) GetTeachers(c context.Context) ([]domain.Teacher, error
return teachers, nil
}

func (t *teacherUseCase) GetFundRegisteredByPeriods(c context.Context, year string) ([]int, error) {

rows, err := t.rep.AllFundRegistered(c, year)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}

if err := rows.Err(); err != nil {
return nil, err
}

return ids, nil
}

func (t *teacherUseCase) GetTeacherByID(c context.Context, id string) (domain.Teacher, error) {
var teacher domain.Teacher

Expand Down
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
// teacherのRoute
e.GET("/teachers", r.teacherController.IndexTeacher)
e.GET("/teachers/:id", r.teacherController.ShowTeacher)
e.GET("/teachers/fundRegistered/:year", r.teacherController.IndexFundRegisteredTeacher)
e.POST("/teachers", r.teacherController.CreateTeacher)
e.PUT("/teachers/:id", r.teacherController.UpdateTeacher)
e.DELETE("/teachers/:id", r.teacherController.DestroyTeacher)
Expand Down
Loading