2020-04-19 05:01:09 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-02-13 06:38:42 +05:30
|
|
|
"context"
|
2020-04-19 05:01:09 +05:30
|
|
|
"errors"
|
2024-02-07 18:54:41 +05:30
|
|
|
"fmt"
|
2020-04-19 05:01:09 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/db"
|
|
|
|
"ely.by/chrly/internal/profiles"
|
2020-04-19 05:01:09 +05:30
|
|
|
)
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
type ProfilesManager interface {
|
2024-02-13 06:38:42 +05:30
|
|
|
PersistProfile(ctx context.Context, profile *db.Profile) error
|
|
|
|
RemoveProfileByUuid(ctx context.Context, uuid string) error
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
type ProfilesApi struct {
|
2024-01-30 13:35:04 +05:30
|
|
|
ProfilesManager
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (ctx *ProfilesApi) Handler() *mux.Router {
|
2020-04-19 05:01:09 +05:30
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
2024-03-05 17:37:54 +05:30
|
|
|
router.HandleFunc("/", ctx.postProfileHandler).Methods(http.MethodPost)
|
|
|
|
router.HandleFunc("/{uuid}", ctx.deleteProfileByUuidHandler).Methods(http.MethodDelete)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (ctx *ProfilesApi) postProfileHandler(resp http.ResponseWriter, req *http.Request) {
|
2024-01-30 13:35:04 +05:30
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
apiBadRequest(resp, map[string][]string{
|
|
|
|
"body": {"The body of the request must be a valid url-encoded string"},
|
|
|
|
})
|
2020-04-19 05:01:09 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
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"),
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
2024-02-13 06:38:42 +05:30
|
|
|
err = ctx.PersistProfile(req.Context(), profile)
|
2024-01-30 13:35:04 +05:30
|
|
|
if err != nil {
|
|
|
|
var v *profiles.ValidationError
|
|
|
|
if errors.As(err, &v) {
|
|
|
|
apiBadRequest(resp, v.Errors)
|
|
|
|
return
|
2020-04-20 17:46:15 +05:30
|
|
|
}
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(resp, fmt.Errorf("unable to save profile to db: %w", err))
|
2024-01-30 13:35:04 +05:30
|
|
|
return
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:37:54 +05:30
|
|
|
func (ctx *ProfilesApi) deleteProfileByUuidHandler(resp http.ResponseWriter, req *http.Request) {
|
2024-01-30 13:35:04 +05:30
|
|
|
uuid := mux.Vars(req)["uuid"]
|
2024-02-13 06:38:42 +05:30
|
|
|
err := ctx.ProfilesManager.RemoveProfileByUuid(req.Context(), uuid)
|
2020-04-19 05:01:09 +05:30
|
|
|
if err != nil {
|
2024-02-07 18:54:41 +05:30
|
|
|
apiServerError(resp, fmt.Errorf("unable to delete profile from db: %w", err))
|
2020-04-19 05:01:09 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|