Ejemplo n.º 1
0
// LoginHandler calls upon the users repo to validate the login process
func (controller *UserController) LoginHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(models.LoginForm)
	repo := repos.UserRepo{Coll: controller.DB.C(userContextName)}
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	result := models.BaseResponse{}
	var user models.User
	user, err := repo.Login(body)

	if err != nil {
		result.Success = false
		result.Message = err.Error()
	} else {
		if len(user.Username) == 0 && len(user.Email) == 0 && len(user.Password) == 0 {
			result.Success = false
			result.Message = "Username or password incorrect."
		} else {
			result.Data = user
			result.Success = true
			result.Message = ""
		}
	}

	json.NewEncoder(w).Encode(result)
}
Ejemplo n.º 2
0
// GetLeaguesHandler calls upon swagger.Client to list all the published LoL leagues
func (controller *SwaggerController) GetLeaguesHandler(w http.ResponseWriter, r *http.Request) {
	swaggerClient := swagger.Client{}
	response := models.BaseResponse{}
	leagues, err := swaggerClient.GetLeages()
	if err != nil {
		response.Success = false
		response.Message = err.Error()
	} else {
		response.Success = true
		response.Message = ""
		response.Data = leagues
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	json.NewEncoder(w).Encode(response)
}
Ejemplo n.º 3
0
// CreateUserHandler calls upon the users repo to process the new user request
func (controller UserController) CreateUserHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*models.User)
	repo := repos.UserRepo{Coll: controller.DB.C(userContextName)}
	result := models.BaseResponse{}
	err := repo.Create(body)
	if err != nil {
		result.Success = false
		result.Message = err.Error()
	} else {
		result.Success = true
		result.Message = ""
		result.Data = body
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	json.NewEncoder(w).Encode(result)
}