Skip to content

Commit

Permalink
feat(#23) : 항공권 전체 조회 및 검색어 조회 기능 구현 및 응답/요청 dto 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
DebbieIsFree committed May 20, 2024
1 parent a1dc37a commit 25ebc0d
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.EnjoyTripBackend.controller;

import com.example.EnjoyTripBackend.dto.NonPagingResponseResult;
import com.example.EnjoyTripBackend.dto.ResponseResult;
import com.example.EnjoyTripBackend.dto.airplane.AirplaneRequestDto;
import com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto;
import com.example.EnjoyTripBackend.service.AirplaneService;
import com.example.EnjoyTripBackend.util.LimitedSizePagination;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AirplaneController {

private final AirplaneService airplaneService;

@GetMapping("/airplanes")
public ResponseEntity<ResponseResult<List<AirplaneResponseDto>>> airplaneList(@PageableDefault(size = 20) Pageable pageable){
return ResponseEntity.ok().body(airplaneService.airplaneList(pageable));
}

@GetMapping("/airplanes/{id}")
public ResponseEntity<NonPagingResponseResult<AirplaneResponseDto>> airplaneDetail(@PathVariable("id")Long id){
return ResponseEntity.ok().body(airplaneService.findById(id));
}

@PostMapping("/airplanes/search")
@LimitedSizePagination(maxSize = 20)
public ResponseEntity<ResponseResult<List<AirplaneResponseDto>>> airplaneSearchList(@PageableDefault(size = 20) Pageable pageable, @RequestBody AirplaneRequestDto requestDto){
return ResponseEntity.ok().body(airplaneService.airplaneSearchList(pageable, requestDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.EnjoyTripBackend.dto.airplane;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class AirplaneRequestDto {
private String depAirportNm;
private String arrAirportNm;
private String depPlandTime;
private String airlineNm;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.EnjoyTripBackend.dto.airplane;

import lombok.*;

@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AirplaneResponseDto {
private Long id;
private String airlineNm;
private String arrAirportNm;
private String depAirportNm;
private String arrPlandTime;
private String depPlandTime;
private String economyCharge;
private String prestigeCharge;
private String vihicleId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.EnjoyTripBackend.repository;

import com.example.EnjoyTripBackend.domain.Airplane;
import com.example.EnjoyTripBackend.dto.PageRequestList;
import com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Optional;

@Mapper
public interface AirplaneRepository {
Long save(Airplane airplane);
List<AirplaneResponseDto> findAll(PageRequestList<?> requestList);
Optional<AirplaneResponseDto> findById(Long id);
List<AirplaneResponseDto> findAllBySearch(PageRequestList<?> requestList);
Long findTotalCount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.EnjoyTripBackend.service;

import com.example.EnjoyTripBackend.domain.Airplane;
import com.example.EnjoyTripBackend.dto.NonPagingResponseResult;
import com.example.EnjoyTripBackend.dto.PageRequestList;
import com.example.EnjoyTripBackend.dto.ResponseResult;
import com.example.EnjoyTripBackend.dto.airplane.AirplaneRequestDto;
import com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto;
import com.example.EnjoyTripBackend.dto.golf.GolfRequestDto;
import com.example.EnjoyTripBackend.dto.golf.GolfResponseDto;
import com.example.EnjoyTripBackend.dto.place.PlaceResponseDto;
import com.example.EnjoyTripBackend.exception.EnjoyTripException;
import com.example.EnjoyTripBackend.exception.ErrorCode;
import com.example.EnjoyTripBackend.repository.AirplaneRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class AirplaneService {

private final AirplaneRepository airplaneRepository;

@Transactional
public void save(AirplaneResponseDto airplaneResponseDto) {
Airplane airplane = Airplane.builder()
.airlineNm(airplaneResponseDto.getAirlineNm())
.arrAirportNm(airplaneResponseDto.getArrAirportNm())
.depAirportNm(airplaneResponseDto.getDepAirportNm())
.arrPlandTime(airplaneResponseDto.getArrPlandTime())
.depPlandTime(airplaneResponseDto.getDepPlandTime())
.economyCharge(airplaneResponseDto.getEconomyCharge())
.prestigeCharge(airplaneResponseDto.getPrestigeCharge())
.vihicleId(airplaneResponseDto.getVihicleId())
.build();

airplaneRepository.save(airplane);
}

public ResponseResult<List<AirplaneResponseDto>> airplaneList(Pageable pageable) {
PageRequestList<?> requestList = PageRequestList.builder()
.pageable(pageable)
.build();

long totalCount = airplaneRepository.findTotalCount();
int totalPages = (int) Math.ceil((double) totalCount / pageable.getPageSize());

return ResponseResult.of("항공권 정보 게시글 목록입니다.", airplaneRepository.findAll(requestList),totalPages);
}

public NonPagingResponseResult<AirplaneResponseDto> findById(Long id) {
return NonPagingResponseResult.of("항공권 상세 정보 게시글 입니다.", airplaneRepository.findById(id).orElseThrow(() -> new EnjoyTripException(ErrorCode.CONTENT_NOT_FOUNT)));
}

public ResponseResult<List<AirplaneResponseDto>> airplaneSearchList(Pageable pageable, AirplaneRequestDto airplaneRequestDto) {
PageRequestList<?> requestList = PageRequestList.builder()
.pageable(pageable)
.data(airplaneRequestDto)
.build();
List<AirplaneResponseDto> airplanes = airplaneRepository.findAllBySearch(requestList);
long totalCount = airplanes.size();
int totalPages = (int) Math.ceil((double) totalCount / pageable.getPageSize());
return ResponseResult.of("검색어 기반 항공권 정보 게시글 목록입니다.", airplanes, totalPages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<mapper resource="mapper/place-query.xml"/>
<mapper resource="mapper/golf-query.xml"/>
<mapper resource="mapper/payment-query.xml"/>
<mapper resource="mapper/airplane-query.xml"/>
</mappers>
</configuration>
43 changes: 43 additions & 0 deletions EnjoyTripBackend/src/main/resources/mapper/airplane-query.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.EnjoyTripBackend.repository.AirplaneRepository">

<insert id="save" parameterType="com.example.EnjoyTripBackend.domain.Airplane">
INSERT
INTO airplane (airlineNm, arrAirportNm, depAirportNm, arrPlandTime, depPlandTime, economyCharge, prestigeCharge, vihicleId)
VALUES (#{airlineNm}, #{arrAirportNm}, #{depAirportNm}, #{arrPlandTime}, #{depPlandTime}, #{economyCharge}, #{prestigeCharge}, #{vihicleId})
</insert>

<select id="findAll" parameterType="com.example.EnjoyTripBackend.dto.PageRequestList" resultType="com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto">
SELECT *
FROM airplane
LIMIT #{pageable.offset}, #{pageable.pageSize}
</select>

<select id="findById" parameterType="Long" resultType="com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto">
SELECT *
FROM airplane
WHERE id = #{id}
</select>

<select id="findAllBySearch" parameterType="com.example.EnjoyTripBackend.dto.PageRequestList" resultType="com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto">
select *
from airplane
WHERE depAirportNm = #{data.depAirportNm}
AND arrAirportNm = #{data.arrAirportNm}
AND SUBSTRING(depPlandTime, 1, 8) = #{data.depPlandTime}
<if test="data.airlineNm != null and data.airlineNm != ''">
AND airlineNm = #{data.airlineNm}
</if>
limit #{pageable.pageSize}
offset #{pageable.offset}
</select>

<select id="findTotalCount" resultType="Long">
SELECT COUNT(*)
FROM airplane
</select>

</mapper>

0 comments on commit 25ebc0d

Please sign in to comment.