Skip to content

Commit

Permalink
refactor(rent-service): change the type of rent entity id from string…
Browse files Browse the repository at this point in the history
… to uuid
  • Loading branch information
vincenzo.corso committed Mar 24, 2024
1 parent 46a3180 commit 5e62218
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Entity
@Table(name = "rents")
Expand All @@ -19,7 +20,7 @@ public class RentEntity {
@GeneratedValue
@UuidGenerator
@Column(name = "rent_id")
private String id;
private UUID id;

@Column(name = "customer_id", nullable = false)
private String customerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Component
public class RentEntityMapper {
public RentEntity convertToEntity(Rent rent) {
RentEntity rentEntity = new RentEntity();
rentEntity.setId(rent.getId());
rentEntity.setId(Optional.ofNullable(rent.getId())
.map(UUID::fromString)
.orElse(null));
rentEntity.setCustomerId(rent.getDetails().customerId());
rentEntity.setVehicleId(rent.getDetails().vehicleId());
rentEntity.setCurrentState(rent.getCurrentState().toString());
Expand All @@ -37,7 +41,9 @@ private RentStateTransitionEntity convertToEntity(RentEntity rent, RentStateTran
}

public Rent convertFromEntity(RentEntity rentEntity) {
String rentId = rentEntity.getId();
String rentId = Optional.ofNullable(rentEntity.getId())
.map(UUID::toString)
.orElse(null);
RentDetails rentDetails = new RentDetails(rentEntity.getCustomerId(), rentEntity.getVehicleId());
List<RentStateTransition> transitions = rentEntity.getStateTransitions().stream().map(this::convertFromEntity).collect(Collectors.toList());
return new RentWrapper(rentId, rentDetails, transitions, rentEntity.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface RentJPARepository extends JpaRepository<RentEntity, String> {
public interface RentJPARepository extends JpaRepository<RentEntity, UUID> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@AllArgsConstructor
Expand All @@ -26,7 +27,10 @@ public class RentJPARepositoryAdapter implements RentRepository {

@Override
public Optional<Rent> findById(String rentId) {
return this.rentRepository.findById(rentId).map(this.rentMapper::convertFromEntity);
return Optional.ofNullable(rentId)
.map(UUID::fromString)
.flatMap(this.rentRepository::findById)
.map(this.rentMapper::convertFromEntity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import it.vincenzocorso.carsharing.rentservice.domain.models.Rent;
import it.vincenzocorso.carsharing.rentservice.domain.models.RentStateTransition;

import java.util.Optional;
import java.util.UUID;

import static it.vincenzocorso.carsharing.rentservice.domain.FakeRent.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -15,7 +18,7 @@ public class FakeRentEntity {
public static final RentEntity RENT_ENTITY = new RentEntityMapper().convertToEntity(RENT_WRAPPER);

public static void assertEqualsWithRent(RentEntity actualRentEntity) {
assertEquals(RENT_ID, actualRentEntity.getId());
assertEquals(RENT_ID, Optional.ofNullable(actualRentEntity.getId()).map(UUID::toString).orElse(null));
assertEquals(CUSTOMER_ID, actualRentEntity.getCustomerId());
assertEquals(VEHICLE_ID, actualRentEntity.getVehicleId());
for(RentStateTransition expectedTransition : ORDERED_STATE_TRANSITIONS) {
Expand Down

0 comments on commit 5e62218

Please sign in to comment.