Skip to content

Commit

Permalink
Merge pull request #18 from bouanani-soufiane/PIG-64-Lofts
Browse files Browse the repository at this point in the history
PIG-64-Lofts
  • Loading branch information
bouanani-soufiane authored Nov 10, 2024
2 parents 3d196cb + f0513af commit 86d3df9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import ma.yc.PigeonSkyRace.piegon.domain.service.LoftDomainService;
import ma.yc.PigeonSkyRace.piegon.domain.service.LoftNameGenerator;
import ma.yc.PigeonSkyRace.piegon.infrastructure.repository.LoftRepository;
import ma.yc.PigeonSkyRace.user.application.service.UserApplicationService;
import ma.yc.PigeonSkyRace.user.domain.exception.InvalidUserException;
import ma.yc.PigeonSkyRace.user.domain.model.valueobject.UserId;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -25,6 +28,7 @@ public class DefaultLoftDomainService implements LoftDomainService, LoftApplicat
private final LoftRepository repository;
private final LoftMapper mapper;
private final LoftNameGenerator loftNameGenerator;
private final UserApplicationService userApplicationService;

@Override
public boolean existsById ( LoftId id ) {
Expand All @@ -33,24 +37,26 @@ public boolean existsById ( LoftId id ) {

@Override
public LoftResponseDTO findById ( LoftId id ) {
return repository.findById(id)
.map(mapper::toDto)
.orElseThrow(() -> new NotFoundException("Loft", id));
return repository.findById(id).map(mapper::toDto).orElseThrow(() -> new NotFoundException("Loft", id));
}

@Override
public List<LoftResponseDTO> findAll () {
return repository.findAll()
.stream()
.map(mapper::toDto)
.toList();
return repository.findAll().stream().map(mapper::toDto).toList();
}

@Override
public LoftResponseDTO create ( LoftRequestDTO dto ) {
try {
userApplicationService.getById(UserId.fromString(dto.userId()));
} catch (NotFoundException e) {
throw new InvalidUserException("User with ID " + dto.userId() + " does not exist", e);
}

Loft loft = mapper.toEntity(dto);
String uniqueName = generateUniqueLoftName();
loft.setName(uniqueName);

return mapper.toDto(repository.save(loft));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ma.yc.PigeonSkyRace.user.domain.exception;

public class InvalidUserException extends RuntimeException {
public InvalidUserException(String message) {
super(message);
}

public InvalidUserException(String message, Throwable cause) {
super(message, cause);
}
}

0 comments on commit 86d3df9

Please sign in to comment.