Beispiel #1
0
// GetUser accepts a request to retrieve a user by hostname
// and login from the datastore and return encoded in JSON
// format.
//
//     GET /api/users/:host/:login
//
func GetUser(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		user  = ToUser(c)
		host  = c.URLParams["host"]
		login = c.URLParams["login"]
	)

	user, err := datastore.GetUserLogin(ctx, host, login)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	json.NewEncoder(w).Encode(user)
}
Beispiel #2
0
// DeleteUser accepts a request to delete the specified
// user account from the system. A successful request will
// respond with an OK 200 status.
//
//     DELETE /api/users/:host/:login
//
func DelUser(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		user  = ToUser(c)
		host  = c.URLParams["host"]
		login = c.URLParams["login"]
	)

	account, err := datastore.GetUserLogin(ctx, host, login)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	if account.ID == user.ID {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	if err := datastore.DelUser(ctx, account); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
}