Example #1
0
func CreateUser(user models.User) (int, createResponse) {
	user.Id = uuid.NewV4().String()
	user.Password = Hash(user.Password)
	result, err := db.UserTable.Insert(user).RunWrite(db.Session)
	// For some ungodly strange reason rethink thinks returning errors on multiple insertions are better in the
	// results, than in the actual err variable. Documented here: https://github.com/rethinkdb/rethinkdb/issues/899
	if err != nil || result.Errors > 0 {
		firstError := errors.New(result.FirstError)
		if r.IsConflictErr(err) || r.IsConflictErr(firstError) {
			return http.StatusConflict, createResponse{userAlreadyExists, nil}
		}
		panic(err)
	}
	return http.StatusCreated, createResponse{userCreated, &user}
}