Exemplo n.º 1
0
func (pC *UsersController) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	var (
		id    string
		err   error
		objId bson.ObjectId
		user  *models.UserModel
	)

	id = ps.ByName("userId")
	if !bson.IsObjectIdHex(id) {
		Error.Println("Id is not object id")
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}

	objId = bson.ObjectIdHex(id)

	if user, err = pC.userModel.Find(objId); err != nil {
		Error.Println("Failed to find user:" + err.Error())
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}

	pC.Serve(w, &webmsgspec.UserResource{user})
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
func (pC *UsersController) Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

	id := ps.ByName("userId")

	if !bson.IsObjectIdHex(id) {
		Error.Println("Not an object id:", id)
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}

	if err := pC.userModel.Delete(bson.ObjectIdHex(id)); err != nil {
		Error.Println("Error deleting:", err)
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}

	pC.Serve(w, &webmsgspec.UsersResource{nil})
}
Exemplo n.º 4
0
func (pC *UsersController) List(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	query := pC.QueryInterfaceMap(r, "query", &models.UserModel{})
	order, _ := r.URL.Query()["order"]
	offset, _ := pC.QueryInt(r, "offset")
	limit, _ := pC.QueryInt(r, "limit")

	users, err := pC.userModel.FindMany(query, order, offset, limit)
	if err != nil {
		Error.Println("Failed to find users:", err)
		pC.ServeWithStatus(w, svcSpec.NewErrorResponse().AddCodeError(404), 404)
		return
	}
	pC.Serve(w, &webmsgspec.UsersResource{users})
}
Exemplo n.º 5
0
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)
}