inject AuthenticationHelper bean

This commit is contained in:
2026-04-17 17:08:00 -06:00
parent 18030d5d2e
commit 4d96d1961c
7 changed files with 54 additions and 101 deletions

View File

@@ -4,8 +4,6 @@ import com.petshop.backend.dto.adoption.AdoptionRequest;
import com.petshop.backend.dto.adoption.AdoptionResponse; import com.petshop.backend.dto.adoption.AdoptionResponse;
import com.petshop.backend.dto.adoption.CustomerAdoptionRequest; import com.petshop.backend.dto.adoption.CustomerAdoptionRequest;
import com.petshop.backend.dto.common.BulkDeleteRequest; import com.petshop.backend.dto.common.BulkDeleteRequest;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.AdoptionService; import com.petshop.backend.service.AdoptionService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@@ -23,11 +21,11 @@ import java.time.LocalDate;
public class AdoptionController { public class AdoptionController {
private final AdoptionService adoptionService; private final AdoptionService adoptionService;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
public AdoptionController(AdoptionService adoptionService, UserRepository userRepository) { public AdoptionController(AdoptionService adoptionService, AuthenticationHelper authHelper) {
this.adoptionService = adoptionService; this.adoptionService = adoptionService;
this.userRepository = userRepository; this.authHelper = authHelper;
} }
@GetMapping @GetMapping
@@ -40,9 +38,7 @@ public class AdoptionController {
@RequestParam(required = false) String date, @RequestParam(required = false) String date,
Pageable pageable) { Pageable pageable) {
Long effectiveCustomerId = AuthenticationHelper.isCustomer() Long effectiveCustomerId = authHelper.getEffectiveCustomerId(customerId);
? AuthenticationHelper.getAuthenticatedUser(userRepository).getId()
: customerId;
LocalDate adoptionDate = (date != null && !date.isBlank()) ? LocalDate.parse(date) : null; LocalDate adoptionDate = (date != null && !date.isBlank()) ? LocalDate.parse(date) : null;
@@ -52,7 +48,7 @@ public class AdoptionController {
@GetMapping("/{id}") @GetMapping("/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AdoptionResponse> getAdoptionById(@PathVariable Long id) { public ResponseEntity<AdoptionResponse> getAdoptionById(@PathVariable Long id) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.ok(adoptionService.getAdoptionById(id, customerId)); return ResponseEntity.ok(adoptionService.getAdoptionById(id, customerId));
} }
@@ -65,16 +61,15 @@ public class AdoptionController {
@PostMapping("/request") @PostMapping("/request")
@PreAuthorize("hasAnyRole('CUSTOMER', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'ADMIN')")
public ResponseEntity<AdoptionResponse> requestAdoption(@Valid @RequestBody CustomerAdoptionRequest request) { public ResponseEntity<AdoptionResponse> requestAdoption(@Valid @RequestBody CustomerAdoptionRequest request) {
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
return ResponseEntity.status(HttpStatus.CREATED).body( return ResponseEntity.status(HttpStatus.CREATED).body(
adoptionService.requestAdoption(user.getId(), request.getPetId(), request.getEmployeeId(), request.getSourceStoreId(), request.getAdoptionDate()) adoptionService.requestAdoption(authHelper.getAuthenticatedUser().getId(), request.getPetId(), request.getEmployeeId(), request.getSourceStoreId(), request.getAdoptionDate())
); );
} }
@PatchMapping("/{id}/cancel") @PatchMapping("/{id}/cancel")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AdoptionResponse> cancelAdoption(@PathVariable Long id) { public ResponseEntity<AdoptionResponse> cancelAdoption(@PathVariable Long id) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.ok(adoptionService.cancelAdoption(id, customerId)); return ResponseEntity.ok(adoptionService.cancelAdoption(id, customerId));
} }

View File

@@ -4,15 +4,14 @@ import com.petshop.backend.dto.ai.AiChatRequest;
import com.petshop.backend.dto.ai.AiChatResponse; import com.petshop.backend.dto.ai.AiChatResponse;
import com.petshop.backend.entity.Pet; import com.petshop.backend.entity.Pet;
import com.petshop.backend.entity.User; import com.petshop.backend.entity.User;
import com.petshop.backend.exception.BusinessException;
import com.petshop.backend.repository.PetRepository; import com.petshop.backend.repository.PetRepository;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.OpenRouterService; import com.petshop.backend.service.OpenRouterService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
import com.petshop.backend.util.ContentFilter; import com.petshop.backend.util.ContentFilter;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collections; import java.util.Collections;
@@ -24,43 +23,31 @@ public class AiChatController {
private final OpenRouterService openRouterService; private final OpenRouterService openRouterService;
private final PetRepository petRepository; private final PetRepository petRepository;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
public AiChatController(OpenRouterService openRouterService, public AiChatController(OpenRouterService openRouterService,
PetRepository petRepository, PetRepository petRepository,
UserRepository userRepository) { AuthenticationHelper authHelper) {
this.openRouterService = openRouterService; this.openRouterService = openRouterService;
this.petRepository = petRepository; this.petRepository = petRepository;
this.userRepository = userRepository; this.authHelper = authHelper;
}
private User getCurrentUser() {
try {
return AuthenticationHelper.getAuthenticatedUser(userRepository);
}
catch (RuntimeException ex) {
throw new UsernameNotFoundException(ex.getMessage(), ex);
}
} }
@PostMapping("/message") @PostMapping("/message")
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public ResponseEntity<AiChatResponse> sendMessage(@Valid @RequestBody AiChatRequest request) { public ResponseEntity<AiChatResponse> sendMessage(@Valid @RequestBody AiChatRequest request) {
if (request.getMessage() == null || request.getMessage().isBlank()) { if (request.getMessage() == null || request.getMessage().isBlank()) {
return ResponseEntity.badRequest().body(AiChatResponse.fail("Message cannot be empty")); throw new BusinessException("Message cannot be empty");
} }
ContentFilter.validate(request.getMessage()); ContentFilter.validate(request.getMessage());
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
List<Pet> userPets; List<Pet> userPets;
try { try {
userPets = petRepository.findAllByOwner_IdAndPetStatusInOrderByPetNameAsc( userPets = petRepository.findAllByOwner_IdAndPetStatusInOrderByPetNameAsc(
user.getId(), List.of("Adopted", "Owned")); user.getId(), List.of("Adopted", "Owned"));
} } catch (Exception e) {
catch (Exception e) {
userPets = Collections.emptyList(); userPets = Collections.emptyList();
} }
@@ -72,15 +59,9 @@ public class AiChatController {
); );
return ResponseEntity.ok(AiChatResponse.ok(aiReply)); return ResponseEntity.ok(AiChatResponse.ok(aiReply));
} } catch (IllegalStateException e) {
catch (IllegalStateException e) {
return ResponseEntity.status(503).body(AiChatResponse.fail("AI service is not configured. Please contact support.")); return ResponseEntity.status(503).body(AiChatResponse.fail("AI service is not configured. Please contact support."));
} } catch (Exception e) {
catch (Exception e) {
return ResponseEntity.status(502).body(AiChatResponse.fail("AI service is temporarily unavailable. Please try again later.")); return ResponseEntity.status(502).body(AiChatResponse.fail("AI service is temporarily unavailable. Please try again later."));
} }
} }

View File

@@ -1,8 +1,6 @@
package com.petshop.backend.controller; package com.petshop.backend.controller;
import com.petshop.backend.dto.analytics.DashboardResponse; import com.petshop.backend.dto.analytics.DashboardResponse;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.AnalyticsService; import com.petshop.backend.service.AnalyticsService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
@@ -20,11 +18,11 @@ import java.time.LocalDate;
public class AnalyticsController { public class AnalyticsController {
private final AnalyticsService analyticsService; private final AnalyticsService analyticsService;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
public AnalyticsController(AnalyticsService analyticsService, UserRepository userRepository) { public AnalyticsController(AnalyticsService analyticsService, AuthenticationHelper authHelper) {
this.analyticsService = analyticsService; this.analyticsService = analyticsService;
this.userRepository = userRepository; this.authHelper = authHelper;
} }
@GetMapping("/dashboard") @GetMapping("/dashboard")
@@ -41,7 +39,7 @@ public class AnalyticsController {
if (top < 1 || top > 50) { if (top < 1 || top > 50) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "top must be between 1 and 50"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "top must be between 1 and 50");
} }
User user = AuthenticationHelper.getAuthenticatedUser(userRepository); var user = authHelper.getAuthenticatedUser();
java.time.LocalDateTime endDateTime = endDate != null ? endDate.plusDays(1).atStartOfDay() : null; java.time.LocalDateTime endDateTime = endDate != null ? endDate.plusDays(1).atStartOfDay() : null;
return ResponseEntity.ok(analyticsService.getDashboardData(days, top, user, paymentMethod, storeId, channel, endDateTime)); return ResponseEntity.ok(analyticsService.getDashboardData(days, top, user, paymentMethod, storeId, channel, endDateTime));
} }

