add adoption sourceStore FK
This commit is contained in:
@@ -20,6 +20,8 @@ public class AdoptionRequest {
|
||||
|
||||
private Long employeeId;
|
||||
|
||||
private Long sourceStoreId;
|
||||
|
||||
public Long getPetId() {
|
||||
return petId;
|
||||
}
|
||||
@@ -60,6 +62,14 @@ public class AdoptionRequest {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public Long getSourceStoreId() {
|
||||
return sourceStoreId;
|
||||
}
|
||||
|
||||
public void setSourceStoreId(Long sourceStoreId) {
|
||||
this.sourceStoreId = sourceStoreId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -69,12 +79,13 @@ public class AdoptionRequest {
|
||||
Objects.equals(customerId, that.customerId) &&
|
||||
Objects.equals(adoptionDate, that.adoptionDate) &&
|
||||
Objects.equals(adoptionStatus, that.adoptionStatus) &&
|
||||
Objects.equals(employeeId, that.employeeId);
|
||||
Objects.equals(employeeId, that.employeeId) &&
|
||||
Objects.equals(sourceStoreId, that.sourceStoreId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(petId, customerId, adoptionDate, adoptionStatus, employeeId);
|
||||
return Objects.hash(petId, customerId, adoptionDate, adoptionStatus, employeeId, sourceStoreId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,6 +96,7 @@ public class AdoptionRequest {
|
||||
", adoptionDate=" + adoptionDate +
|
||||
", adoptionStatus='" + adoptionStatus + '\'' +
|
||||
", employeeId=" + employeeId +
|
||||
", sourceStoreId=" + sourceStoreId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ public class AdoptionResponse {
|
||||
private String customerName;
|
||||
private Long employeeId;
|
||||
private String employeeName;
|
||||
private Long sourceStoreId;
|
||||
private String sourceStoreName;
|
||||
private LocalDate adoptionDate;
|
||||
private String adoptionStatus;
|
||||
private BigDecimal adoptionFee;
|
||||
@@ -22,7 +24,7 @@ public class AdoptionResponse {
|
||||
public AdoptionResponse() {
|
||||
}
|
||||
|
||||
public AdoptionResponse(Long adoptionId, Long petId, String petName, Long customerId, String customerName, Long employeeId, String employeeName, LocalDate adoptionDate, String adoptionStatus, BigDecimal adoptionFee, LocalDateTime createdAt, LocalDateTime updatedAt) {
|
||||
public AdoptionResponse(Long adoptionId, Long petId, String petName, Long customerId, String customerName, Long employeeId, String employeeName, Long sourceStoreId, String sourceStoreName, LocalDate adoptionDate, String adoptionStatus, BigDecimal adoptionFee, LocalDateTime createdAt, LocalDateTime updatedAt) {
|
||||
this.adoptionId = adoptionId;
|
||||
this.petId = petId;
|
||||
this.petName = petName;
|
||||
@@ -30,6 +32,8 @@ public class AdoptionResponse {
|
||||
this.customerName = customerName;
|
||||
this.employeeId = employeeId;
|
||||
this.employeeName = employeeName;
|
||||
this.sourceStoreId = sourceStoreId;
|
||||
this.sourceStoreName = sourceStoreName;
|
||||
this.adoptionDate = adoptionDate;
|
||||
this.adoptionStatus = adoptionStatus;
|
||||
this.adoptionFee = adoptionFee;
|
||||
@@ -93,6 +97,22 @@ public class AdoptionResponse {
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
public Long getSourceStoreId() {
|
||||
return sourceStoreId;
|
||||
}
|
||||
|
||||
public void setSourceStoreId(Long sourceStoreId) {
|
||||
this.sourceStoreId = sourceStoreId;
|
||||
}
|
||||
|
||||
public String getSourceStoreName() {
|
||||
return sourceStoreName;
|
||||
}
|
||||
|
||||
public void setSourceStoreName(String sourceStoreName) {
|
||||
this.sourceStoreName = sourceStoreName;
|
||||
}
|
||||
|
||||
public LocalDate getAdoptionDate() {
|
||||
return adoptionDate;
|
||||
}
|
||||
@@ -138,12 +158,12 @@ public class AdoptionResponse {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
AdoptionResponse that = (AdoptionResponse) o;
|
||||
return Objects.equals(adoptionId, that.adoptionId) && Objects.equals(petId, that.petId) && Objects.equals(petName, that.petName) && Objects.equals(customerId, that.customerId) && Objects.equals(customerName, that.customerName) && Objects.equals(adoptionDate, that.adoptionDate) && Objects.equals(adoptionStatus, that.adoptionStatus) && Objects.equals(adoptionFee, that.adoptionFee) && Objects.equals(createdAt, that.createdAt) && Objects.equals(updatedAt, that.updatedAt);
|
||||
return Objects.equals(adoptionId, that.adoptionId) && Objects.equals(petId, that.petId) && Objects.equals(petName, that.petName) && Objects.equals(customerId, that.customerId) && Objects.equals(customerName, that.customerName) && Objects.equals(sourceStoreId, that.sourceStoreId) && Objects.equals(sourceStoreName, that.sourceStoreName) && Objects.equals(adoptionDate, that.adoptionDate) && Objects.equals(adoptionStatus, that.adoptionStatus) && Objects.equals(adoptionFee, that.adoptionFee) && Objects.equals(createdAt, that.createdAt) && Objects.equals(updatedAt, that.updatedAt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(adoptionId, petId, petName, customerId, customerName, adoptionDate, adoptionStatus, adoptionFee, createdAt, updatedAt);
|
||||
return Objects.hash(adoptionId, petId, petName, customerId, customerName, sourceStoreId, sourceStoreName, adoptionDate, adoptionStatus, adoptionFee, createdAt, updatedAt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -154,6 +174,8 @@ public class AdoptionResponse {
|
||||
", petName='" + petName + '\'' +
|
||||
", customerId=" + customerId +
|
||||
", customerName='" + customerName + '\'' +
|
||||
", sourceStoreId=" + sourceStoreId +
|
||||
", sourceStoreName='" + sourceStoreName + '\'' +
|
||||
", adoptionDate=" + adoptionDate +
|
||||
", adoptionStatus='" + adoptionStatus + '\'' +
|
||||
", adoptionFee=" + adoptionFee +
|
||||
|
||||
@@ -28,6 +28,10 @@ public class Adoption {
|
||||
@JoinColumn(name = "employeeId", nullable = false)
|
||||
private User employee;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "sourceStoreId")
|
||||
private StoreLocation sourceStore;
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDate adoptionDate;
|
||||
|
||||
@@ -77,6 +81,14 @@ public class Adoption {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
public StoreLocation getSourceStore() {
|
||||
return sourceStore;
|
||||
}
|
||||
|
||||
public void setSourceStore(StoreLocation sourceStore) {
|
||||
this.sourceStore = sourceStore;
|
||||
}
|
||||
|
||||
public LocalDate getAdoptionDate() {
|
||||
return adoptionDate;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ import com.petshop.backend.dto.adoption.AdoptionResponse;
|
||||
import com.petshop.backend.dto.common.BulkDeleteRequest;
|
||||
import com.petshop.backend.entity.Adoption;
|
||||
import com.petshop.backend.entity.Pet;
|
||||
import com.petshop.backend.entity.StoreLocation;
|
||||
import com.petshop.backend.entity.User;
|
||||
import com.petshop.backend.exception.ResourceNotFoundException;
|
||||
import com.petshop.backend.repository.AdoptionRepository;
|
||||
import com.petshop.backend.repository.PetRepository;
|
||||
import com.petshop.backend.repository.StoreRepository;
|
||||
import com.petshop.backend.repository.UserRepository;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -27,11 +29,13 @@ public class AdoptionService {
|
||||
private final AdoptionRepository adoptionRepository;
|
||||
private final PetRepository petRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final StoreRepository storeRepository;
|
||||
|
||||
public AdoptionService(AdoptionRepository adoptionRepository, PetRepository petRepository, UserRepository userRepository) {
|
||||
public AdoptionService(AdoptionRepository adoptionRepository, PetRepository petRepository, UserRepository userRepository, StoreRepository storeRepository) {
|
||||
this.adoptionRepository = adoptionRepository;
|
||||
this.petRepository = petRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.storeRepository = storeRepository;
|
||||
}
|
||||
|
||||
public Page<AdoptionResponse> getAllAdoptions(String query, Pageable pageable, Long customerId) {
|
||||
@@ -73,6 +77,10 @@ public class AdoptionService {
|
||||
User customer = userRepository.findById(request.getCustomerId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Customer not found with id: " + request.getCustomerId()));
|
||||
User employee = resolveAdoptionEmployee(request.getEmployeeId());
|
||||
StoreLocation sourceStore = request.getSourceStoreId() != null
|
||||
? storeRepository.findById(request.getSourceStoreId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getSourceStoreId()))
|
||||
: null;
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
validatePetAvailability(pet, null);
|
||||
|
||||
@@ -80,6 +88,7 @@ public class AdoptionService {
|
||||
adoption.setPet(pet);
|
||||
adoption.setCustomer(customer);
|
||||
adoption.setEmployee(employee);
|
||||
adoption.setSourceStore(sourceStore);
|
||||
adoption.setAdoptionDate(request.getAdoptionDate());
|
||||
adoption.setAdoptionStatus(adoptionStatus);
|
||||
|
||||
@@ -99,12 +108,17 @@ public class AdoptionService {
|
||||
User customer = userRepository.findById(request.getCustomerId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Customer not found with id: " + request.getCustomerId()));
|
||||
User employee = resolveAdoptionEmployee(request.getEmployeeId());
|
||||
StoreLocation sourceStore = request.getSourceStoreId() != null
|
||||
? storeRepository.findById(request.getSourceStoreId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + request.getSourceStoreId()))
|
||||
: null;
|
||||
String adoptionStatus = normalizeAdoptionStatus(request.getAdoptionStatus());
|
||||
validatePetAvailability(pet, adoption.getAdoptionId());
|
||||
|
||||
adoption.setPet(pet);
|
||||
adoption.setCustomer(customer);
|
||||
adoption.setEmployee(employee);
|
||||
adoption.setSourceStore(sourceStore);
|
||||
adoption.setAdoptionDate(request.getAdoptionDate());
|
||||
adoption.setAdoptionStatus(adoptionStatus);
|
||||
|
||||
@@ -127,6 +141,7 @@ public class AdoptionService {
|
||||
}
|
||||
|
||||
private AdoptionResponse mapToResponse(Adoption adoption) {
|
||||
StoreLocation sourceStore = adoption.getSourceStore();
|
||||
return new AdoptionResponse(
|
||||
adoption.getAdoptionId(),
|
||||
adoption.getPet().getPetId(),
|
||||
@@ -135,6 +150,8 @@ public class AdoptionService {
|
||||
adoption.getCustomer().getFirstName() + " " + adoption.getCustomer().getLastName(),
|
||||
adoption.getEmployee().getId(),
|
||||
adoption.getEmployee().getFirstName() + " " + adoption.getEmployee().getLastName(),
|
||||
sourceStore != null ? sourceStore.getStoreId() : null,
|
||||
sourceStore != null ? sourceStore.getStoreName() : null,
|
||||
adoption.getAdoptionDate(),
|
||||
adoption.getAdoptionStatus(),
|
||||
adoption.getPet().getPetPrice(),
|
||||
|
||||
Reference in New Issue
Block a user