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

62 add a new endpoint to add shift data #68

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 47 additions & 0 deletions api/api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
summary: "シフトの追加"
description: "日付とそれに対応した複数loginをDBに反映する"
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/importShiftData'
required:
- shift
responses:
'200':
description: "成功。追加した日付一覧をjsonで返します"
content:
application/json:
schema:
$ref: '#/components/schemas/addedDate'
'400':
description: "失敗。エラーメッセージをjsonで返します"
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/users:
post:
summary: "ユーザの追加"
Expand Down Expand Up @@ -214,6 +238,29 @@ components:
properties:
Shift:
$ref: '#/components/schemas/Shift'
importShiftData:
type: array
items:
type: object
properties:
date:
type: string
format: date
example: "2024-05-01"
login:
type: array
items:
type: string
example: ["user1", "user2"]
example:
- date: "2024-05-01"
login: ["user1", "user2"]
- date: "2024-05-02"
login: ["user3", "user4"]
addedDate:
type: object
properties:
date: {type: array, example: ["2024-05-01", "2024-05-02"]}
ActivityData:
type: object
properties:
Expand Down
57 changes: 56 additions & 1 deletion api/src/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Shift struct {
ID uint `gorm:"primaryKey"`
Date string
UserID uint `json: "user_id"`
UserID int `json: "user_id"`
User User `gorm:"foreignKey:UserID"`
}

Expand Down Expand Up @@ -57,6 +57,10 @@ type Role struct {
Name string
}

type Date struct {
Date string
}

func initializeDB() (*gorm.DB, error) {
db, err := connectToDB()
if err != nil {
Expand Down Expand Up @@ -335,3 +339,54 @@ func addUidToExistUser(login string, uid string) bool {
}
return true
}

func addShiftToDB(schedule []Schedule) ([]string, error) {
db, err := connectToDB()
if err != nil {
panic("database error")
}

var addedDate []string
var flag bool

for _, s := range schedule {
if s.Date == "" || len(s.Login) == 0 {
continue
}
flag = false
for _, l := range s.Login {
// loginからUserIDを取得
userId, err := getUserIdFromLogin(db, l)
if err != nil {
return nil, err
}
// s.dateとloginが一致するシフトがすでに存在した場合はスキップ
var shift Shift
if err := db.Where("user_id = ? AND date = ?", userId, s.Date).First(&shift).Error; err != nil {
if err != gorm.ErrRecordNotFound {
return nil, err
}
// s.dateとlogin情報を追加
shift = Shift{Date: s.Date, UserID: userId}
if result := db.Create(&shift); result.Error != nil {
return nil, result.Error
}
flag = true
} else {
continue
}
}
if flag {
addedDate = append(addedDate, s.Date)
}
}
return addedDate, nil
}

func getUserIdFromLogin(db *gorm.DB, login string) (int, error) {
var user User
if err := db.Where("login = ?", login).First(&user).Error; err != nil {
return 0, err
}
return user.ID, nil
}
31 changes: 31 additions & 0 deletions api/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ type M5StickRequestData struct {
LocationName string `json:"location"`
}

type Schedule struct {
Date string `json:"date"`
Login []string `json:"login"`
}

type UserData struct {
IntraName string `json:"intra_name"`
}
Expand Down Expand Up @@ -85,6 +90,7 @@ func main() {
router.POST("/receive-uid", HandleUIDSubmission)

router.GET("/shift", getShiftData)
router.POST("/shift", addShiftData)

router.POST("/activities", addActivity)
router.GET("/activities/cleanings", getCleanData)
Expand Down Expand Up @@ -117,6 +123,31 @@ func getShiftData(c *gin.Context) {
c.JSON(http.StatusOK, shifts)
}

func addShiftData(c *gin.Context) {
var schedule []Schedule

if err := c.BindJSON(&schedule); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if len(schedule) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Shift is required"})
return
}
for _, s := range schedule {
log.Println(s.Date)
for _, l := range s.Login {
log.Println(l)
}
}
if date, err := addShiftToDB(schedule); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
} else {
c.JSON(http.StatusOK, gin.H{"date": date})
}
}

func getCleanData(c *gin.Context) {
//start_timeとend_timeを取得
start_time, end_time, err := getQueryAboutTime(c)
Expand Down
Loading