Sorting works with the BE! Yay! Need to improve the the code though
This commit is contained in:
+6
-5
@@ -4,17 +4,13 @@ import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO;
|
||||
import com.adityachandel.booklore.model.dto.LibraryDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
||||
import com.adityachandel.booklore.model.entity.Sort;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import com.adityachandel.booklore.service.LibraryService;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -62,4 +58,9 @@ public class LibraryController {
|
||||
List<BookDTO> books = libraryService.getBooks(libraryId);
|
||||
return ResponseEntity.ok(books);
|
||||
}
|
||||
|
||||
@PutMapping("/{libraryId}/sort")
|
||||
public ResponseEntity<LibraryDTO> updateLibrary(@PathVariable long libraryId, @RequestBody Sort sort) {
|
||||
return ResponseEntity.ok(libraryService.updateSort(libraryId, sort));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class SortConverter implements AttributeConverter<Sort, String> {
|
||||
if (sort == null) {
|
||||
return null;
|
||||
}
|
||||
return sort.getSortField() + "," + sort.getSortDirection().name();
|
||||
return sort.getField() + "," + sort.getDirection().name();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -23,8 +23,8 @@ public class SortConverter implements AttributeConverter<Sort, String> {
|
||||
}
|
||||
String[] parts = dbData.split(",");
|
||||
Sort sort = new Sort();
|
||||
sort.setSortField(parts[0]);
|
||||
sort.setSortDirection(SortDirection.valueOf(parts[1]));
|
||||
sort.setField(parts[0]);
|
||||
sort.setDirection(SortDirection.valueOf(parts[1]));
|
||||
return sort;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Sort {
|
||||
private String sortField;
|
||||
private SortDirection sortDirection;
|
||||
private String field;
|
||||
private SortDirection direction;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.adityachandel.booklore.model.dto.LibraryDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
||||
import com.adityachandel.booklore.model.entity.Book;
|
||||
import com.adityachandel.booklore.model.entity.Library;
|
||||
import com.adityachandel.booklore.model.entity.Sort;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
import com.adityachandel.booklore.transformer.BookTransformer;
|
||||
@@ -13,8 +14,6 @@ import com.adityachandel.booklore.transformer.LibraryTransformer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -78,4 +77,10 @@ public class LibraryService {
|
||||
List<Book> books = bookRepository.findBooksByLibraryId(libraryId);
|
||||
return books.stream().map(BookTransformer::convertToBookDTO).toList();
|
||||
}
|
||||
|
||||
public LibraryDTO updateSort(long libraryId, Sort sort) {
|
||||
Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
library.setSort(sort);
|
||||
return LibraryTransformer.convertToLibraryDTO(libraryRepository.save(library));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user