Beispiel #1
0
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()
}
Beispiel #2
0
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)
}
Beispiel #3
0
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
}