diff --git a/api/api/openapi.yml b/api/api/openapi.yml index 79f0391..f363bf2 100644 --- a/api/api/openapi.yml +++ b/api/api/openapi.yml @@ -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: "ユーザの追加" @@ -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: diff --git a/api/src/db.go b/api/src/db.go index 850f081..c3893f9 100644 --- a/api/src/db.go +++ b/api/src/db.go @@ -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"` } @@ -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 { @@ -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 +} diff --git a/api/src/main.go b/api/src/main.go index 2d0b8d4..c863256 100644 --- a/api/src/main.go +++ b/api/src/main.go @@ -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"` } @@ -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) @@ -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)