modified: serv_nginx/api_bb/internal/handlers/avatar.go

add to response ok message
This commit is contained in:
2025-10-13 01:17:58 +05:00
parent e13545c5f1
commit 39df128e1e
+55 -51
View File
@@ -33,65 +33,69 @@ func (h *AvatarHandler) Routes() chi.Router {
} }
func (h *AvatarHandler) UploadAvatar(w http.ResponseWriter, r *http.Request) { func (h *AvatarHandler) UploadAvatar(w http.ResponseWriter, r *http.Request) {
user, ok := middleware.GetUserFromContext(r.Context()) user, ok := middleware.GetUserFromContext(r.Context())
if !ok { if !ok {
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required") utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
return return
} }
// Парсим multipart форму // Парсим multipart форму
if err := r.ParseMultipartForm(10 << 20); err != nil { // 10MB limit if err := r.ParseMultipartForm(10 << 20); err != nil { // 10MB limit
utils.RespondWithError(w, http.StatusBadRequest, "Failed to parse form: "+err.Error()) utils.RespondWithError(w, http.StatusBadRequest, "Failed to parse form: "+err.Error())
return return
} }
file, header, err := r.FormFile("avatar") file, header, err := r.FormFile("avatar")
if err != nil { if err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Failed to get file: "+err.Error()) utils.RespondWithError(w, http.StatusBadRequest, "Failed to get file: "+err.Error())
return return
} }
defer file.Close() defer file.Close()
// Проверяем тип файла // Проверяем тип файла
allowedTypes := map[string]bool{ allowedTypes := map[string]bool{
"image/jpeg": true, "image/jpeg": true,
"image/jpg": true, "image/jpg": true,
"image/png": true, "image/png": true,
"image/gif": true, "image/gif": true,
} }
if !allowedTypes[header.Header.Get("Content-Type")] { if !allowedTypes[header.Header.Get("Content-Type")] {
utils.RespondWithError(w, http.StatusBadRequest, "Only JPEG, PNG and GIF images are allowed") utils.RespondWithError(w, http.StatusBadRequest, "Only JPEG, PNG and GIF images are allowed")
return return
} }
// Загружаем аватар // Загружаем аватар
avatarPath, err := h.avatarService.UploadAvatar(user.ID, file, header) avatarPath, err := h.avatarService.UploadAvatar(user.ID, file, header)
if err != nil { if err != nil {
h.logger.Error("Failed to upload avatar", zap.Error(err)) h.logger.Error("Failed to upload avatar", zap.Error(err))
utils.RespondWithError(w, http.StatusInternalServerError, "Failed to upload avatar: "+err.Error()) utils.RespondWithError(w, http.StatusInternalServerError, "Failed to upload avatar: "+err.Error())
return return
} }
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{ // Возвращаем ответ с полем success
"message": "Avatar uploaded successfully", utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
"avatar": avatarPath, "success": true,
}) "message": "Avatar uploaded successfully",
"avatar": avatarPath,
})
} }
func (h *AvatarHandler) DeleteAvatar(w http.ResponseWriter, r *http.Request) { func (h *AvatarHandler) DeleteAvatar(w http.ResponseWriter, r *http.Request) {
user, ok := middleware.GetUserFromContext(r.Context()) user, ok := middleware.GetUserFromContext(r.Context())
if !ok { if !ok {
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required") utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
return return
} }
if err := h.avatarService.DeleteAvatar(user.ID); err != nil { if err := h.avatarService.DeleteAvatar(user.ID); err != nil {
h.logger.Error("Failed to delete avatar", zap.Error(err)) h.logger.Error("Failed to delete avatar", zap.Error(err))
utils.RespondWithError(w, http.StatusInternalServerError, "Failed to delete avatar: "+err.Error()) utils.RespondWithError(w, http.StatusInternalServerError, "Failed to delete avatar: "+err.Error())
return return
} }
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{ // Возвращаем ответ с полем success
"message": "Avatar deleted successfully", utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
}) "success": true,
"message": "Avatar deleted successfully",
})
} }