Skip to content

Commit

Permalink
Merge pull request #669 from NUTFes/feat/hikahana/668-periods-fund-api
Browse files Browse the repository at this point in the history
[feat] 年度別のfund_informationsを取得するAPIの作成
  • Loading branch information
Kubosaka authored Feb 15, 2024
2 parents 95f20b0 + 9948ef6 commit f2bbb4e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,26 @@ const docTemplate = `{
}
},
},
"/fund_informations/details/{year}": {
"get": {
tags: ["fund_information"],
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
}
}
},
},
"/purchaseitems": {
"get": {
tags: ["purchase_item"],
Expand Down
11 changes: 11 additions & 0 deletions api/externals/controller/fund_information_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type FundInformationController interface {
DestroyFundInformation(echo.Context) error
IndexFundInformationDetails(echo.Context) error
ShowFundInformationDetailByID(echo.Context) error
IndexFundInformationDetailsByPeriod(echo.Context) error
}

func NewFundInformationController(u usecase.FundInformationUseCase) FundInformationController {
Expand Down Expand Up @@ -106,3 +107,13 @@ func (f *fundInformationController) ShowFundInformationDetailByID(c echo.Context
}
return c.JSON(http.StatusOK, fundinforuserandteacher)
}

// IndexFundInformationDetailsByPeriod
func (f *fundInformationController) IndexFundInformationDetailsByPeriod(c echo.Context) error {
year := c.Param("year")
fundInformationDetailsByPeriod, err := f.u.GetFundInformationDetailsByPeriod(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, fundInformationDetailsByPeriod)
}
43 changes: 41 additions & 2 deletions api/externals/repository/fund_information_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type FundInformationRepository interface {
FindDetails(context.Context) (*sql.Rows, error)
FindDetailByID(context.Context, string) (*sql.Row, error)
FindLatestRecord(context.Context) (*sql.Row, error)
AllDetailsByPeriod(context.Context, string) (*sql.Rows, error)
}

func NewFundInformationRepository(c db.Client, ac abstract.Crud) FundInformationRepository {
Expand Down Expand Up @@ -66,7 +67,7 @@ func (fir *fundInformationRepository) Create(
"," + price +
",'" + remark +
"'," + isFirstCheck +
"," + isLastCheck +
"," + isLastCheck +
"," + receivedAt + ")"
return fir.crud.UpdateDB(c, query)
}
Expand All @@ -93,7 +94,7 @@ func (fir *fundInformationRepository) Update(
", remark ='" + remark +
"', is_first_check = " + isFirstCheck +
", is_last_check = " + isLastCheck +
", received_at = " + receivedAt +
", received_at = " + receivedAt +
" WHERE id = " + id
return fir.crud.UpdateDB(c, query)
}
Expand Down Expand Up @@ -162,3 +163,41 @@ func (fir *fundInformationRepository) FindLatestRecord(c context.Context) (*sql.
`
return fir.crud.ReadByID(c, query)
}

// 年度別のfund_informationに紐づくuserとteacherを取得する
func (fir *fundInformationRepository) AllDetailsByPeriod(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
fund_informations.*,
users.*,
teachers.*,
departments.*
FROM
fund_informations
INNER JOIN
users
ON
fund_informations.user_id = users.id
INNER JOIN
teachers
ON
fund_informations.teacher_id = teachers.id
INNER JOIN
departments
ON
teachers.department_id = departments.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.id;"
return fir.crud.Read(c, query)
}
53 changes: 53 additions & 0 deletions api/internals/usecase/fund_information_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type FundInformationUseCase interface {
DestroyFundInformation(context.Context, string) error
GetFundInformationDetails(context.Context) ([]domain.FundInformationDetail, error)
GetFundInformationDetailByID(context.Context, string) (domain.FundInformationDetail, error)
GetFundInformationDetailsByPeriod(context.Context, string) ([]domain.FundInformationDetail, error)
}

func NewFundInformationUseCase(rep rep.FundInformationRepository) FundInformationUseCase {
Expand Down Expand Up @@ -194,6 +195,7 @@ func (f *fundInformationUseCase) GetFundInformationDetails(c context.Context) ([
return nil, err
}
fundInformationDetails = append(fundInformationDetails, fundInformationDetail)

}
return fundInformationDetails, nil
}
Expand Down Expand Up @@ -240,3 +242,54 @@ func (f *fundInformationUseCase) GetFundInformationDetailByID(c context.Context,
}
return fundInformationDetail, nil
}

//fund_informations_byyear-api(GETS)
func (f *fundInformationUseCase) GetFundInformationDetailsByPeriod(c context.Context, year string) ([]domain.FundInformationDetail, error) {
fundInformationDetail:= domain.FundInformationDetail{}
var fundInformationDetails []domain.FundInformationDetail

rows, err := f.rep.AllDetailsByPeriod(c, year)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
err := rows.Scan(
&fundInformationDetail.FundInformation.ID,
&fundInformationDetail.FundInformation.UserID,
&fundInformationDetail.FundInformation.TeacherID,
&fundInformationDetail.FundInformation.Price,
&fundInformationDetail.FundInformation.Remark,
&fundInformationDetail.FundInformation.IsFirstCheck,
&fundInformationDetail.FundInformation.IsLastCheck,
&fundInformationDetail.FundInformation.ReceivedAt,
&fundInformationDetail.FundInformation.CreatedAt,
&fundInformationDetail.FundInformation.UpdatedAt,
&fundInformationDetail.User.ID,
&fundInformationDetail.User.Name,
&fundInformationDetail.User.BureauID,
&fundInformationDetail.User.RoleID,
&fundInformationDetail.User.CreatedAt,
&fundInformationDetail.User.UpdatedAt,
&fundInformationDetail.Teacher.ID,
&fundInformationDetail.Teacher.Name,
&fundInformationDetail.Teacher.Position,
&fundInformationDetail.Teacher.DepartmentID,
&fundInformationDetail.Teacher.Room,
&fundInformationDetail.Teacher.IsBlack,
&fundInformationDetail.Teacher.Remark,
&fundInformationDetail.Teacher.CreatedAt,
&fundInformationDetail.Teacher.UpdatedAt,
&fundInformationDetail.Department.ID,
&fundInformationDetail.Department.Name,
&fundInformationDetail.Department.CreatedAt,
&fundInformationDetail.Department.UpdatedAt,
)
if err != nil {
return nil, err
}
fundInformationDetails = append(fundInformationDetails, fundInformationDetail)
}
return fundInformationDetails, nil
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.DELETE("/fund_informations/:id", r.fundInformationController.DestroyFundInformation)
e.GET("/fund_informations/details", r.fundInformationController.IndexFundInformationDetails)
e.GET("/fund_informations/:id/details", r.fundInformationController.ShowFundInformationDetailByID)
e.GET("/fund_informations/details/:year", r.fundInformationController.IndexFundInformationDetailsByPeriod)

// mail auth
e.POST("/mail_auth/signup", r.mailAuthController.SignUp)
Expand Down

0 comments on commit f2bbb4e

Please sign in to comment.