Esempio n. 1
0
func DeleteUser(w http.ResponseWriter, r *http.Request) {
	user := model.User{}
	user.UserID = context.Get(r, "user_id").(uint64)
	err := user.Get()
	if err != nil {
		reply.Err(w, ae.DB("", err))
	}
	err = user.Delete()
	if err != nil {
		reply.Err(w, ae.DB("", err))
	}
	reply.OK(w, user)
}
Esempio n. 2
0
func UpdateUser(w http.ResponseWriter, r *http.Request) {
	userID := context.Get(r, "user_id").(uint64)

	reqBody := userReqPutBody{}
	appErr := decode(r, &reqBody)
	if appErr != nil {
		reply.Err(w, appErr)
		return
	}

	user := model.User{UserID: userID}
	if err := user.Get(); err != nil {
		if err == sql.ErrNoRows {
			reply.Err(w, ae.ResourceNotFound(""))
			return
		}
		reply.Err(w, ae.DB("", err))
		return
	}

	user.FirstName = reqBody.FirstName
	user.LastName = reqBody.LastName
	user.ProfilePicURL = reqBody.ProfilePicURL
	user.MetricSystem = reqBody.MetricSystem

	if err := user.Save(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	if err := user.Get(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	reply.OK(w, user)
	return
}
Esempio n. 3
0
func GetUser(w http.ResponseWriter, r *http.Request) {
	userID := context.Get(r, "user_id").(uint64)
	var err error

	urlParams := context.Get(r, "params").(httprouter.Params)
	id := urlParams.ByName("id")
	if len(id) != 0 {
		userID, err = strconv.ParseUint(id, 10, 64)
		if err != nil {
			reply.Err(w, ae.InvalidInput("id is not a number", "id"))
		}
	}

	user := model.User{}
	user.UserID = userID
	err = user.Get()
	if err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	reply.OK(w, user)
}