backend DRY/KISS cleanup #324
@@ -4,7 +4,6 @@ import com.petshop.backend.dto.ai.AiChatRequest;
|
||||
import com.petshop.backend.dto.ai.AiChatResponse;
|
||||
import com.petshop.backend.entity.Pet;
|
||||
import com.petshop.backend.entity.User;
|
||||
import com.petshop.backend.exception.BusinessException;
|
||||
import com.petshop.backend.repository.PetRepository;
|
||||
import com.petshop.backend.service.OpenRouterService;
|
||||
import com.petshop.backend.util.AuthenticationHelper;
|
||||
@@ -37,7 +36,7 @@ public class AiChatController {
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
public ResponseEntity<AiChatResponse> sendMessage(@Valid @RequestBody AiChatRequest request) {
|
||||
if (request.getMessage() == null || request.getMessage().isBlank()) {
|
||||
throw new BusinessException("Message cannot be empty");
|
||||
return ResponseEntity.badRequest().body(AiChatResponse.fail("Message cannot be empty"));
|
||||
}
|
||||
ContentFilter.validate(request.getMessage());
|
||||
|
||||
|
||||
@@ -279,22 +279,17 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@PostMapping("/me/avatar")
|
||||
public ResponseEntity<AvatarUploadResponse> uploadAvatar(@RequestParam("avatar") MultipartFile file) {
|
||||
public ResponseEntity<AvatarUploadResponse> uploadAvatar(@RequestParam("avatar") MultipartFile file) throws IOException {
|
||||
User user = authHelper.getAuthenticatedUser();
|
||||
|
||||
ImageValidationUtil.validate(file);
|
||||
|
||||
try {
|
||||
avatarStorageService.deleteAvatar(user);
|
||||
String avatarPath = avatarStorageService.storeAvatar(file);
|
||||
user.setAvatarUrl(avatarPath);
|
||||
userRepository.save(user);
|
||||
avatarStorageService.deleteAvatar(user);
|
||||
String avatarPath = avatarStorageService.storeAvatar(file);
|
||||
user.setAvatarUrl(avatarPath);
|
||||
userRepository.save(user);
|
||||
|
||||
return ResponseEntity.ok(new AvatarUploadResponse(avatarStorageService.toOwnerAvatarUrl(user), "Avatar uploaded successfully"));
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException("Failed to upload avatar: " + e.getMessage());
|
||||
}
|
||||
return ResponseEntity.ok(new AvatarUploadResponse(avatarStorageService.toOwnerAvatarUrl(user), "Avatar uploaded successfully"));
|
||||
}
|
||||
|
||||
@GetMapping("/me/avatar")
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.petshop.backend.exception;
|
||||
|
||||
import com.petshop.backend.service.PetService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.core.PropertyReferenceException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -121,6 +122,11 @@ public class GlobalExceptionHandler {
|
||||
return buildErrorResponse(HttpStatus.FORBIDDEN, "Access to this pet image is not allowed", ex, request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(IOException.class)
|
||||
public ResponseEntity<ApiErrorResponse> handleIOException(IOException ex, HttpServletRequest request) {
|
||||
return buildErrorResponse(HttpStatus.BAD_REQUEST, ex.getMessage(), ex, request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ApiErrorResponse> handleGenericException(Exception ex, HttpServletRequest request) {
|
||||
String message = ex.getMessage() == null || ex.getMessage().isBlank()
|
||||
|
||||
@@ -139,11 +139,15 @@ public class CouponService {
|
||||
}
|
||||
|
||||
public static boolean isPercentageType(String discountType) {
|
||||
return "PERCENTAGE".equalsIgnoreCase(discountType) || "PERCENT".equalsIgnoreCase(discountType);
|
||||
if (discountType == null) return false;
|
||||
String type = discountType.trim();
|
||||
return "PERCENTAGE".equalsIgnoreCase(type) || "PERCENT".equalsIgnoreCase(type);
|
||||
}
|
||||
|
||||
public static boolean isFixedType(String discountType) {
|
||||
return "FIXED".equalsIgnoreCase(discountType) || "FLAT".equalsIgnoreCase(discountType);
|
||||
if (discountType == null) return false;
|
||||
String type = discountType.trim();
|
||||
return "FIXED".equalsIgnoreCase(type) || "FLAT".equalsIgnoreCase(type);
|
||||
}
|
||||
|
||||
private CouponResponse mapToResponse(Coupon coupon) {
|
||||
|
||||
Reference in New Issue
Block a user