feat(auth): implement user registration endpoint
This commit is contained in:
42
internal/api/user_handler.go
Normal file
42
internal/api/user_handler.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/core"
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
Service *core.UserService
|
||||
}
|
||||
|
||||
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
var req models.RegisterRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.Service.RegisterNewUser(r.Context(), req)
|
||||
if err != nil {
|
||||
// Определяем, какую ошибку вернуть клиенту
|
||||
switch {
|
||||
case errors.Is(err, database.ErrUserExists):
|
||||
http.Error(w, err.Error(), http.StatusConflict) // 409
|
||||
case errors.Is(err, core.ErrInvalidUsername), errors.Is(err, core.ErrInvalidEmail), errors.Is(err, core.ErrPasswordTooShort):
|
||||
http.Error(w, err.Error(), http.StatusBadRequest) // 400
|
||||
default:
|
||||
// Логируем внутреннюю ошибку, но не показываем ее клиенту
|
||||
// log.Printf("internal server error: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError) // 500
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Шаг 11 из ТЗ: Возвращаем 201 Created
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
Reference in New Issue
Block a user