package http_router import ( "context" "encoding/json" "log/slog" "net/http" "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/constants" "github.com/gorilla/mux" rmodel "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/request_model" ) // @Summary Получить профиль агента // @Description Получение профиля агента по ID // @Tags agents // @Accept json // @Produce json // @Param agent_id path string true "ID агента" // @Success 200 {object} rmodel.ProfileGetResponse "Профиль агента" // @Failure 400 {object} map[string]string "Неверные параметры запроса" // @Failure 500 {object} map[string]string "Внутренняя ошибка сервера" // @Security BearerAuth // @Router /api/v1/agents/{agent_id}/profile [get] func (h *handler) getProfileAgentHandler(w http.ResponseWriter, r *http.Request) { const handlerName = "getAgentProfileHandler" var ( vars = mux.Vars(r) agentId = vars["agent_id"] ) result, err := h.agentService.GetProfile(r.Context(), &rmodel.ProfileGetRequest{ Id: agentId, }) if err != nil { h.handleAgentError(w, err) h.logger.Error("error getting agent profile", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(result); err != nil { http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError) h.logger.Error("error encoding response", slog.String("error", err.Error()), slog.String("handler", handlerName), ) } } // @Summary Обновить профиль агента // @Description Обновление профиля агента // @Tags agents // @Accept json // @Produce json // @Param agent_id path string true "ID агента" // @Param request body rmodel.ProfileUpdateRequest true "Данные для обновления профиля" // @Success 200 {object} map[string]string "Профиль обновлен" // @Failure 400 {object} map[string]string "Неверные данные запроса" // @Failure 500 {object} map[string]string "Внутренняя ошибка сервера" // @Security BearerAuth // @Router /api/v1/agents/{agent_id}/profile [put] func (h *handler) updateProfileAgentHandler(w http.ResponseWriter, r *http.Request) { const handlerName = "setAgentProfileHandler" var ( vars = mux.Vars(r) agentId = vars["agent_id"] ) var request rmodel.ProfileUpdateRequest if err := json.NewDecoder(r.Body).Decode(&request); err != nil { http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest) h.logger.Error("error unmarshalling request: ", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } request.Id = agentId if _, err := h.agentService.UpdateProfile(r.Context(), &request); err != nil { h.handleAgentError(w, err) h.logger.Error("error updating agent profile", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } go h.createSetProfileEvent( context.Background(), agentId, true, handlerName, ) w.WriteHeader(http.StatusNoContent) } // @Summary Получить профиль дистрибьютора // @Description Получение профиля дистрибьютора по ID // @Tags distributors // @Accept json // @Produce json // @Param distributor_id path string true "ID дистрибьютора" // @Success 200 {object} rmodel.ProfileGetResponse "Профиль дистрибьютора" // @Failure 400 {object} map[string]string "Неверные параметры запроса" // @Failure 500 {object} map[string]string "Внутренняя ошибка сервера" // @Security BearerAuth // @Router /api/v1/distributor/{distributor_id}/profile [get] func (h *handler) getProfileDisributorHandler(w http.ResponseWriter, r *http.Request) { const handlerName = "getProfileDisributorHandler" var ( vars = mux.Vars(r) distId = vars["distributor_id"] ) result, err := h.distributorService.GetProfile(r.Context(), &rmodel.ProfileGetRequest{ Id: distId, }) if err != nil { h.handleDistributorError(w, err) h.logger.Error("error getting distributor profile", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(result); err != nil { http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError) h.logger.Error("error encoding response", slog.String("error", err.Error()), slog.String("handler", handlerName), ) } } // @Summary Обновить профиль дистрибьютора // @Description Обновление профиля дистрибьютора // @Tags distributors // @Accept json // @Produce json // @Param distributor_id path string true "ID дистрибьютора" // @Param request body rmodel.ProfileUpdateRequest true "Данные для обновления профиля" // @Success 200 {object} map[string]string "Профиль обновлен" // @Failure 400 {object} map[string]string "Неверные данные запроса" // @Failure 500 {object} map[string]string "Внутренняя ошибка сервера" // @Security BearerAuth // @Router /api/v1/distributor/{distributor_id}/profile [put] func (h *handler) updateProfileDistributorHandler(w http.ResponseWriter, r *http.Request) { const handlerName = "updateProfileDistributorHandler" var ( vars = mux.Vars(r) distId = vars["distributor_id"] ) var request rmodel.ProfileUpdateRequest if err := json.NewDecoder(r.Body).Decode(&request); err != nil { http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest) h.logger.Error("error while unmarshalling request: ", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } request.Id = distId if _, err := h.distributorService.UpdateProfile(r.Context(), &request); err != nil { h.handleDistributorError(w, err) h.logger.Error("error updating distributor profile", slog.String("error", err.Error()), slog.String("handler", handlerName), ) return } go h.createSetProfileEvent( context.Background(), distId, false, handlerName, ) w.WriteHeader(http.StatusNoContent) }