Fix backend validation
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -130,6 +130,7 @@
|
|||||||
<argument>docker-compose.dev.yml</argument>
|
<argument>docker-compose.dev.yml</argument>
|
||||||
<argument>down</argument>
|
<argument>down</argument>
|
||||||
<argument>-v</argument>
|
<argument>-v</argument>
|
||||||
|
<argument>--remove-orphans</argument>
|
||||||
</arguments>
|
</arguments>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.petshop.backend.config;
|
|||||||
|
|
||||||
import com.petshop.backend.entity.User;
|
import com.petshop.backend.entity.User;
|
||||||
import com.petshop.backend.repository.UserRepository;
|
import com.petshop.backend.repository.UserRepository;
|
||||||
|
import com.petshop.backend.service.StoreAssignmentService;
|
||||||
import com.petshop.backend.service.UserBusinessLinkageService;
|
import com.petshop.backend.service.UserBusinessLinkageService;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
@@ -13,11 +14,13 @@ public class DataInitializer implements CommandLineRunner {
|
|||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final UserBusinessLinkageService userBusinessLinkageService;
|
private final UserBusinessLinkageService userBusinessLinkageService;
|
||||||
|
private final StoreAssignmentService storeAssignmentService;
|
||||||
|
|
||||||
public DataInitializer(UserRepository userRepository, PasswordEncoder passwordEncoder, UserBusinessLinkageService userBusinessLinkageService) {
|
public DataInitializer(UserRepository userRepository, PasswordEncoder passwordEncoder, UserBusinessLinkageService userBusinessLinkageService, StoreAssignmentService storeAssignmentService) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
this.userBusinessLinkageService = userBusinessLinkageService;
|
this.userBusinessLinkageService = userBusinessLinkageService;
|
||||||
|
this.storeAssignmentService = storeAssignmentService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,7 +65,7 @@ public class DataInitializer implements CommandLineRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ensure linked employee
|
// Ensure linked employee
|
||||||
userBusinessLinkageService.ensureLinkedEmployee(admin);
|
storeAssignmentService.assignStoreIfMissing(userBusinessLinkageService.ensureLinkedEmployee(admin), 1L);
|
||||||
|
|
||||||
User staff = userRepository.findByUsername("staff").orElse(null);
|
User staff = userRepository.findByUsername("staff").orElse(null);
|
||||||
if (staff == null) {
|
if (staff == null) {
|
||||||
@@ -102,7 +105,7 @@ public class DataInitializer implements CommandLineRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ensure linked employee
|
// Ensure linked employee
|
||||||
userBusinessLinkageService.ensureLinkedEmployee(staff);
|
storeAssignmentService.assignStoreIfMissing(userBusinessLinkageService.ensureLinkedEmployee(staff), 1L);
|
||||||
|
|
||||||
User customer = userRepository.findByUsername("customer").orElse(null);
|
User customer = userRepository.findByUsername("customer").orElse(null);
|
||||||
if (customer == null) {
|
if (customer == null) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
import org.springframework.security.authentication.DisabledException;
|
import org.springframework.security.authentication.DisabledException;
|
||||||
|
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@@ -126,6 +127,13 @@ public class AuthController {
|
|||||||
Map<String, String> error = new HashMap<>();
|
Map<String, String> error = new HashMap<>();
|
||||||
error.put("message", "Invalid username or password");
|
error.put("message", "Invalid username or password");
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(error);
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(error);
|
||||||
|
} catch (InternalAuthenticationServiceException e) {
|
||||||
|
if (e.getCause() instanceof DisabledException disabledException) {
|
||||||
|
Map<String, String> error = new HashMap<>();
|
||||||
|
error.put("message", disabledException.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(error);
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
} catch (DisabledException e) {
|
} catch (DisabledException e) {
|
||||||
Map<String, String> error = new HashMap<>();
|
Map<String, String> error = new HashMap<>();
|
||||||
error.put("message", e.getMessage());
|
error.put("message", e.getMessage());
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.petshop.backend.exception;
|
|||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
@@ -71,6 +72,16 @@ public class GlobalExceptionHandler {
|
|||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
|
||||||
|
ErrorResponse error = new ErrorResponse(
|
||||||
|
HttpStatus.BAD_REQUEST.value(),
|
||||||
|
"Operation violates existing data relationships",
|
||||||
|
LocalDateTime.now()
|
||||||
|
);
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
||||||
ErrorResponse error = new ErrorResponse(
|
ErrorResponse error = new ErrorResponse(
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.petshop.backend.service;
|
||||||
|
|
||||||
|
import com.petshop.backend.entity.Employee;
|
||||||
|
import com.petshop.backend.entity.EmployeeStore;
|
||||||
|
import com.petshop.backend.entity.StoreLocation;
|
||||||
|
import com.petshop.backend.exception.ResourceNotFoundException;
|
||||||
|
import com.petshop.backend.repository.EmployeeStoreRepository;
|
||||||
|
import com.petshop.backend.repository.StoreRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StoreAssignmentService {
|
||||||
|
|
||||||
|
private final EmployeeStoreRepository employeeStoreRepository;
|
||||||
|
private final StoreRepository storeRepository;
|
||||||
|
|
||||||
|
public StoreAssignmentService(EmployeeStoreRepository employeeStoreRepository, StoreRepository storeRepository) {
|
||||||
|
this.employeeStoreRepository = employeeStoreRepository;
|
||||||
|
this.storeRepository = storeRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void assignStoreIfMissing(Employee employee, Long storeId) {
|
||||||
|
if (employeeStoreRepository.findByEmployeeEmployeeId(employee.getEmployeeId()).isPresent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StoreLocation store = storeRepository.findById(storeId)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + storeId));
|
||||||
|
|
||||||
|
employeeStoreRepository.save(new EmployeeStore(employee, store));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user