Skip to content

Commit

Permalink
Merge pull request #148 from phuuthanh-dev/develop
Browse files Browse the repository at this point in the history
fix filter auctions
  • Loading branch information
phuuthanh-dev authored Jul 20, 2024
2 parents e399b33 + 60c6c4e commit 5afff15
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ public ResponseEntity<Page<Auction>> getAllAuctionsSortedAndPaged(
return ResponseEntity.ok(auctionService.getAllAuctions(auctionState, pageable, auctionName, categoryId));
}

@GetMapping("/get-by-day/{startDate}/{endDate}")
public ResponseEntity<List<Auction>> getAllAuctionsSortedAndPaged(
@PathVariable String startDate,
@PathVariable String endDate) {
return ResponseEntity.ok(auctionService.findAuctionSortByBetweenStartDayAndEndDay(startDate, endDate));
@GetMapping("/get-by-day")
public ResponseEntity<Page<Auction>> getAllAuctionsSortedAndPaged(
@RequestParam(defaultValue = "state") String sortBy,
@RequestParam String startDate,
@RequestParam String endDate,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "5") int size,
@RequestParam(defaultValue = "desc") String sortOrder) {
Sort.Direction direction = (sortOrder.equalsIgnoreCase("asc")) ? Sort.Direction.ASC : Sort.Direction.DESC;
Pageable pageable = PageRequest.of(page, size, direction, sortBy);
return ResponseEntity.ok(auctionService.findAuctionSortByBetweenStartDayAndEndDay(startDate, endDate, pageable));
}

@GetMapping("/get-all")
Expand All @@ -65,11 +71,6 @@ public ResponseEntity<Auction> getAuctionById(@PathVariable Integer id) {
return ResponseEntity.ok(auctionService.getAuctionById(id));
}

