Skip to content

Commit

Permalink
Merge pull request #333 from yuminn-k/fix/get-all-attendance-by-id
Browse files Browse the repository at this point in the history
GetAttendanceByIDメソッドの修正: 指定IDの全ての出席情報を取得
  • Loading branch information
yuminn-k authored Jun 18, 2024
2 parents f1951a6 + 097b42d commit 090e186
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
10 changes: 5 additions & 5 deletions controllers/attendance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ac *AttendanceController) GetAllAttendances(ctx *gin.Context) {
// @Accept json
// @Produce json
// @Param id path int true "Attendance ID"
// @Success 200 {object} models.Attendance "Attendance"
// @Success 200 {array} models.Attendance "Attendance"
// @Failure 400 {string} string "無効なリクエスト"
// @Failure 500 {string} string "サーバーエラーが発生しました"
// @Router /at/attendance/{id} [get]
Expand All @@ -125,16 +125,16 @@ func (ac *AttendanceController) GetAttendance(ctx *gin.Context) {
return
}

attendance, err := ac.attendanceService.GetAttendanceByID(strconv.Itoa(int(attendanceID)))
attendances, err := ac.attendanceService.GetAttendanceByID(strconv.Itoa(int(attendanceID)))
if err != nil {
handleServiceError(ctx, err)
return
}
if attendance == nil {
respondWithError(ctx, constants.StatusNotFound, "Attendance not found")
if attendances == nil {
respondWithError(ctx, constants.StatusNotFound, "Attendances not found")
return
}
respondWithSuccess(ctx, constants.StatusOK, attendance)
respondWithSuccess(ctx, constants.StatusOK, attendances)
}

// DeleteAttendance godoc
Expand Down
5 changes: 4 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ const docTemplate = `{
"200": {
"description": "Attendance",
"schema": {
"$ref": "#/definitions/models.Attendance"
"type": "array",
"items": {
"$ref": "#/definitions/models.Attendance"
}
}
},
"400": {
Expand Down
5 changes: 4 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
"200": {
"description": "Attendance",
"schema": {
"$ref": "#/definitions/models.Attendance"
"type": "array",
"items": {
"$ref": "#/definitions/models.Attendance"
}
}
},
"400": {
Expand Down
4 changes: 3 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ paths:
"200":
description: Attendance
schema:
$ref: '#/definitions/models.Attendance'
items:
$ref: '#/definitions/models.Attendance'
type: array
"400":
description: 無効なリクエスト
schema:
Expand Down
8 changes: 4 additions & 4 deletions repositories/attendance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type AttendanceRepository interface {
CreateAttendance(attendance *models.Attendance) error
GetAttendanceByUIDAndCID(uid uint, cid uint) (*models.Attendance, error)
GetAllAttendancesByCID(cid uint) ([]models.Attendance, error)
GetAttendanceByID(id string) (*models.Attendance, error)
GetAttendanceByID(id string) ([]models.Attendance, error)
UpdateAttendance(attendance *models.Attendance) error
DeleteAttendance(id string) error
}
Expand Down Expand Up @@ -48,10 +48,10 @@ func (repo *attendanceRepository) GetAllAttendancesByCID(cid uint) ([]models.Att
}

// GetAttendanceByID IDによって出席情報を取得
func (repo *attendanceRepository) GetAttendanceByID(id string) (*models.Attendance, error) {
var attendance models.Attendance
func (repo *attendanceRepository) GetAttendanceByID(id string) ([]models.Attendance, error) {
var attendance []models.Attendance
err := repo.db.Where("csid = ?", id).Find(&attendance).Error
return &attendance, err
return attendance, err
}

// UpdateAttendance 出席情報を更新
Expand Down
4 changes: 2 additions & 2 deletions services/attendance_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type AttendanceService interface {
CreateOrUpdateAttendance(cid uint, uid uint, csid uint, status string) error
GetAllAttendancesByCID(cid uint) ([]models.Attendance, error)
GetAttendanceByID(id string) (*models.Attendance, error)
GetAttendanceByID(id string) ([]models.Attendance, error)
DeleteAttendance(id string) error
}

Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *attendanceService) GetAllAttendancesByCID(cid uint) ([]models.Attendanc
}

// GetAttendanceByID IDによって出席情報を取得
func (s *attendanceService) GetAttendanceByID(id string) (*models.Attendance, error) {
func (s *attendanceService) GetAttendanceByID(id string) ([]models.Attendance, error) {
attendance, err := s.repo.GetAttendanceByID(id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand Down

0 comments on commit 090e186

Please sign in to comment.