diff --git a/src/main/java/com/campool/controller/RentalController.kt b/src/main/java/com/campool/controller/RentalController.kt index 031845f..f839008 100644 --- a/src/main/java/com/campool/controller/RentalController.kt +++ b/src/main/java/com/campool/controller/RentalController.kt @@ -1,80 +1,63 @@ -package com.campool.controller; +package com.campool.controller + +import com.campool.service.RentalService +import com.campool.service.BookingService +import com.campool.annotation.LoginValidation +import javax.validation.Valid +import com.campool.model.RentalRegisterRequest +import com.campool.model.CampingGear +import com.campool.annotation.LoginUserId +import com.campool.model.RentalsRequestByLocation +import com.campool.model.Rental +import com.campool.model.RentalDetailsResponse +import org.springframework.transaction.annotation.Transactional +import com.campool.model.RentalCompleteRequest +import org.springframework.web.bind.annotation.* -import com.campool.annotation.LoginUserId; -import com.campool.annotation.LoginValidation; -import com.campool.model.BookingInfo; -import com.campool.model.CampingGear; -import com.campool.model.Rental; -import com.campool.model.RentalCompleteRequest; -import com.campool.model.RentalDetailsResponse; -import com.campool.model.RentalInfo; -import com.campool.model.RentalRegisterRequest; -import com.campool.model.RentalsRequestByLocation; -import com.campool.service.BookingService; -import com.campool.service.RentalService; -import java.util.List; -import javax.validation.Valid; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PatchMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -@RequiredArgsConstructor @RestController -public class RentalController { - - @NonNull - private final RentalService rentalService; - - @NonNull - private final BookingService bookingService; - +class RentalController( + private val rentalService: RentalService, + private val bookingService: BookingService +) { @LoginValidation @PostMapping("/rentals") - public void registerRental(@Valid RentalRegisterRequest rentalRegisterRequest, - @RequestBody List gears, @LoginUserId String id) { - rentalService.register(id, rentalRegisterRequest, gears); + fun registerRental( + @Valid request: RentalRegisterRequest, + @RequestBody gears: List, @LoginUserId id: String + ) { + rentalService.register(id, request, gears) } @LoginValidation @GetMapping("/rentals") - public List getRentalsByLocation( - @Valid RentalsRequestByLocation rentalsRequestByLocation) { - return rentalService.getRentalsByLocation(rentalsRequestByLocation); - } + fun getRentalsByLocation(@Valid request: RentalsRequestByLocation): List = + rentalService.getRentalsByLocation(request) @LoginValidation @GetMapping("/rentals/{id}") - public RentalDetailsResponse getRentalInfoById(@PathVariable long id) { - RentalInfo rentalInfo = rentalService.getRentalById(id); - List gears = rentalService.getGearsByRentalId(id); - return new RentalDetailsResponse(rentalInfo, gears); + fun getRentalInfoById(@PathVariable id: Long): RentalDetailsResponse { + val rentalInfo = rentalService.getRentalById(id) + val gears = rentalService.getGearsByRentalId(id) + return RentalDetailsResponse(rentalInfo, gears) } @LoginValidation @PatchMapping("/rentals/{rentalId}/rent") - public void rentCampingGears(@PathVariable long rentalId, @LoginUserId String userId) { - rentalService.updateStatusToRented(rentalId, userId); + fun rentCampingGears(@PathVariable rentalId: Long, @LoginUserId userId: String) { + rentalService.updateStatusToRented(rentalId, userId) } @LoginValidation @PostMapping("/rentals/complete") @Transactional - public void completeRental(@Valid RentalCompleteRequest request, @LoginUserId String userId) { - BookingInfo bookingInfo = bookingService.getBookingInfoById(request.getBookingId()); - rentalService.completeRental(request.getRentalId(), bookingInfo, userId); + fun completeRental(@Valid request: RentalCompleteRequest, @LoginUserId userId: String) { + val bookingInfo = bookingService.getBookingInfoById(request.bookingId) + rentalService.completeRental(request.rentalId, bookingInfo, userId) } @LoginValidation @DeleteMapping("/rentals/{rentalId}") - public void deleteRental(@PathVariable long rentalId, @LoginUserId String userId) { - rentalService.deleteRental(rentalId, userId); + fun deleteRental(@PathVariable rentalId: Long, @LoginUserId userId: String) { + rentalService.deleteRental(rentalId, userId) } - -} +} \ No newline at end of file diff --git a/src/main/java/com/campool/enumeration/RentalStatus.kt b/src/main/java/com/campool/enumeration/RentalStatus.kt index d51e2db..c195a61 100644 --- a/src/main/java/com/campool/enumeration/RentalStatus.kt +++ b/src/main/java/com/campool/enumeration/RentalStatus.kt @@ -1,14 +1,8 @@ -package com.campool.enumeration; +package com.campool.enumeration -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -@Getter -public enum RentalStatus { +enum class RentalStatus(val message: String) { TRADEABLE("거래 가능"), TRADING("거래 중"), RENTED("대여 중"), TRADE_COMPLETED("거래 완료"); - private final String message; -} +} \ No newline at end of file diff --git a/src/main/java/com/campool/mapper/RentalMapper.kt b/src/main/java/com/campool/mapper/RentalMapper.kt index a17fe0a..55551d4 100644 --- a/src/main/java/com/campool/mapper/RentalMapper.kt +++ b/src/main/java/com/campool/mapper/RentalMapper.kt @@ -1,33 +1,29 @@ -package com.campool.mapper; - -import com.campool.enumeration.RentalStatus; -import com.campool.model.CampingGear; -import com.campool.model.Rental; -import com.campool.model.RentalInfo; -import com.campool.model.RentalRegisterRequest; -import com.campool.model.RentalsRequestByLocation; -import java.time.LocalDate; -import java.util.List; -import org.apache.ibatis.annotations.Mapper; +package com.campool.mapper -@Mapper -public interface RentalMapper { - - void insertRental(String userId, RentalRegisterRequest rental, RentalStatus status); +import com.campool.model.RentalRegisterRequest +import com.campool.enumeration.RentalStatus +import com.campool.model.CampingGear +import com.campool.model.RentalInfo +import com.campool.model.RentalsRequestByLocation +import com.campool.model.Rental +import org.apache.ibatis.annotations.Mapper +import java.time.LocalDate - void insertGears(List gears); +@Mapper +interface RentalMapper { + fun insertRental(userId: String, rental: RentalRegisterRequest, status: RentalStatus) - RentalInfo findRentalInfoById(long id); + fun insertGears(gears: List) - List findGearsByRentalId(long rentalId); + fun findRentalInfoById(id: Long): RentalInfo? - List findRentalsByLocation(RentalsRequestByLocation rental, RentalStatus status, - String polygon); + fun findGearsByRentalId(rentalId: Long): List - Integer findCostByIdAndDate(long id, LocalDate startDate, LocalDate endDate); + fun findRentalsByLocation(rental: RentalsRequestByLocation, status: RentalStatus, polygon: String): List - void updateStatusById(long id, RentalStatus status); + fun findCostByIdAndDate(id: Long, startDate: LocalDate, endDate: LocalDate): Int? - void deleteById(long id); + fun updateStatusById(id: Long, status: RentalStatus) -} + fun deleteById(id: Long) +} \ No newline at end of file diff --git a/src/main/java/com/campool/model/BookingInfo.kt b/src/main/java/com/campool/model/BookingInfo.kt index ffaef5e..a3e8791 100644 --- a/src/main/java/com/campool/model/BookingInfo.kt +++ b/src/main/java/com/campool/model/BookingInfo.kt @@ -1,42 +1,21 @@ -package com.campool.model; - -import com.campool.enumeration.RentalStatus; -import java.time.LocalDate; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class BookingInfo { - - private final long bookingId; - - private final LocalDate startDate; - - private final LocalDate endDate; - - private final int amount; - - private final long rentalId; - - private final String title; - - private final String description; - - private final RentalStatus rentalStatus; - - private final double longitude; - - private final double latitude; - - private final String userId; - - private final String name; - - private final String email; - - private final String telephone; - -} +package com.campool.model + +import java.time.LocalDate +import com.campool.enumeration.RentalStatus + +data class BookingInfo( + val bookingId: Long, + val startDate: LocalDate, + val endDate: LocalDate, + val amount: Int, + val rentalId: Long, + val title: String, + val description: String, + val rentalStatus: RentalStatus, + val longitude: Double, + val latitude: Double, + val userId: String, + val name: String, + val email: String, + val telephone: String +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/Rental.kt b/src/main/java/com/campool/model/Rental.kt index 4a9c6a7..33055aa 100644 --- a/src/main/java/com/campool/model/Rental.kt +++ b/src/main/java/com/campool/model/Rental.kt @@ -1,29 +1,14 @@ -package com.campool.model; - -import java.time.LocalDate; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class Rental { - - private final int rentalId; - - private final String title; - - private final String userId; - - private final LocalDate startDate; - - private final LocalDate endDate; - - private final int cost; - - private final double longitude; - - private final double latitude; - -} +package com.campool.model + +import java.time.LocalDate + +data class Rental( + val rentalId: Int, + val title: String, + val userId: String, + val startDate: LocalDate, + val endDate: LocalDate, + val cost: Int, + val longitude: Double, + val latitude: Double +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/RentalCompleteRequest.kt b/src/main/java/com/campool/model/RentalCompleteRequest.kt index 6f641b3..7998363 100644 --- a/src/main/java/com/campool/model/RentalCompleteRequest.kt +++ b/src/main/java/com/campool/model/RentalCompleteRequest.kt @@ -1,19 +1,6 @@ -package com.campool.model; +package com.campool.model -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class RentalCompleteRequest { - - @NonNull - private final long rentalId; - - @NonNull - private final long bookingId; - -} +class RentalCompleteRequest( + val rentalId: Long, + val bookingId: Long +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/RentalDetailsResponse.kt b/src/main/java/com/campool/model/RentalDetailsResponse.kt index b8d0e4b..600b8de 100644 --- a/src/main/java/com/campool/model/RentalDetailsResponse.kt +++ b/src/main/java/com/campool/model/RentalDetailsResponse.kt @@ -1,18 +1,6 @@ -package com.campool.model; +package com.campool.model - -import java.util.List; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class RentalDetailsResponse { - - private final RentalInfo rentalInfo; - - private final List gears; - -} +class RentalDetailsResponse( + val rentalInfo: RentalInfo, + val gears: List +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/RentalInfo.kt b/src/main/java/com/campool/model/RentalInfo.kt index 991bb0e..8450887 100644 --- a/src/main/java/com/campool/model/RentalInfo.kt +++ b/src/main/java/com/campool/model/RentalInfo.kt @@ -1,34 +1,17 @@ -package com.campool.model; - -import com.campool.enumeration.RentalStatus; -import java.time.LocalDate; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class RentalInfo { - - private final int rentalId; - - private final String title; - - private final String description; - - private final RentalStatus status; - - private final String userId; - - private final LocalDate startDate; - - private final LocalDate endDate; - - private final int cost; - - private final double longitude; - - private final double latitude; - -} +package com.campool.model + +import com.campool.enumeration.RentalStatus +import java.time.LocalDate + +data class RentalInfo( + val rentalId: Long, + val title: String, + val description: String, + val status: RentalStatus, + val userId: String, + val startDate: LocalDate, + val endDate: LocalDate, + val cost: Int, + val longitude: Double, + val latitude: Double +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/RentalRegisterRequest.kt b/src/main/java/com/campool/model/RentalRegisterRequest.kt index 68bdab6..6d16e41 100644 --- a/src/main/java/com/campool/model/RentalRegisterRequest.kt +++ b/src/main/java/com/campool/model/RentalRegisterRequest.kt @@ -1,41 +1,25 @@ -package com.campool.model; - -import java.time.LocalDate; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Size; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import lombok.ToString; - -@Getter -@RequiredArgsConstructor -@ToString -public class RentalRegisterRequest { +package com.campool.model +import java.time.LocalDate +import javax.validation.constraints.NotBlank +import javax.validation.constraints.Size + +class RentalRegisterRequest( @NotBlank(message = "제목을 입력해주세요.") @Size(max = 255, message = "최대 255자리까지 입력 가능합니다.") - private final String title; + val title: String, @NotBlank(message = "설명을 입력해주세요.") @Size(max = 255, message = "최대 255자리까지 입력 가능합니다.") - private final String description; - - @NonNull - private final LocalDate startDate; + val description: String, - @NonNull - private final LocalDate endDate; + val startDate: LocalDate, - @NonNull - @Min(0) - private final int cost; + val endDate: LocalDate, - @NonNull - private final double longitude; + val cost: Int, - @NonNull - private final double latitude; + val longitude: Double, -} + val latitude: Double +) \ No newline at end of file diff --git a/src/main/java/com/campool/model/RentalsRequestByLocation.kt b/src/main/java/com/campool/model/RentalsRequestByLocation.kt index 2bdb703..436f227 100644 --- a/src/main/java/com/campool/model/RentalsRequestByLocation.kt +++ b/src/main/java/com/campool/model/RentalsRequestByLocation.kt @@ -1,30 +1,8 @@ -package com.campool.model; - -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import lombok.ToString; -import org.springframework.lang.Nullable; - -@Getter -@RequiredArgsConstructor -@ToString -public class RentalsRequestByLocation { - - @NonNull - @Max(100000) - @Min(100) - private final int distanceInMeters; - - @NonNull - private final double longitude; - - @NonNull - private final double latitude; - - @Nullable - private final Integer typeId; - -} +package com.campool.model + +class RentalsRequestByLocation( + val distanceInMeters: Int, + val longitude: Double, + val latitude: Double, + val typeId: Int +) \ No newline at end of file diff --git a/src/main/java/com/campool/service/RentalService.kt b/src/main/java/com/campool/service/RentalService.kt index 831a93b..ef36eb4 100644 --- a/src/main/java/com/campool/service/RentalService.kt +++ b/src/main/java/com/campool/service/RentalService.kt @@ -1,114 +1,82 @@ -package com.campool.service; - -import com.campool.enumeration.RentalStatus; -import com.campool.mapper.RentalMapper; -import com.campool.model.BookingInfo; -import com.campool.model.CampingGear; -import com.campool.model.Rental; -import com.campool.model.RentalInfo; -import com.campool.model.RentalRegisterRequest; -import com.campool.model.RentalsRequestByLocation; -import java.math.BigDecimal; -import java.util.List; -import java.util.NoSuchElementException; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +package com.campool.service + +import org.springframework.transaction.annotation.Transactional +import com.campool.mapper.RentalMapper +import com.campool.model.RentalRegisterRequest +import com.campool.model.CampingGear +import com.campool.enumeration.RentalStatus +import com.campool.model.RentalInfo +import java.util.NoSuchElementException +import com.campool.model.RentalsRequestByLocation +import com.campool.model.Rental +import java.math.BigDecimal +import com.campool.model.BookingInfo +import org.springframework.stereotype.Service @Transactional(readOnly = true) -@RequiredArgsConstructor @Service -public class RentalService { - - private static final BigDecimal LONGITUDE_PER_METER = new BigDecimal("0.0000113182"); - private static final BigDecimal LATITUDE_PER_METER = new BigDecimal("0.0000089426"); - - @NonNull - private final RentalMapper rentalMapper; - - @Transactional - public void register(String userId, RentalRegisterRequest rentalRegisterRequest, - List gears) { - rentalMapper.insertRental(userId, rentalRegisterRequest, RentalStatus.TRADEABLE); - rentalMapper.insertGears(gears); - } - - public RentalInfo getRentalById(long id) { - RentalInfo rentalInfo = rentalMapper.findRentalInfoById(id); - if (rentalInfo == null) { - throw new NoSuchElementException("해당하는 렌트 정보가 없습니다."); - } - return rentalInfo; +class RentalService( + private val rentalMapper: RentalMapper +) { + companion object { + private val LONGITUDE_PER_METER = BigDecimal("0.0000113182") + private val LATITUDE_PER_METER = BigDecimal("0.0000089426") } - public List getGearsByRentalId(long rentalId) { - return rentalMapper.findGearsByRentalId(rentalId); + @Transactional + fun register(userId: String, rentalRegisterRequest: RentalRegisterRequest, gears: List) { + rentalMapper.insertRental(userId, rentalRegisterRequest, RentalStatus.TRADEABLE) + rentalMapper.insertGears(gears) } - public List getRentalsByLocation(RentalsRequestByLocation rentalsRequestByLocation) { - int meters = rentalsRequestByLocation.getDistanceInMeters(); - double longitude = rentalsRequestByLocation.getLongitude(); - double latitude = rentalsRequestByLocation.getLatitude(); + fun getRentalById(id: Long): RentalInfo = + rentalMapper.findRentalInfoById(id) ?: throw NoSuchElementException("해당하는 렌트 정보가 없습니다.") - String polygon = getPolygonString(new BigDecimal(longitude), new BigDecimal(latitude), - new BigDecimal(meters)); + fun getGearsByRentalId(rentalId: Long): List = rentalMapper.findGearsByRentalId(rentalId) + fun getRentalsByLocation(rentalsRequestByLocation: RentalsRequestByLocation): List { + val meters = rentalsRequestByLocation.distanceInMeters + val longitude = rentalsRequestByLocation.longitude + val latitude = rentalsRequestByLocation.latitude + val polygon = getPolygonString(BigDecimal(longitude), BigDecimal(latitude), BigDecimal(meters)) return rentalMapper - .findRentalsByLocation(rentalsRequestByLocation, RentalStatus.TRADEABLE, polygon); + .findRentalsByLocation(rentalsRequestByLocation, RentalStatus.TRADEABLE, polygon) } - private String getPolygonString(BigDecimal longitude, BigDecimal latitude, BigDecimal meters) { - BigDecimal differenceX = LONGITUDE_PER_METER.multiply(meters); - BigDecimal differenceY = LATITUDE_PER_METER.multiply(meters); - - BigDecimal X1 = longitude.subtract(differenceX); - BigDecimal X2 = longitude.add(differenceX); - BigDecimal Y1 = latitude.subtract(differenceY); - BigDecimal Y2 = latitude.add(differenceY); - - return "POLYGON((" - + X1 + " " + Y1 + "," - + X2 + " " + Y1 + "," - + X2 + " " + Y2 + "," - + X1 + " " + Y2 + "," - + X1 + " " + Y1 + "))"; + private fun getPolygonString(longitude: BigDecimal, latitude: BigDecimal, meters: BigDecimal): String { + val differenceX = LONGITUDE_PER_METER.multiply(meters) + val differenceY = LATITUDE_PER_METER.multiply(meters) + val x1 = longitude.subtract(differenceX) + val x2 = longitude.add(differenceX) + val y1 = latitude.subtract(differenceY) + val y2 = latitude.add(differenceY) + return ("POLYGON(($x1 $y1, $x2 $y1, $x2 $y2, $x1 $y2, $x1 $y1))") } - public void updateStatusToRented(long rentalId, String userId) { - updateNewStatus(RentalStatus.TRADING, RentalStatus.RENTED, rentalId, userId); + fun updateStatusToRented(rentalId: Long, userId: String) { + updateNewStatus(RentalStatus.TRADING, RentalStatus.RENTED, rentalId, userId) } - public void completeRental(long rentalId, BookingInfo bookingInfo, String userId) { - if (rentalId != bookingInfo.getRentalId() || !userId.equals(bookingInfo.getUserId())) { - throw new IllegalArgumentException("유효하지 않는 요청입니다."); - } - - updateNewStatus(RentalStatus.RENTED, RentalStatus.TRADE_COMPLETED, rentalId, userId); + fun completeRental(rentalId: Long, bookingInfo: BookingInfo, userId: String) { + require(!(rentalId != bookingInfo.rentalId || userId != bookingInfo.userId)) { "유효하지 않는 요청입니다." } + updateNewStatus(RentalStatus.RENTED, RentalStatus.TRADE_COMPLETED, rentalId, userId) } @Transactional - public void updateNewStatus(RentalStatus currentStatus, RentalStatus newStatus, long rentalId, - String userId) { - if (!isValidRentalStatus(getRentalById(rentalId), currentStatus, userId)) { - throw new IllegalStateException(currentStatus.getMessage() + "인 상태가 아닙니다."); - } - - rentalMapper.updateStatusById(rentalId, newStatus); + fun updateNewStatus(currentStatus: RentalStatus, newStatus: RentalStatus, rentalId: Long, userId: String) { + check(isValidRentalStatus(getRentalById(rentalId), currentStatus, userId)) + { currentStatus.message + "인 상태가 아닙니다." } + rentalMapper.updateStatusById(rentalId, newStatus) } - private boolean isValidRentalStatus(RentalInfo rentalInfo, RentalStatus status, String userId) { - return rentalInfo.getStatus() == status && rentalInfo.getUserId().equals(userId); - } + private fun isValidRentalStatus(rentalInfo: RentalInfo, status: RentalStatus, userId: String): Boolean = + rentalInfo.status == status && rentalInfo.userId == userId @Transactional - public void deleteRental(long rentalId, String userId) { - RentalInfo rentalInfo = rentalMapper.findRentalInfoById(rentalId); - if (rentalInfo.getStatus() != RentalStatus.TRADEABLE - || !rentalInfo.getUserId().equals(userId)) { - throw new IllegalStateException("본인이 등록하고 거래 가능 상태일 때만 삭제할 수 있습니다."); - } - rentalMapper.deleteById(rentalId); + fun deleteRental(rentalId: Long, userId: String) { + val rentalInfo = rentalMapper.findRentalInfoById(rentalId)!! + check(!(rentalInfo.status != RentalStatus.TRADEABLE || rentalInfo.userId != userId)) + { "본인이 등록하고 거래 가능 상태일 때만 삭제할 수 있습니다." } + rentalMapper.deleteById(rentalId) } - -} +} \ No newline at end of file