예제 #1
0
func (this *Schedules) Add(req *http.Request, authUser models.AuthUser, render render.Render) {
	var schedule models.Schedule

	if decode(req, render, &schedule) != nil {
		return
	}

	schedule.Id = 0
	schedule.UserId = authUser.Id

	errors := models.NewErrors()
	if err := schedule.Validate(this.db, errors); err != nil {
		render.Error(500)
		return
	}

	if errors.Count() > 0 {
		render.JSON(400, errors)
		return
	}

	if this.db.Save(&schedule).Error != nil {
		render.Error(500)
		return
	}
}
예제 #2
0
func (this *Users) Register(res http.ResponseWriter, req *http.Request, render render.Render) {
	var user models.RegisterUser
	if decode(req, render, &user) != nil {
		return
	}

	errors := models.NewErrors()
	if err := user.Validate(this.db, errors); err != nil {
		utils.HttpError(res, http.StatusInternalServerError)
		return
	}

	if errors.Count() > 0 {
		render.JSON(http.StatusBadRequest, map[string]*models.Errors{"errors": errors})
		return
	}

	if err := user.Geocode(); err != nil {
		utils.HttpError(res, http.StatusInternalServerError)
		return
	}

	if err := user.Register(this.db); err != nil {
		utils.HttpError(res, http.StatusInternalServerError)
		return
	}

	token, err := models.GenerateApiToken(this.db)
	if err != nil {
		utils.HttpError(res, http.StatusInternalServerError)
		return
	}

	user.Token = token
	if this.db.Model(user.User).UpdateColumn("token", token).Error != nil {
		utils.HttpError(res, http.StatusInternalServerError)
		return
	}

	user.Password = ""
	render.JSON(http.StatusOK, user)

	go this.sendVerificationCode(user.User)
}