simplify controllers and utilities
This commit is contained in:
@@ -2,8 +2,6 @@ package com.petshop.backend.controller;
|
||||
|
||||
import com.petshop.backend.dto.pet.MyPetRequest;
|
||||
import com.petshop.backend.dto.pet.MyPetResponse;
|
||||
import com.petshop.backend.entity.User;
|
||||
import com.petshop.backend.repository.UserRepository;
|
||||
import com.petshop.backend.service.PetService;
|
||||
import com.petshop.backend.util.AuthenticationHelper;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -22,7 +20,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/my-pets")
|
||||
@@ -30,47 +27,34 @@ import java.util.Map;
|
||||
public class MyPetController {
|
||||
|
||||
private final PetService petService;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public MyPetController(PetService petService, UserRepository userRepository) {
|
||||
public MyPetController(PetService petService) {
|
||||
this.petService = petService;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<MyPetResponse>> getMyPets(@RequestParam(required = false) String status) {
|
||||
return ResponseEntity.ok(petService.getMyPets(currentUserId(), status));
|
||||
return ResponseEntity.ok(petService.getMyPets(AuthenticationHelper.getAuthenticatedUserId(), status));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<MyPetResponse> createMyPet(@Valid @RequestBody MyPetRequest request) {
|
||||
return ResponseEntity.ok(petService.createMyPet(currentUserId(), request));
|
||||
return ResponseEntity.ok(petService.createMyPet(AuthenticationHelper.getAuthenticatedUserId(), request));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<MyPetResponse> updateMyPet(@PathVariable Long id, @Valid @RequestBody MyPetRequest request) {
|
||||
return ResponseEntity.ok(petService.updateMyPet(currentUserId(), id, request));
|
||||
return ResponseEntity.ok(petService.updateMyPet(AuthenticationHelper.getAuthenticatedUserId(), id, request));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> deleteMyPet(@PathVariable Long id) {
|
||||
petService.deleteMyPet(currentUserId(), id);
|
||||
petService.deleteMyPet(AuthenticationHelper.getAuthenticatedUserId(), id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/image")
|
||||
public ResponseEntity<?> uploadMyPetImage(@PathVariable Long id, @RequestParam("image") MultipartFile image) {
|
||||
try {
|
||||
return ResponseEntity.ok(petService.uploadMyPetImage(currentUserId(), id, image));
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", ex.getMessage()));
|
||||
} catch (IOException ex) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "Failed to upload pet image: " + ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private Long currentUserId() {
|
||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||
return user.getId();
|
||||
public ResponseEntity<MyPetResponse> uploadMyPetImage(@PathVariable Long id, @RequestParam("image") MultipartFile image) throws IOException {
|
||||
return ResponseEntity.ok(petService.uploadMyPetImage(AuthenticationHelper.getAuthenticatedUserId(), id, image));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,12 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class AuthenticationHelper {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public AuthenticationHelper(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public static Authentication getAuthentication() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null || !authentication.isAuthenticated()) {
|
||||
@@ -41,11 +47,19 @@ public class AuthenticationHelper {
|
||||
return getAuthenticatedPrincipal().getRole() == User.Role.CUSTOMER;
|
||||
}
|
||||
|
||||
public static Long getCustomerIdOrNull(UserRepository userRepository) {
|
||||
public User getAuthenticatedUser() {
|
||||
return getAuthenticatedUser(userRepository);
|
||||
}
|
||||
|
||||
public Long getCustomerIdOrNull() {
|
||||
if (!isCustomer()) {
|
||||
return null;
|
||||
}
|
||||
return getAuthenticatedUser(userRepository).getId();
|
||||
return getAuthenticatedUser().getId();
|
||||
}
|
||||
|
||||
public Long getEffectiveCustomerId(Long requestedCustomerId) {
|
||||
return isCustomer() ? getAuthenticatedUser().getId() : requestedCustomerId;
|
||||
}
|
||||
|
||||
public static User getAuthenticatedUser(UserRepository userRepository) {
|
||||
|
||||
@@ -11,4 +11,20 @@ public final class StringUtils {
|
||||
String trimmed = value.trim();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
|
||||
public static String fullName(String firstName, String lastName) {
|
||||
String first = trimToNull(firstName);
|
||||
String last = trimToNull(lastName);
|
||||
if (first == null) {
|
||||
return last;
|
||||
}
|
||||
if (last == null) {
|
||||
return first;
|
||||
}
|
||||
return first + " " + last;
|
||||
}
|
||||
|
||||
public static String normalizePhone(String value) {
|
||||
return trimToNull(PhoneUtils.normalize(trimToNull(value)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user