feat: 部署初版测试
This commit is contained in:
54
server/internal/handler/product_profile_handler.go
Normal file
54
server/internal/handler/product_profile_handler.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/zs/InsightReply/internal/model"
|
||||
"github.com/zs/InsightReply/internal/service"
|
||||
)
|
||||
|
||||
type ProductProfileHandler struct {
|
||||
svc *service.ProductProfileService
|
||||
}
|
||||
|
||||
func NewProductProfileHandler(svc *service.ProductProfileService) *ProductProfileHandler {
|
||||
return &ProductProfileHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *ProductProfileHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.Context().Value("userID").(string)
|
||||
|
||||
profile, err := h.svc.GetProfile(userID)
|
||||
if err != nil {
|
||||
SendError(w, http.StatusNotFound, 4004, "Product profile not found")
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccess(w, profile)
|
||||
}
|
||||
|
||||
func (h *ProductProfileHandler) SaveProfile(w http.ResponseWriter, r *http.Request) {
|
||||
userIDStr := r.Context().Value("userID").(string)
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
SendError(w, http.StatusUnauthorized, 4010, "Invalid user ID")
|
||||
return
|
||||
}
|
||||
|
||||
var profile model.UserProductProfile
|
||||
if err := json.NewDecoder(r.Body).Decode(&profile); err != nil {
|
||||
SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
profile.UserID = userID // Ensure user cannot overwrite other's profile
|
||||
|
||||
if err := h.svc.SaveProfile(&profile); err != nil {
|
||||
SendError(w, http.StatusInternalServerError, 5001, "Failed to save product profile")
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccess(w, profile)
|
||||
}
|
||||
Reference in New Issue
Block a user