Skip to content

Commit

Permalink
feat: 이동수단 API 수정
Browse files Browse the repository at this point in the history
resolve: #3
  • Loading branch information
BinarySstar committed Nov 23, 2024
1 parent 4f9165f commit b7b636a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -32,8 +31,8 @@ public class VehicleController {
@ApiResponse(responseCode = "404", description = "회원 세션이 존재하지 않음", content = @Content(schema = @Schema(implementation = VehicleResponse.class)))
})
@GetMapping("/list")
public ResponseEntity<List<VehicleResponse>> getVehicles(HttpSession session) {
return ResponseEntity.ok(vehicleService.getVehicles(session));
public ResponseEntity<List<VehicleResponse>> getVehicles(@RequestParam String memberId) {
return ResponseEntity.ok(vehicleService.getVehicles(memberId));
}

@Operation(summary = "이동 수단 추가", description = "현재 로그인한 회원의 이동 수단을 추가합니다.")
Expand All @@ -46,9 +45,9 @@ public ResponseEntity<List<VehicleResponse>> getVehicles(HttpSession session) {
@ApiResponse(responseCode = "404", description = "회원 세션이 존재하지 않음", content = @Content(schema = @Schema(implementation = VehicleResponse.class)))
})
@PostMapping("/add")
public ResponseEntity<VehicleResponse> addVehicle(HttpSession session,
public ResponseEntity<VehicleResponse> addVehicle(@RequestParam String memberId,
@RequestBody VehicleRequest request) {
return ResponseEntity.ok(vehicleService.addVehicle(session, request));
return ResponseEntity.ok(vehicleService.addVehicle(memberId, request));
}

@Operation(summary = "이동 수단 수정", description = "현재 로그인한 회원의 이동 수단을 수정합니다.")
Expand All @@ -61,9 +60,9 @@ public ResponseEntity<VehicleResponse> addVehicle(HttpSession session,
@ApiResponse(responseCode = "404", description = "회원 세션이 존재하지 않음", content = @Content(schema = @Schema(implementation = VehicleResponse.class)))
})
@PutMapping("/update")
public ResponseEntity<VehicleResponse> updateVehicle(HttpSession session,
public ResponseEntity<VehicleResponse> updateVehicle(@RequestParam String memberId,
@RequestBody VehicleRequest request) {
return ResponseEntity.ok(vehicleService.updateVehicle(session, request));
return ResponseEntity.ok(vehicleService.updateVehicle(memberId, request));
}

@Operation(summary = "이동 수단 삭제", description = "현재 로그인한 회원의 이동 수단을 삭제합니다.")
Expand All @@ -75,9 +74,9 @@ public ResponseEntity<VehicleResponse> updateVehicle(HttpSession session,
@ApiResponse(responseCode = "404", description = "회원 세션이 존재하지 않음")
})
@DeleteMapping("/delete")
public ResponseEntity<Void> deleteVehicle(HttpSession session,
public ResponseEntity<Void> deleteVehicle(@RequestParam String memberId,
@RequestParam Long vehicleId) {
vehicleService.deleteVehicle(session, vehicleId);
vehicleService.deleteVehicle(memberId, vehicleId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import univ.goormthon.kongju.domain.member.entity.Member;
import univ.goormthon.kongju.domain.member.repository.MemberRepository;
import univ.goormthon.kongju.domain.vehicle.dto.request.VehicleRequest;
import univ.goormthon.kongju.domain.vehicle.dto.response.VehicleResponse;
import univ.goormthon.kongju.domain.vehicle.entity.Vehicle;
Expand All @@ -21,10 +22,12 @@
public class VehicleService {

private final VehicleRepository vehicleRepository;
private final MemberRepository memberRepository;

@Transactional(readOnly = true)
public List<VehicleResponse> getVehicles(HttpSession session) {
Member member = (Member) session.getAttribute("member");
public List<VehicleResponse> getVehicles(String memberId) {
Member member = memberRepository.findById(Long.parseLong(memberId))
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));

List<Vehicle> vehicles = vehicleRepository.findAllByMember(member);

Expand All @@ -38,11 +41,9 @@ public List<VehicleResponse> getVehicles(HttpSession session) {
}

@Transactional
public VehicleResponse addVehicle(HttpSession session, VehicleRequest request) {
Member member = (Member) session.getAttribute("member");
if(member == null) {
throw new NotFoundException(ErrorCode.MEMBER_NOT_FOUND);
}
public VehicleResponse addVehicle(String memberId, VehicleRequest request) {
Member member = memberRepository.findById(Long.parseLong(memberId))
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));

Vehicle vehicle = Vehicle.builder()
.member(member)
Expand All @@ -60,11 +61,9 @@ public VehicleResponse addVehicle(HttpSession session, VehicleRequest request) {
}

@Transactional
public VehicleResponse updateVehicle(HttpSession session, VehicleRequest request) {
Member member = (Member) session.getAttribute("member");
if(member == null) {
throw new NotFoundException(ErrorCode.MEMBER_NOT_FOUND);
}
public VehicleResponse updateVehicle(String memberId, VehicleRequest request) {
Member member = memberRepository.findById(Long.parseLong(memberId))
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));

Vehicle vehicle = vehicleRepository.findByMember(member)
.orElseThrow(() -> new NotFoundException(ErrorCode.VEHICLE_NOT_FOUND));
Expand All @@ -79,11 +78,9 @@ public VehicleResponse updateVehicle(HttpSession session, VehicleRequest request
}

@Transactional
public void deleteVehicle(HttpSession session, Long vehicleId) {
Member member = (Member) session.getAttribute("member");
if(member == null) {
throw new NotFoundException(ErrorCode.MEMBER_NOT_FOUND);
}
public void deleteVehicle(String memberId, Long vehicleId) {
Member member = memberRepository.findById(Long.parseLong(memberId))
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));

Vehicle vehicle = vehicleRepository.findById(vehicleId)
.orElseThrow(() -> new NotFoundException(ErrorCode.VEHICLE_NOT_FOUND));
Expand Down

0 comments on commit b7b636a

Please sign in to comment.