예제 #1
0
파일: users.go 프로젝트: byrnedo/usersvc
func (pC *UsersController) Replace(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	id := ps.ByName("userId")
	if !bson.IsObjectIdHex(id) {
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}

	decoder := json.NewDecoder(r.Body)
	var u webmsgspec.UpdatedUserResource

	if err := decoder.Decode(&u); err != nil {
		Error.Println(err)
		panic("Failed to decode json:" + err.Error())
	}

	u.Data.ID = id

	if valErrs := validate.ValidateStruct(u); len(valErrs) != 0 {
		errResponse := svcSpec.NewValidationErrorResonse(valErrs)
		pC.ServeWithStatus(w, errResponse, 400)
		return
	}

	inserted, err := pC.userModel.Replace(u.Data)
	if err != nil {
		Error.Println("Error updating user:" + err.Error())
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(500), 500)
		return
	}
	pC.ServeWithStatus(w, inserted, 200)
}
예제 #2
0
파일: users.go 프로젝트: byrnedo/usersvc
func (c *UsersController) Authenticate(subj string, reply string, data *mqmsgspec.InnerAuthenticateUserRequest) {
	Info.Println("Got authenticate request:", data)

	var valid bool
	if valErrs := validate.ValidateStruct(data); len(valErrs) != 0 {
		for key, fieldErr := range valErrs {
			Error.Println("Validation failed:", key, ":", fieldErr.Tag)

		}
		valid = false
	} else {
		err := c.userModel.Authenticate(data.GetUsername(), data.GetPassword())

		if err == nil {
			valid = true
			Info.Println("Authentication successful")
		} else {
			valid = false
			Info.Println("Authentication failed:", err)
		}
	}

	response := mqmsgspec.NewAuthenticateUserResponse(&mqmsgspec.InnerAuthenticateUserResponse{Authenticated: &valid})
	if err := c.natsCon.Publish(reply, data.GetContext(), response); err != nil {
		Error.Println("Error sending reply:" + err.Error())
	}
}
예제 #3
0
파일: users.go 프로젝트: byrnedo/usersvc
func (pC *UsersController) Create(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	decoder := json.NewDecoder(r.Body)
	var u webmsgspec.NewUserResource

	if err := decoder.Decode(&u); err != nil {
		Error.Println(err)
		panic("Failed to decode json:" + err.Error())
	}

	if valErrs := validate.ValidateStruct(u); len(valErrs) != 0 {
		errResponse := svcSpec.NewValidationErrorResonse(valErrs)
		pC.ServeWithStatus(w, errResponse, 400)
		return
	}

	inserted, err := pC.userModel.Create(u.Data)
	if err != nil {
		Error.Println("Error creating user:" + err.Error())
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(500), 500)
		return
	}
	pC.ServeWithStatus(w, inserted, 201)
}