mirror of
https://github.com/elyby/chrly.git
synced 2024-12-25 22:50:14 +05:30
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"ely.by/chrly/internal/db"
|
|
"ely.by/chrly/internal/profiles"
|
|
)
|
|
|
|
type ProfilesManager interface {
|
|
PersistProfile(ctx context.Context, profile *db.Profile) error
|
|
RemoveProfileByUuid(ctx context.Context, uuid string) error
|
|
}
|
|
|
|
type ProfilesApi struct {
|
|
ProfilesManager
|
|
}
|
|
|
|
func (ctx *ProfilesApi) Handler() *mux.Router {
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
router.HandleFunc("/", ctx.postProfileHandler).Methods(http.MethodPost)
|
|
router.HandleFunc("/{uuid}", ctx.deleteProfileByUuidHandler).Methods(http.MethodDelete)
|
|
|
|
return router
|
|
}
|
|
|
|
func (ctx *ProfilesApi) postProfileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
err := req.ParseForm()
|
|
if err != nil {
|
|
apiBadRequest(resp, map[string][]string{
|
|
"body": {"The body of the request must be a valid url-encoded string"},
|
|
})
|
|
return
|
|
}
|
|
|
|
profile := &db.Profile{
|
|
Uuid: req.Form.Get("uuid"),
|
|
Username: req.Form.Get("username"),
|
|
SkinUrl: req.Form.Get("skinUrl"),
|
|
SkinModel: req.Form.Get("skinModel"),
|
|
CapeUrl: req.Form.Get("capeUrl"),
|
|
MojangTextures: req.Form.Get("mojangTextures"),
|
|
MojangSignature: req.Form.Get("mojangSignature"),
|
|
}
|
|
|
|
err = ctx.PersistProfile(req.Context(), profile)
|
|
if err != nil {
|
|
var v *profiles.ValidationError
|
|
if errors.As(err, &v) {
|
|
apiBadRequest(resp, v.Errors)
|
|
return
|
|
}
|
|
|
|
apiServerError(resp, fmt.Errorf("unable to save profile to db: %w", err))
|
|
return
|
|
}
|
|
|
|
resp.WriteHeader(http.StatusCreated)
|
|
}
|
|
|
|
func (ctx *ProfilesApi) deleteProfileByUuidHandler(resp http.ResponseWriter, req *http.Request) {
|
|
uuid := mux.Vars(req)["uuid"]
|
|
err := ctx.ProfilesManager.RemoveProfileByUuid(req.Context(), uuid)
|
|
if err != nil {
|
|
apiServerError(resp, fmt.Errorf("unable to delete profile from db: %w", err))
|
|
return
|
|
}
|
|
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
}
|