View File

@@ -3,8 +3,6 @@ package com.petshop.backend.controller;
import com.petshop.backend.dto.appointment.AppointmentRequest; import com.petshop.backend.dto.appointment.AppointmentRequest;
import com.petshop.backend.dto.appointment.AppointmentResponse; import com.petshop.backend.dto.appointment.AppointmentResponse;
import com.petshop.backend.dto.common.BulkDeleteRequest; import com.petshop.backend.dto.common.BulkDeleteRequest;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.AppointmentService; import com.petshop.backend.service.AppointmentService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@@ -23,11 +21,11 @@ import java.util.List;
public class AppointmentController { public class AppointmentController {
private final AppointmentService appointmentService; private final AppointmentService appointmentService;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
public AppointmentController(AppointmentService appointmentService, UserRepository userRepository) { public AppointmentController(AppointmentService appointmentService, AuthenticationHelper authHelper) {
this.appointmentService = appointmentService; this.appointmentService = appointmentService;
this.userRepository = userRepository; this.authHelper = authHelper;
} }
@GetMapping @GetMapping
@@ -41,9 +39,7 @@ public class AppointmentController {
@RequestParam(required = false) Long employeeId, @RequestParam(required = false) Long employeeId,
Pageable pageable) { Pageable pageable) {
Long effectiveCustomerId = AuthenticationHelper.isCustomer() Long effectiveCustomerId = authHelper.getEffectiveCustomerId(customerId);
? AuthenticationHelper.getAuthenticatedUser(userRepository).getId()
: customerId;
LocalDate appointmentDate = (date != null && !date.isBlank()) ? LocalDate.parse(date) : null; LocalDate appointmentDate = (date != null && !date.isBlank()) ? LocalDate.parse(date) : null;
@@ -54,7 +50,7 @@ public class AppointmentController {
@GetMapping("/{id}") @GetMapping("/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AppointmentResponse> getAppointmentById(@PathVariable Long id) { public ResponseEntity<AppointmentResponse> getAppointmentById(@PathVariable Long id) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.ok(appointmentService.getAppointmentById(id, customerId)); return ResponseEntity.ok(appointmentService.getAppointmentById(id, customerId));
} }
@@ -62,8 +58,7 @@ public class AppointmentController {
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AppointmentResponse> createAppointment(@Valid @RequestBody AppointmentRequest request) { public ResponseEntity<AppointmentResponse> createAppointment(@Valid @RequestBody AppointmentRequest request) {
if (AuthenticationHelper.isCustomer()) { if (AuthenticationHelper.isCustomer()) {
User user = AuthenticationHelper.getAuthenticatedUser(userRepository); if (!request.getCustomerId().equals(authHelper.getAuthenticatedUser().getId())) {
if (!request.getCustomerId().equals(user.getId())) {
throw new org.springframework.security.access.AccessDeniedException("You can only create appointments for yourself"); throw new org.springframework.security.access.AccessDeniedException("You can only create appointments for yourself");
} }
} }
@@ -74,7 +69,7 @@ public class AppointmentController {
@PatchMapping("/{id}/cancel") @PatchMapping("/{id}/cancel")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<AppointmentResponse> cancelAppointment(@PathVariable Long id) { public ResponseEntity<AppointmentResponse> cancelAppointment(@PathVariable Long id) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.ok(appointmentService.cancelAppointment(id, customerId)); return ResponseEntity.ok(appointmentService.cancelAppointment(id, customerId));
} }

View File

@@ -64,8 +64,9 @@ public class AuthController {
private final PasswordResetService passwordResetService; private final PasswordResetService passwordResetService;
private final EmailService emailService; private final EmailService emailService;
private final UserAuthCacheService userAuthCacheService; private final UserAuthCacheService userAuthCacheService;
private final AuthenticationHelper authHelper;
public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, JwtUtil jwtUtil, PasswordEncoder passwordEncoder, AvatarStorageService avatarStorageService, ActivityLogService activityLogService, PasswordResetService passwordResetService, EmailService emailService, UserAuthCacheService userAuthCacheService) { public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, JwtUtil jwtUtil, PasswordEncoder passwordEncoder, AvatarStorageService avatarStorageService, ActivityLogService activityLogService, PasswordResetService passwordResetService, EmailService emailService, UserAuthCacheService userAuthCacheService, AuthenticationHelper authHelper) {
this.authenticationManager = authenticationManager; this.authenticationManager = authenticationManager;
this.userRepository = userRepository; this.userRepository = userRepository;
this.jwtUtil = jwtUtil; this.jwtUtil = jwtUtil;
@@ -75,6 +76,7 @@ public class AuthController {
this.passwordResetService = passwordResetService; this.passwordResetService = passwordResetService;
this.emailService = emailService; this.emailService = emailService;
this.userAuthCacheService = userAuthCacheService; this.userAuthCacheService = userAuthCacheService;
this.authHelper = authHelper;
} }
@PostMapping("/register") @PostMapping("/register")
@@ -180,7 +182,7 @@ public class AuthController {
@Transactional(readOnly = true) @Transactional(readOnly = true)
@GetMapping("/me") @GetMapping("/me")
public ResponseEntity<UserInfoResponse> getCurrentUser() { public ResponseEntity<UserInfoResponse> getCurrentUser() {
User user = getAuthenticatedUser(); User user = authHelper.getAuthenticatedUser();
return ResponseEntity.ok(toUserInfoResponse(user)); return ResponseEntity.ok(toUserInfoResponse(user));
} }
@@ -278,7 +280,7 @@ public class AuthController {
@PostMapping("/me/avatar") @PostMapping("/me/avatar")
public ResponseEntity<AvatarUploadResponse> uploadAvatar(@RequestParam("avatar") MultipartFile file) { public ResponseEntity<AvatarUploadResponse> uploadAvatar(@RequestParam("avatar") MultipartFile file) {
User user = getAuthenticatedUser(); User user = authHelper.getAuthenticatedUser();
ImageValidationUtil.validate(file); ImageValidationUtil.validate(file);
@@ -297,7 +299,7 @@ public class AuthController {
@GetMapping("/me/avatar") @GetMapping("/me/avatar")
public ResponseEntity<?> getAvatar() { public ResponseEntity<?> getAvatar() {
User user = getAuthenticatedUser(); User user = authHelper.getAuthenticatedUser();
if (!avatarStorageService.hasAvatar(user)) { if (!avatarStorageService.hasAvatar(user)) {
throw new ResourceNotFoundException("No avatar uploaded"); throw new ResourceNotFoundException("No avatar uploaded");
@@ -310,7 +312,7 @@ public class AuthController {
@GetMapping("/me/avatar/file") @GetMapping("/me/avatar/file")
public ResponseEntity<Resource> getAvatarFile() { public ResponseEntity<Resource> getAvatarFile() {
User user = getAuthenticatedUser(); User user = authHelper.getAuthenticatedUser();
if (!avatarStorageService.hasAvatar(user)) { if (!avatarStorageService.hasAvatar(user)) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
@@ -327,7 +329,7 @@ public class AuthController {
@DeleteMapping("/me/avatar") @DeleteMapping("/me/avatar")
public ResponseEntity<?> deleteAvatar() { public ResponseEntity<?> deleteAvatar() {
User user = getAuthenticatedUser(); User user = authHelper.getAuthenticatedUser();
if (avatarStorageService.hasAvatar(user)) { if (avatarStorageService.hasAvatar(user)) {
try { try {
@@ -352,11 +354,4 @@ public class AuthController {
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
private User getAuthenticatedUser() {
try {
return AuthenticationHelper.getAuthenticatedUser(userRepository);
} catch (RuntimeException ex) {
throw new UsernameNotFoundException(ex.getMessage(), ex);
}
}
} }

View File

@@ -9,7 +9,6 @@ import com.petshop.backend.entity.Message;
import com.petshop.backend.entity.User; import com.petshop.backend.entity.User;
import com.petshop.backend.exception.ResourceNotFoundException; import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.MessageRepository; import com.petshop.backend.repository.MessageRepository;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.ChatAttachmentStorageService; import com.petshop.backend.service.ChatAttachmentStorageService;
import com.petshop.backend.service.ChatRealtimeService; import com.petshop.backend.service.ChatRealtimeService;
import com.petshop.backend.service.ChatService; import com.petshop.backend.service.ChatService;
@@ -22,7 +21,6 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -35,33 +33,25 @@ public class ChatController {
private final ChatService chatService; private final ChatService chatService;
private final ChatRealtimeService chatRealtimeService; private final ChatRealtimeService chatRealtimeService;
private final OpenRouterAiService openRouterAiService; private final OpenRouterAiService openRouterAiService;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
private final ChatAttachmentStorageService attachmentStorageService; private final ChatAttachmentStorageService attachmentStorageService;
private final MessageRepository messageRepository; private final MessageRepository messageRepository;
public ChatController(ChatService chatService, ChatRealtimeService chatRealtimeService, public ChatController(ChatService chatService, ChatRealtimeService chatRealtimeService,
OpenRouterAiService openRouterAiService, UserRepository userRepository, ChatAttachmentStorageService attachmentStorageService, OpenRouterAiService openRouterAiService, AuthenticationHelper authHelper, ChatAttachmentStorageService attachmentStorageService,
MessageRepository messageRepository) { MessageRepository messageRepository) {
this.chatService = chatService; this.chatService = chatService;
this.chatRealtimeService = chatRealtimeService; this.chatRealtimeService = chatRealtimeService;
this.openRouterAiService = openRouterAiService; this.openRouterAiService = openRouterAiService;
this.userRepository = userRepository; this.authHelper = authHelper;
this.attachmentStorageService = attachmentStorageService; this.attachmentStorageService = attachmentStorageService;
this.messageRepository = messageRepository; this.messageRepository = messageRepository;
} }
private User getCurrentUser() {
try {
return AuthenticationHelper.getAuthenticatedUser(userRepository);
} catch (RuntimeException ex) {
throw new UsernameNotFoundException(ex.getMessage(), ex);
}
}
@PostMapping("/conversations") @PostMapping("/conversations")
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public ResponseEntity<ConversationResponse> createConversation(@Valid @RequestBody ConversationRequest request) { public ResponseEntity<ConversationResponse> createConversation(@Valid @RequestBody ConversationRequest request) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
ConversationResponse response = chatService.createConversation(user.getId(), request); ConversationResponse response = chatService.createConversation(user.getId(), request);
chatRealtimeService.publishNewConversation(response); chatRealtimeService.publishNewConversation(response);
return ResponseEntity.status(HttpStatus.CREATED).body(response); return ResponseEntity.status(HttpStatus.CREATED).body(response);
@@ -71,7 +61,7 @@ public class ChatController {
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<List<ConversationResponse>> getConversations( public ResponseEntity<List<ConversationResponse>> getConversations(
@RequestParam(required = false, defaultValue = "false") boolean mine) { @RequestParam(required = false, defaultValue = "false") boolean mine) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
List<ConversationResponse> conversations = chatService.getConversations(user.getId(), user.getRole(), mine); List<ConversationResponse> conversations = chatService.getConversations(user.getId(), user.getRole(), mine);
return ResponseEntity.ok(conversations); return ResponseEntity.ok(conversations);
} }
@@ -79,7 +69,7 @@ public class ChatController {
@GetMapping("/conversations/{id}") @GetMapping("/conversations/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<ConversationResponse> getConversation(@PathVariable Long id) { public ResponseEntity<ConversationResponse> getConversation(@PathVariable Long id) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
ConversationResponse conversation = chatService.getConversation(id, user.getId(), user.getRole()); ConversationResponse conversation = chatService.getConversation(id, user.getId(), user.getRole());
return ResponseEntity.ok(conversation); return ResponseEntity.ok(conversation);
} }
@@ -89,7 +79,7 @@ public class ChatController {
public ResponseEntity<MessageResponse> sendMessage( public ResponseEntity<MessageResponse> sendMessage(
@PathVariable Long id, @PathVariable Long id,
@Valid @RequestBody MessageRequest request) { @Valid @RequestBody MessageRequest request) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request); MessageResponse message = chatService.sendMessage(id, user.getId(), user.getRole(), request);
chatRealtimeService.publishMessage(id, message); chatRealtimeService.publishMessage(id, message);
chatRealtimeService.publishConversationUpdate(id); chatRealtimeService.publishConversationUpdate(id);
@@ -103,7 +93,7 @@ public class ChatController {
@PathVariable Long id, @PathVariable Long id,
@RequestParam("file") MultipartFile file, @RequestParam("file") MultipartFile file,
@RequestParam(value = "content", required = false) String content) { @RequestParam(value = "content", required = false) String content) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
MessageResponse message = chatService.sendMessageWithAttachment(id, user.getId(), user.getRole(), file, content); MessageResponse message = chatService.sendMessageWithAttachment(id, user.getId(), user.getRole(), file, content);
chatRealtimeService.publishMessage(id, message); chatRealtimeService.publishMessage(id, message);
chatRealtimeService.publishConversationUpdate(id); chatRealtimeService.publishConversationUpdate(id);
@@ -114,7 +104,7 @@ public class ChatController {
@GetMapping("/messages/{messageId}/attachment") @GetMapping("/messages/{messageId}/attachment")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<Resource> getMessageAttachment(@PathVariable Long messageId) { public ResponseEntity<Resource> getMessageAttachment(@PathVariable Long messageId) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
Message message = messageRepository.findById(messageId) Message message = messageRepository.findById(messageId)
.orElseThrow(() -> new ResourceNotFoundException("Message not found with id: " + messageId)); .orElseThrow(() -> new ResourceNotFoundException("Message not found with id: " + messageId));
@@ -140,7 +130,7 @@ public class ChatController {
@GetMapping("/conversations/{id}/messages") @GetMapping("/conversations/{id}/messages")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<List<MessageResponse>> getMessages(@PathVariable Long id) { public ResponseEntity<List<MessageResponse>> getMessages(@PathVariable Long id) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
List<MessageResponse> messages = chatService.getMessages(id, user.getId(), user.getRole()); List<MessageResponse> messages = chatService.getMessages(id, user.getId(), user.getRole());
return ResponseEntity.ok(messages); return ResponseEntity.ok(messages);
} }
@@ -148,7 +138,7 @@ public class ChatController {
@PostMapping("/conversations/{id}/request-human") @PostMapping("/conversations/{id}/request-human")
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public ResponseEntity<ConversationResponse> requestHumanTakeover(@PathVariable Long id) { public ResponseEntity<ConversationResponse> requestHumanTakeover(@PathVariable Long id) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
ConversationResponse conversation = chatService.requestHumanTakeover(id, user.getId(), user.getRole()); ConversationResponse conversation = chatService.requestHumanTakeover(id, user.getId(), user.getRole());
chatRealtimeService.publishConversationUpdate(id); chatRealtimeService.publishConversationUpdate(id);
return ResponseEntity.ok(conversation); return ResponseEntity.ok(conversation);
@@ -157,7 +147,7 @@ public class ChatController {
@PutMapping("/conversations/{id}") @PutMapping("/conversations/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<ConversationResponse> updateConversation(@PathVariable Long id, @Valid @RequestBody UpdateConversationRequest request) { public ResponseEntity<ConversationResponse> updateConversation(@PathVariable Long id, @Valid @RequestBody UpdateConversationRequest request) {
User user = getCurrentUser(); User user = authHelper.getAuthenticatedUser();
ConversationResponse conversation = chatService.updateConversation(id, user.getId(), user.getRole(), request); ConversationResponse conversation = chatService.updateConversation(id, user.getId(), user.getRole(), request);
chatRealtimeService.publishConversationUpdate(id); chatRealtimeService.publishConversationUpdate(id);
return ResponseEntity.ok(conversation); return ResponseEntity.ok(conversation);

View File

@@ -3,7 +3,6 @@ package com.petshop.backend.controller;
import com.petshop.backend.dto.refund.RefundRequest; import com.petshop.backend.dto.refund.RefundRequest;
import com.petshop.backend.dto.refund.RefundResponse; import com.petshop.backend.dto.refund.RefundResponse;
import com.petshop.backend.dto.refund.RefundUpdateRequest; import com.petshop.backend.dto.refund.RefundUpdateRequest;
import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.service.RefundService; import com.petshop.backend.service.RefundService;
import com.petshop.backend.util.AuthenticationHelper; import com.petshop.backend.util.AuthenticationHelper;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@@ -19,24 +18,24 @@ import java.util.List;
public class RefundController { public class RefundController {
private final RefundService refundService; private final RefundService refundService;
private final UserRepository userRepository; private final AuthenticationHelper authHelper;
public RefundController(RefundService refundService, UserRepository userRepository) { public RefundController(RefundService refundService, AuthenticationHelper authHelper) {
this.refundService = refundService; this.refundService = refundService;
this.userRepository = userRepository; this.authHelper = authHelper;
} }
@PostMapping @PostMapping
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF')")
public ResponseEntity<RefundResponse> createRefund(@Valid @RequestBody RefundRequest request) { public ResponseEntity<RefundResponse> createRefund(@Valid @RequestBody RefundRequest request) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.status(HttpStatus.CREATED).body(refundService.createRefund(request, customerId)); return ResponseEntity.status(HttpStatus.CREATED).body(refundService.createRefund(request, customerId));
} }
@GetMapping @GetMapping
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<List<RefundResponse>> getAllRefunds() { public ResponseEntity<List<RefundResponse>> getAllRefunds() {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
List<RefundResponse> refunds = refundService.getAllRefunds(customerId); List<RefundResponse> refunds = refundService.getAllRefunds(customerId);
return ResponseEntity.ok(refunds); return ResponseEntity.ok(refunds);
} }
@@ -44,7 +43,7 @@ public class RefundController {
@GetMapping("/{id}") @GetMapping("/{id}")
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')") @PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
public ResponseEntity<RefundResponse> getRefundById(@PathVariable Long id) { public ResponseEntity<RefundResponse> getRefundById(@PathVariable Long id) {
Long customerId = AuthenticationHelper.getCustomerIdOrNull(userRepository); Long customerId = authHelper.getCustomerIdOrNull();
return ResponseEntity.ok(refundService.getRefundById(id, customerId)); return ResponseEntity.ok(refundService.getRefundById(id, customerId));
} }