Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

주차장 상세조회 #56

Merged
merged 14 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.parking.api.parking;

import com.example.parking.application.parking.ParkingService;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse;
import com.example.parking.application.parking.dto.ParkingLotsResponse;
import com.example.parking.application.parking.dto.ParkingQueryRequest;
import com.example.parking.application.parking.dto.ParkingSearchConditionRequest;
Expand All @@ -11,8 +12,10 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "주차장 컨트롤러")
Expand All @@ -22,6 +25,14 @@ public class ParkingController {

private final ParkingService parkingService;

@Operation(summary = "주차장 상세조회", description = "주차장 상세조회")
@GetMapping("/parkings/{parkingId}")
public ResponseEntity<ParkingDetailInfoResponse> findParking(@PathVariable Long parkingId) {
ParkingDetailInfoResponse parkingDetailInfoResponse = parkingService.findParking(parkingId);
return ResponseEntity.status(HttpStatus.OK).body(parkingDetailInfoResponse);

}

@Operation(summary = "주차장 반경 조회", description = "주차장 반경 조회")
@GetMapping("/parkings")
public ResponseEntity<ParkingLotsResponse> find(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.example.parking.application.parking;

import com.example.parking.application.SearchConditionMapper;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse.FeeInfo;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse.HolidayOperatingTime;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse.SaturdayOperatingTime;
import com.example.parking.application.parking.dto.ParkingDetailInfoResponse.WeekdayOperatingTime;
import com.example.parking.application.parking.dto.ParkingLotsResponse;
import com.example.parking.application.parking.dto.ParkingLotsResponse.ParkingResponse;
import com.example.parking.application.parking.dto.ParkingQueryRequest;
import com.example.parking.application.parking.dto.ParkingSearchConditionRequest;
import com.example.parking.application.review.ReviewService;
import com.example.parking.application.review.dto.ReviewInfoResponse;
import com.example.parking.domain.favorite.Favorite;
import com.example.parking.domain.favorite.FavoriteRepository;
import com.example.parking.domain.parking.Fee;
Expand Down Expand Up @@ -38,6 +45,7 @@ public class ParkingService {
private final FavoriteRepository favoriteRepository;
private final SearchConditionMapper searchConditionMapper;
private final ParkingFeeCalculator parkingFeeCalculator;
private final ReviewService reviewService;

@Transactional(readOnly = true)
public ParkingLotsResponse findParkingLots(ParkingQueryRequest parkingQueryRequest,
Expand Down Expand Up @@ -137,4 +145,42 @@ public void saveAll(List<Parking> parkingLots) {
public Set<Parking> getParkingLots(Set<String> parkingNames) {
return parkingRepository.findAllByBaseInformationNameIn(parkingNames);
}

@Transactional(readOnly = true)
public ParkingDetailInfoResponse findParking(Long parkingId) {
LocalDateTime now = LocalDateTime.now();
Parking parking = parkingRepository.getById(parkingId);
ReviewInfoResponse reviews = reviewService.readReviews(parkingId);
int diffMinute = parking.calculateUpdatedDiff(now);

return toParkingResponse(reviews, parking, diffMinute);
}

private ParkingDetailInfoResponse toParkingResponse(ReviewInfoResponse reviews, Parking parking, int diffMinute) {
return new ParkingDetailInfoResponse(
parking.getBaseInformation().getName(),
parking.getBaseInformation().getParkingType().getDescription(),
parking.getLocation().getLatitude(),
parking.getLocation().getLongitude(),
new FeeInfo(
parking.getFeePolicy().getBaseFee().getFee(),
parking.getFeePolicy().getBaseTimeUnit().getTimeUnit()
),
parking.getSpace().getCurrentParking(),
parking.getSpace().getCapacity(),
diffMinute,
parking.getBaseInformation().getTel(),
parking.getBaseInformation().getPayTypes().getDescription(),
new WeekdayOperatingTime(
parking.getOperatingTime().getWeekdayBeginTime(),
parking.getOperatingTime().getWeekdayEndTime()),
new SaturdayOperatingTime(
parking.getOperatingTime().getSaturdayBeginTime(),
parking.getOperatingTime().getSaturdayEndTime()),
new HolidayOperatingTime(
parking.getOperatingTime().getHolidayBeginTime(),
parking.getOperatingTime().getHolidayEndTime()),
reviews
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.example.parking.application.parking.dto;

import com.example.parking.application.review.dto.ReviewInfoResponse;
import java.time.LocalTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ParkingDetailInfoResponse {

private String parkingName;
private String parkingType;
private Double latitude;
private Double longitude;
private FeeInfo feeInfo;
private Integer currentParkingCount;
private Integer capacity;
private Integer lastUpdated;
private String tel;
private String paymentType;
private WeekdayOperatingTime weekdayOperatingTime;
private SaturdayOperatingTime saturdayOperatingTime;
private HolidayOperatingTime holidayOperatingTime;
private ReviewInfoResponse reviewInfo;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ 운영시간

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api 스펙에 없어서 놓쳤네여 추가햇슴다


public ParkingDetailInfoResponse(String parkingName, String parkingType, Double latitude, Double longitude,
FeeInfo feeInfo,
Integer currentParkingCount, Integer capacity, Integer lastUpdated, String tel,
String paymentType, WeekdayOperatingTime weekdayOperatingTime,
SaturdayOperatingTime saturdayOperatingTime,
HolidayOperatingTime holidayOperatingTime, ReviewInfoResponse reviewInfo) {
this.parkingName = parkingName;
this.parkingType = parkingType;
this.latitude = latitude;
this.longitude = longitude;
this.feeInfo = feeInfo;
this.currentParkingCount = currentParkingCount;
this.capacity = capacity;
this.lastUpdated = lastUpdated;
this.tel = tel;
this.paymentType = paymentType;
this.weekdayOperatingTime = weekdayOperatingTime;
this.saturdayOperatingTime = saturdayOperatingTime;
this.holidayOperatingTime = holidayOperatingTime;
this.reviewInfo = reviewInfo;
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class FeeInfo {
private Integer fee;
private Integer time;

public FeeInfo(Integer fee, Integer time) {
this.fee = fee;
this.time = time;
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class WeekdayOperatingTime {
private LocalTime beginTime;
private LocalTime endTime;

public WeekdayOperatingTime(LocalTime beginTime, LocalTime endTime) {
this.beginTime = beginTime;
this.endTime = endTime;
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class SaturdayOperatingTime {
private LocalTime beginTime;
private LocalTime endTime;

public SaturdayOperatingTime(LocalTime beginTime, LocalTime endTime) {
this.beginTime = beginTime;
this.endTime = endTime;
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class HolidayOperatingTime {
private LocalTime beginTime;
private LocalTime endTime;

public HolidayOperatingTime(LocalTime beginTime, LocalTime endTime) {
this.beginTime = beginTime;
this.endTime = endTime;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Embeddable
public class FeePolicy {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import java.time.LocalTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Embeddable
public class OperatingTime {

public static final OperatingTime ALWAYS_OPEN = new OperatingTime(TimeInfo.ALL_DAY, TimeInfo.ALL_DAY, TimeInfo.ALL_DAY);
public static final OperatingTime ALWAYS_OPEN = new OperatingTime(TimeInfo.ALL_DAY, TimeInfo.ALL_DAY,
TimeInfo.ALL_DAY);

@AttributeOverride(name = "beginTime", column = @Column(name = "weekday_begin_time"))
@AttributeOverride(name = "endTime", column = @Column(name = "weekday_end_time"))
Expand All @@ -35,4 +39,28 @@ public OperatingTime(TimeInfo weekday,
this.saturday = saturday;
this.holiday = holiday;
}

public LocalTime getWeekdayBeginTime() {
return weekday.getBeginTime();
}

public LocalTime getWeekdayEndTime() {
return weekday.getEndTime();
}

public LocalTime getSaturdayBeginTime() {
return saturday.getBeginTime();
}

public LocalTime getSaturdayEndTime() {
return saturday.getEndTime();
}

public LocalTime getHolidayBeginTime() {
return holiday.getBeginTime();
}

public LocalTime getHolidayEndTime() {
return holiday.getEndTime();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/example/parking/domain/parking/Parking.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import lombok.AccessLevel;
Expand All @@ -18,6 +20,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Parking extends AuditingEntity {

private static final int MINUTE_UNIT = 60;

private static final int AVERAGE_WALKING_SPEED = 5;

@Id
Expand Down Expand Up @@ -126,6 +130,12 @@ private double calculateDistanceToDestination(Location destination) {
return 2 * radius * Math.asin(squareRoot);
}

public int calculateUpdatedDiff(LocalDateTime now) {
Duration diff = Duration.between(now, getUpdatedAt());
Long diffMinute = diff.getSeconds() / MINUTE_UNIT;
return diffMinute.intValue();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/parking/domain/parking/Space.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Space {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.example.parking.domain.parking;

import jakarta.persistence.Embeddable;
import java.time.LocalTime;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class TimeInfo {
Expand All @@ -25,7 +26,8 @@ public int calculateOverlapMinutes(LocalTime beginTime, LocalTime endTime) {
if (this.endTime.isBefore(this.beginTime)) {
TimeInfo today = new TimeInfo(this.beginTime, LocalTime.MAX);
TimeInfo tomorrow = new TimeInfo(LocalTime.MIN, this.endTime);
return today.calculateOverlapMinutes(beginTime, endTime) + tomorrow.calculateOverlapMinutes(beginTime, endTime);
return today.calculateOverlapMinutes(beginTime, endTime) + tomorrow.calculateOverlapMinutes(beginTime,
endTime);
}
LocalTime overlapBeginTime = decideOverlapBeginTime(beginTime);
LocalTime overlapEndTime = decideOverlapEndTime(endTime);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.parking.domain.parking.repository;

import com.example.parking.domain.parking.Parking;
import com.example.parking.support.exception.DomainException;
import com.example.parking.support.exception.ExceptionInformation;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -12,7 +14,7 @@
public interface ParkingRepository extends Repository<Parking, Long> {

default Parking getById(Long id) {
return findById(id).orElseThrow(() -> new RuntimeException("익셉션 !!"));
return findById(id).orElseThrow(() -> new DomainException(ExceptionInformation.INVALID_PARKING));
}

Optional<Parking> findById(Long id);
Expand Down
Loading
Loading