add
This commit is contained in:
@@ -4,7 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"common/types"
|
||||
"common/httpx"
|
||||
"common/codes"
|
||||
"user/internal/model"
|
||||
"user/internal/service"
|
||||
)
|
||||
@@ -19,68 +20,62 @@ func New(s *service.Service) *Handler {
|
||||
|
||||
func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil)
|
||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
||||
return
|
||||
}
|
||||
var req model.RegisterReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, false, "invalid json", nil)
|
||||
httpx.BadRequest(w, "invalid json")
|
||||
return
|
||||
}
|
||||
userID, err := h.S.Register(req.Account, req.Password)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case service.ErrInvalidInput:
|
||||
writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil)
|
||||
httpx.BadRequest(w, "invalid account or password")
|
||||
case service.ErrAccountExists:
|
||||
writeJSON(w, http.StatusConflict, false, "account exists", nil)
|
||||
httpx.Conflict(w, "account exists")
|
||||
default:
|
||||
writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
|
||||
httpx.InternalError(w)
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, true, "ok", map[string]string{"user_id": userID})
|
||||
httpx.Created(w, map[string]string{"user_id": userID})
|
||||
}
|
||||
|
||||
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil)
|
||||
httpx.MethodNotAllowed(w, string(codes.MethodNotAllowed))
|
||||
return
|
||||
}
|
||||
var req model.LoginReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, false, "invalid json", nil)
|
||||
httpx.BadRequest(w, "invalid json")
|
||||
return
|
||||
}
|
||||
userID, err := h.S.Login(req.Account, req.Password)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case service.ErrInvalidInput:
|
||||
writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil)
|
||||
httpx.BadRequest(w, "invalid account or password")
|
||||
case service.ErrUnauthorized:
|
||||
writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil)
|
||||
httpx.Unauthorized(w, "unauthorized")
|
||||
default:
|
||||
writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
|
||||
httpx.InternalError(w)
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"user_id": userID})
|
||||
httpx.OK(w, map[string]string{"user_id": userID})
|
||||
}
|
||||
|
||||
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, true, "ok", nil)
|
||||
httpx.OK(w, nil)
|
||||
}
|
||||
|
||||
func (h *Handler) Version(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"version": "user-service v0.1.0"})
|
||||
httpx.OK(w, map[string]string{"version": "user-service v0.1.0"})
|
||||
}
|
||||
|
||||
func (h *Handler) Root(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, true, "ok", map[string]string{"service": "user"})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, code int, status bool, msg string, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(types.Response{Status: status, Message: msg, Data: data})
|
||||
httpx.OK(w, map[string]string{"service": "user"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user