func CreateSheet(body *music.Sheet, sheet *models.Sheet) { sheet.Name = body.Name sheet.Privilege = body.Privilege sheet.UserID = body.UserID sheet.CreatedAt = time.Now().Unix() sheet.UpdatedAt = time.Now().Unix() }
func (this *Sheet) Update(body *music.Sheet) datatype.Response { sheet := models.Sheet{} tx := this.DB.Begin() u := models.User{} if body.UserID == 0 { return badRequest("") } tx.Where("id = ?", strconv.Itoa(body.UserID)).First(&u) if u.ID == 0 { return forbidden("no such user") } tx.Where("user_id = ? AND name = ?", strconv.Itoa(u.ID), body.UpdateName).First(&sheet) if sheet.ID != 0 { return forbidden("no such sheet") } //One user cannot create sheet with same name tx.Where("user_id = ? AND name = ?", strconv.Itoa(u.ID), body.Name).First(&sheet) if sheet.ID == 0 { return forbidden("not this user's sheet") } if body.UpdateName != "" { sheet.Name = body.UpdateName } if body.Privilege != "" { sheet.Privilege = body.Privilege } tx.Save(&sheet) tx.Commit() return ok("updated", sheet) }
func (this *Audio) Update(body *music.Audio) datatype.Response { audio := models.Audio{} sheet := models.Sheet{} tx := this.DB.Begin() u := models.User{} if body.UserID == 0 { return badRequest("") } tx.Where("id = ?", strconv.Itoa(body.UserID)).First(&u) if u.ID == 0 { return forbidden("no such user") } tx.Where("audio_url = ?", body.AudioUrl).First(&audio) if audio.ID == 0 { return forbidden("no such audio") } tx.Where("id = ? AND user_id = ?", strconv.Itoa(audio.SheetID), strconv.Itoa(u.ID)).First(&sheet) if sheet.ID == 0 { return forbidden("not this user's audio") } if body.Name != "" { audio.Name = body.Name } if body.ImageUrl != "" { audio.ImageUrl = body.ImageUrl } //if body.Artist != ""{ // audio.Artist = body.Artist //} if body.SheetID != 0 { sheet.ID = 0 tx.Where("id = ? AND user_id = ?", strconv.Itoa(body.SheetID), strconv.Itoa(u.ID)).First(&sheet) if sheet.ID == 0 { return forbidden("the terminal sheet doesn't exist or belong to the request user!") } audio.SheetID = body.SheetID } tx.Save(&audio) tx.Commit() return ok("modify success", audio) }
func (this *SheetMigration) Migration(body *music.SheetMigration) datatype.Response { sheet := models.Sheet{} tx := this.DB.Begin() for _, id := range body.IdList { audio := models.Audio{} tx.Where("id = ?", id).First(&audio) tx.Where("id = ?", strconv.Itoa(body.ToSheetId)).First(&sheet) if sheet.ID == 0 || sheet.UserID != body.UserID { return forbidden("terminal sheet doesn't exist or not belong to user!") } // if the second select does not find , it will stay in origin, so need to reset sheetid to 1 sheet.ID = 0 tx.Where("id = ?", strconv.Itoa(audio.SheetID)).First(&sheet) fmt.Println(sheet.ID) if sheet.ID == 0 || sheet.UserID != body.UserID { return forbidden("the audio not belong to request user!") } audio.SheetID = body.ToSheetId tx.Save(&audio) } tx.Commit() return ok("success", "") }
func (this *Account) Register(body *user.Account) datatype.Response { var res datatype.Response //check mail reg := regexp.MustCompile(`^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$`) sel := [...]bool{true, true, true} u := models.User{} sheet := models.Sheet{} salt := models.Salts{} flag := 0 // begin transaction tx := this.DB.Begin() if body.Password == "" { goto BadRequest } if body.Name == "" || body.Mail == "" || body.Phone == "" { goto BadRequest } if reg.FindAllString(body.Mail, -1) == nil { goto BadRequest } if body.Name != "" { tx.Where("name=?", body.Name).First(&u) sel[0] = checkUser(u) } if body.Mail != "" { tx.Where("mail=?", body.Mail).First(&u) sel[1] = checkUser(u) } if body.Phone != "" { tx.Where("phone=?", body.Phone).First(&u) sel[2] = checkUser(u) } for i, v := range sel { if v { flag = i goto Forbidden } } CreateUser(&u, &salt, body) tx.Create(&u) //Create default sheet for user tx.Where("mail=?", body.Mail).First(&u) sheet.UserID = u.ID sheet.Name = "default#" + strconv.Itoa(u.ID) sheet.Privilege = "privacy" sheet.CreatedAt = time.Now().Unix() sheet.UpdatedAt = time.Now().Unix() tx.Create(&sheet) // transaction commit salt.UserID = u.ID tx.Create(&salt) tx.Commit() res = datatype.Response{ Status: http.StatusOK, Body: u, } return res BadRequest: res = datatype.Response{ Status: http.StatusBadRequest, } return res Forbidden: var resText string switch flag { case 0: resText = "Name existed" case 1: resText = "Mail existed" case 2: resText = "Phone existed" } res = datatype.Response{ Status: http.StatusForbidden, ResponseText: resText, } return res }