@GetMapping("/get-by-name/{key}")
public ResponseEntity<List<Auction>> getAuctionByName(@PathVariable String key) {
return ResponseEntity.ok(auctionService.findAuctionByName(key));
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteAuction(@PathVariable Integer id) {
auctionService.deleteAuction(id);
Expand All @@ -89,11 +90,11 @@ public List<Auction> getAuctionsByState(@RequestParam AuctionState state) {

@GetMapping("/get-by-states")
public ResponseEntity<Page<Auction>> getAllAuctionsSortedAndPaged(
@RequestParam(defaultValue = "id") String sortBy,
@RequestParam(defaultValue = "state") String sortBy,
@RequestParam(defaultValue = "") List<AuctionState> states,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "5") int size,
@RequestParam(defaultValue = "asc") String sortOrder) {
@RequestParam(defaultValue = "desc") String sortOrder) {
Sort.Direction direction = (sortOrder.equalsIgnoreCase("asc")) ? Sort.Direction.ASC : Sort.Direction.DESC;
Pageable pageable = PageRequest.of(page, size, direction, sortBy);
return ResponseEntity.ok(auctionService.getAuctionsByStates(states, pageable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import vn.webapp.backend.auction.dto.SendJewelryFromUserRequest;
import vn.webapp.backend.auction.enums.JewelryState;
import vn.webapp.backend.auction.model.Jewelry;
import vn.webapp.backend.auction.model.RequestApproval;
import vn.webapp.backend.auction.model.User;
import vn.webapp.backend.auction.service.jewelry.JewelryService;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public interface AuctionRepository extends JpaRepository<Auction, Integer> {

@Query("SELECT a FROM Auction a WHERE a.startDate >= :startday AND a.endDate <= :endday")
List<Auction> findAuctionSortByBetweenStartdayAndEndday(@Param("startday") Timestamp startday, @Param("endday") Timestamp endday);
@Query("SELECT a FROM Auction a WHERE a.startDate >= :startday AND a.startDate <= :endday AND a.state <> 'DELETED'")
Page<Auction> findAuctionSortByBetweenStartDayAndEndDay(@Param("startday") Timestamp startday, @Param("endday") Timestamp endday, Pageable pageable);

@Query("SELECT a FROM Auction a WHERE :auctionName = '' OR a.name LIKE %:auctionName%")
List<Auction> findAuctionByNameContaining(@Param("auctionName") String auctionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface AuctionService {

List<Auction> getAuctionByState(AuctionState state);

List<Auction> findAuctionSortByBetweenStartDayAndEndDay(String startDay, String endDay);
Page<Auction> findAuctionSortByBetweenStartDayAndEndDay(String startDay, String endDay, Pageable pageable);

Page<Auction> getByStaffID(Integer id, String auctionName, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void deleteAuction(Integer id) {
}

@Override
public List<Auction> findAuctionSortByBetweenStartDayAndEndDay(String startDay, String endDay) {
public Page<Auction> findAuctionSortByBetweenStartDayAndEndDay(String startDay, String endDay, Pageable pageable) {
LocalDate localDate1 = LocalDate.parse(startDay);
LocalDate localDate2 = LocalDate.parse(endDay);

Expand All @@ -68,7 +68,7 @@ public List<Auction> findAuctionSortByBetweenStartDayAndEndDay(String startDay,
Timestamp timestampEndDate2 = Timestamp.valueOf(endOfDay);


return auctionRepository.findAuctionSortByBetweenStartdayAndEndday(timestampStartDate1, timestampEndDate2);
return auctionRepository.findAuctionSortByBetweenStartDayAndEndDay(timestampStartDate1, timestampEndDate2, pageable);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spring.banner.location=banner.txt
# Config db
spring.datasource.url=jdbc:sqlserver://localhost:1433;encrypt=true;trustServerCertificate=true;databaseName=DB_AUCTION;
spring.datasource.username=sa
spring.datasource.password=12345
spring.datasource.password=Thanhth@nh1

# Config create table automatically
spring.jpa.hibernate.ddl-auto=update
Expand Down
38 changes: 0 additions & 38 deletions src/test/java/vn/webapp/backend/auction/AuctionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,6 @@ public void testGetAuctionByStateReturnsWell() {
}
}

@Test
public void testFindAuctionSortByBetweenStartdayAndEnddayReturnsWell() {
// Expected
String startDay = "2023-01-01";
String endDay = "2023-12-31";
LocalDate localDate1 = LocalDate.parse(startDay);
LocalDate localDate2 = LocalDate.parse(endDay);

Timestamp timestampStartDate1 = Timestamp.valueOf(localDate1.atStartOfDay());

LocalDateTime endOfDay = LocalDateTime.of(localDate2, LocalTime.MAX);
Timestamp timestampEndDate2 = Timestamp.valueOf(endOfDay);

// Act
List<Auction> auctions = auctionService.findAuctionSortByBetweenStartDayAndEndDay(startDay, endDay);

// Assert
assertNotNull(auctions);
assertFalse(auctions.isEmpty());
for (Auction auction : auctions) {
assertTrue(auction.getStartDate().after(timestampStartDate1) || auction.getStartDate().equals(timestampStartDate1));
assertTrue(auction.getEndDate().before(timestampEndDate2) || auction.getEndDate().equals(timestampEndDate2));
}
}

@Test
public void testGetCurrentAuctionByJewelryIdReturnsWell() {
// Expected
Expand Down Expand Up @@ -144,19 +119,6 @@ public void testGetAuctionByStateReturnsEmptyList() {
// Act
List<Auction> auctions = auctionService.getAuctionByState(state);

// Assert
assertNotNull(auctions);
assertTrue(auctions.isEmpty());
}
@Test
public void testFindAuctionSortByBetweenStartdayAndEnddayReturnsEmptyList() {
// Specify a time period where no auctions exist
String startDay = "2024-01-01";
String endDay = "2024-01-02";

// Act
List<Auction> auctions = auctionService.findAuctionSortByBetweenStartDayAndEndDay(startDay, endDay);

// Assert
assertNotNull(auctions);
assertTrue(auctions.isEmpty());
Expand Down

0 comments on commit 5afff15

Please sign in to comment.