Refactor ShelfService

This commit is contained in:
aditya.chandel
2025-06-08 01:06:40 -06:00
parent ff6c2a5e67
commit 306baef647
@@ -32,49 +32,60 @@ public class ShelfService {
private final UserRepository userRepository; private final UserRepository userRepository;
public Shelf createShelf(ShelfCreateRequest request) { public Shelf createShelf(ShelfCreateRequest request) {
BookLoreUser user = authenticationService.getAuthenticatedUser(); Long userId = getAuthenticatedUserId();
BookLoreUserEntity userEntity = userRepository.findById(user.getId()).orElseThrow(() -> new UsernameNotFoundException("User not found")); if (shelfRepository.existsByUserIdAndName(userId, request.getName())) {
boolean exists = shelfRepository.existsByUserIdAndName(user.getId(), request.getName());
if (exists) {
throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName()); throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName());
} }
ShelfEntity shelfEntity = ShelfEntity.builder() ShelfEntity shelfEntity = ShelfEntity.builder()
.icon(request.getIcon()) .icon(request.getIcon())
.name(request.getName()) .name(request.getName())
.user(userEntity) .user(fetchUserEntityById(userId))
.build(); .build();
return shelfMapper.toShelf(shelfRepository.save(shelfEntity)); return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
} }
public Shelf updateShelf(Long id, ShelfCreateRequest request) { public Shelf updateShelf(Long id, ShelfCreateRequest request) {
ShelfEntity shelfEntity = shelfRepository.findById(id).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(id)); ShelfEntity shelfEntity = findShelfByIdOrThrow(id);
shelfEntity.setName(request.getName()); shelfEntity.setName(request.getName());
shelfEntity.setIcon(request.getIcon()); shelfEntity.setIcon(request.getIcon());
return shelfMapper.toShelf(shelfRepository.save(shelfEntity)); return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
} }
public List<Shelf> getShelves() { public List<Shelf> getShelves() {
BookLoreUser user = authenticationService.getAuthenticatedUser(); Long userId = getAuthenticatedUserId();
return shelfRepository.findByUserId(user.getId()).stream() return shelfRepository.findByUserId(userId).stream()
.map(shelfMapper::toShelf) .map(shelfMapper::toShelf)
.toList(); .toList();
} }
public Shelf getShelf(Long shelfId) { public Shelf getShelf(Long shelfId) {
ShelfEntity shelfEntity = shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId)); return shelfMapper.toShelf(findShelfByIdOrThrow(shelfId));
return shelfMapper.toShelf(shelfEntity);
} }
public void deleteShelf(Long shelfId) { public void deleteShelf(Long shelfId) {
shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId)); findShelfByIdOrThrow(shelfId);
shelfRepository.deleteById(shelfId); shelfRepository.deleteById(shelfId);
} }
public List<Book> getShelfBooks(Long shelfId) { public List<Book> getShelfBooks(Long shelfId) {
shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId)); findShelfByIdOrThrow(shelfId);
List<BookEntity> bookEntities = bookRepository.findByShelfId(shelfId); return bookRepository.findByShelfId(shelfId).stream()
return bookEntities.stream()
.map(bookMapper::toBook) .map(bookMapper::toBook)
.toList(); .toList();
} }
private Long getAuthenticatedUserId() {
BookLoreUser user = authenticationService.getAuthenticatedUser();
return user.getId();
}
private BookLoreUserEntity fetchUserEntityById(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new UsernameNotFoundException("User not found with ID " + userId));
}
private ShelfEntity findShelfByIdOrThrow(Long shelfId) {
return shelfRepository.findById(shelfId)
.orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
}
} }