Skip to content

Commit

Permalink
[SERVER] update BookingMapper: sort list before mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Jul 20, 2020
1 parent 30cf217 commit e7b073f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bht.saigonparking.service.booking.entity;

import java.sql.Timestamp;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
Expand Down Expand Up @@ -40,6 +42,8 @@
@Table(name = "[BOOKING]")
public final class BookingEntity extends BaseEntity {

public static final Comparator<Map.Entry<BookingEntity, String>> SORT_BY_CREATED_AT = new SortByCreatedAt();

@Column(name = "[PARKING_LOT_ID]", nullable = false)
private Long parkingLotId;

Expand All @@ -64,4 +68,13 @@ public final class BookingEntity extends BaseEntity {
@EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "bookingEntity", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<BookingHistoryEntity> bookingHistoryEntitySet;


@NoArgsConstructor
private static final class SortByCreatedAt implements Comparator<Map.Entry<BookingEntity, String>> {
@Override
public int compare(Map.Entry<BookingEntity, String> bookingEntry1, Map.Entry<BookingEntity, String> bookingEntry2) {
return bookingEntry1.getKey().createdAt.compareTo(bookingEntry2.getKey().createdAt);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bht.saigonparking.service.booking.entity;

import java.sql.Timestamp;
import java.util.Comparator;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -37,6 +38,8 @@
@Table(name = "[BOOKING_HISTORY]")
public final class BookingHistoryEntity extends BaseEntity {

public static final Comparator<BookingHistoryEntity> SORT_BY_LAST_UPDATED = new SortByLastUpdated();

@ManyToOne(optional = false)
@JoinColumn(name = "[BOOKING_ID]", referencedColumnName = "[ID]", nullable = false, updatable = false)
private BookingEntity bookingEntity;
Expand All @@ -52,4 +55,12 @@ public final class BookingHistoryEntity extends BaseEntity {
@EqualsAndHashCode.Exclude
@Column(name = "[LAST_UPDATED]", nullable = false, updatable = false)
private Timestamp lastUpdated;

@NoArgsConstructor
private static final class SortByLastUpdated implements Comparator<BookingHistoryEntity> {
@Override
public int compare(BookingHistoryEntity history1, BookingHistoryEntity history2) {
return history1.lastUpdated.compareTo(history2.lastUpdated);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.bht.saigonparking.service.booking.entity.BookingHistoryEntity;

/**
*
* implements all necessary mappers for BookingService
*
* @author bht
*/
Expand Down Expand Up @@ -90,11 +92,17 @@ public interface BookingMapper {

@Named("toBookingList")
default List<Booking> toBookingList(@NotNull Map<BookingEntity, String> bookingEntityParkingLotNameMap) {
return bookingEntityParkingLotNameMap.entrySet().stream().map(this::toBooking).collect(Collectors.toList());
return bookingEntityParkingLotNameMap.entrySet().stream()
.sorted(BookingEntity.SORT_BY_CREATED_AT)
.map(this::toBooking)
.collect(Collectors.toList());
}

@Named("toBookingHistoryList")
default List<BookingHistory> toBookingHistoryList(@NotNull Set<BookingHistoryEntity> bookingHistoryEntitySet) {
return bookingHistoryEntitySet.stream().map(this::toBookingHistory).collect(Collectors.toList());
return bookingHistoryEntitySet.stream()
.sorted(BookingHistoryEntity.SORT_BY_LAST_UPDATED)
.map(this::toBookingHistory)
.collect(Collectors.toList());
}
}

0 comments on commit e7b073f

Please sign in to comment.