示例#1
0
文件: update.go 项目: ts25504/MuShare
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)
}
示例#2
0
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", "")
}