37 lines
755 B
Go
37 lines
755 B
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/zs/InsightReply/internal/service"
|
|
)
|
|
|
|
type UserHandler struct {
|
|
svc *service.UserService
|
|
}
|
|
|
|
func NewUserHandler(svc *service.UserService) *UserHandler {
|
|
return &UserHandler{svc: svc}
|
|
}
|
|
|
|
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Email string `json:"email"`
|
|
Identity string `json:"identity"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
user, err := h.svc.Register(body.Email, body.Identity)
|
|
if err != nil {
|
|
SendError(w, http.StatusInternalServerError, 5001, "Failed to register user")
|
|
return
|
|
}
|
|
|
|
SendSuccess(w, user)
|
|
}
|