Skip to content

Commit

Permalink
Merge pull request #83 from ajy9851/feat/58/get-my-orders
Browse files Browse the repository at this point in the history
feat: ๊ฐ€๊ฒฉ ์ˆ˜์ • ๊ธฐ๋Šฅ
  • Loading branch information
rbm0524 authored Nov 13, 2024
2 parents 65d8923 + 015544d commit 14d9260
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${swagger_version}"
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
implementation 'ch.hsr:geohash:1.4.0'

implementation 'net.nurigo:sdk:4.3.0'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public ResponseEntity<ApiResponse<String>> signUpMember(
String serviceToken =
kakaoAuthService.register(
email, memberInfoRequest.deliveryName(), memberInfoRequest.phoneNumber());

ResponseCookie cookie =
ResponseCookie.from("serviceToken", serviceToken)
.httpOnly(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.ordertogether.team14_be.order.details.controller;

import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailRequestDto;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailResponseDto;
import com.ordertogether.team14_be.member.persistence.entity.Member;
import com.ordertogether.team14_be.member.presentation.LoginMember;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailReq;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailRes;
import com.ordertogether.team14_be.order.details.dto.get.GetCreatorOrderInfoRes;
import com.ordertogether.team14_be.order.details.dto.get.GetOrdersInfoReq;
import com.ordertogether.team14_be.order.details.dto.get.GetOrdersInfoRes;
import com.ordertogether.team14_be.order.details.dto.get.GetParticipantOrderInfoRes;
import com.ordertogether.team14_be.order.details.dto.update.UpdateOrderPriceReq;
import com.ordertogether.team14_be.order.details.service.OrderDetailService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -19,10 +31,36 @@ public class OrderDetailController {

// ์ฃผ๋ฌธ ์ƒ์„ฑ
@PostMapping
public ResponseEntity<CreateOrderDetailResponseDto> createOrderDetail(
@RequestBody CreateOrderDetailRequestDto createOrderDetailRequestDto) {
CreateOrderDetailResponseDto createOrderDetailResponseDto =
orderDetailService.createOrderDetail(createOrderDetailRequestDto);
return ResponseEntity.ok(createOrderDetailResponseDto);
public ResponseEntity<CreateOrderDetailRes> createOrderDetail(
@RequestBody CreateOrderDetailReq createOrderDetailReq) {
CreateOrderDetailRes createOrderDetailRes =
orderDetailService.createOrderDetail(createOrderDetailReq);
return ResponseEntity.ok(createOrderDetailRes);
}

@GetMapping
public ResponseEntity<GetOrdersInfoRes> getOrdersInfo(
@LoginMember Member member, @ModelAttribute @Valid GetOrdersInfoReq dto) {
return ResponseEntity.ok(orderDetailService.getOrdersInfo(member, dto));
}

@GetMapping("/participant")
public ResponseEntity<GetParticipantOrderInfoRes> getParticipantOrderInfo(
@LoginMember Member member, @RequestParam(name = "spot-id") Long spotId) {
return ResponseEntity.ok(orderDetailService.getParticipantOrderInfo(member, spotId));
}

@GetMapping("/creator")
public ResponseEntity<GetCreatorOrderInfoRes> getCreatorOrderInfo(
@LoginMember Member member, @RequestParam(name = "spot-id") Long spotId) {
return ResponseEntity.ok(orderDetailService.getCreatorOrderInfo(member, spotId));
}

// ๊ฐ€๊ฒฉ ์ˆ˜์ •
@PutMapping("/price")
public ResponseEntity<Void> updateOrderPrice(
@LoginMember Member member, @RequestBody @Valid UpdateOrderPriceReq dto) {
orderDetailService.updateOrderPrice(member, dto);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class CreateOrderDetailRequestDto {
public class CreateOrderDetailReq {
private int price;
private boolean isPayed;
private Long participantId; // ์ฐธ์—ฌ์ž ์•„์ด๋””
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class CreateOrderDetailResponseDto {
public class CreateOrderDetailRes {
private Long id;
private int price;
private boolean isPayed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ordertogether.team14_be.order.details.dto.get;

import java.util.List;

public record GetCreatorOrderInfoRes(
String category,
String storeName,
int minimumOrderAmount,
String pickUpLocation,
String deliveryStatus,
List<MemberBriefInfo> memberInfo) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ordertogether.team14_be.order.details.dto.get;

import java.util.List;

public record GetOrdersInfoReq(
Integer page, Integer size, List<String> sort // ์ฒซ ๋ฒˆ์งธ - ์ •๋ ฌํ•  ํ•„๋“œ๋ช…(DB ๊ธฐ์ค€), ๋‘ ๋ฒˆ์งธ - ์ •๋ ฌ ์ˆœ์„œ(desc, asc)
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ordertogether.team14_be.order.details.dto.get;

import java.util.List;

public record GetOrdersInfoRes(int totalPages, long totalElements, List<OrderInfo> ordersInfo) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ordertogether.team14_be.order.details.dto.get;

public record GetParticipantOrderInfoRes(
String category,
String storeName,
int minimumOrderAmount,
String pickUpLocation,
String deliveryStatus,
int price) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.ordertogether.team14_be.order.details.dto.get;

public record MemberBriefInfo(long memberId, String deliveryName, int price, boolean isPayed) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ordertogether.team14_be.order.details.dto.get;

import com.ordertogether.team14_be.order.details.entity.OrderDetail;
import com.ordertogether.team14_be.spot.entity.Spot;

public record OrderInfo(
Long id,
String category,
String storeName,
int minimumOrderAmount,
String pickUpLocation,
String deliveryStatus,
int price,
boolean isCreator) {

public OrderInfo(Long memberId, OrderDetail order, Spot spot) {
this(
order.getId(),
spot.getCategory().toString(),
spot.getStoreName(),
spot.getMinimumOrderAmount(),
spot.getPickUpLocation(),
spot.getDeliveryStatus(),
order.getPrice(),
spot.getMember().getId().equals(memberId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ordertogether.team14_be.order.details.dto.update;

public record UpdateOrderPriceReq(Long orderId, int price) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public class OrderDetail extends BaseTimeEntity {
private int price;

private boolean isPayed;

public void updatePrice(int price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.ordertogether.team14_be.order.details.repository;

import com.ordertogether.team14_be.member.persistence.entity.Member;
import com.ordertogether.team14_be.order.details.entity.OrderDetail;
import com.ordertogether.team14_be.spot.entity.Spot;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderDetailRepository extends JpaRepository<OrderDetail, Long> {}
public interface OrderDetailRepository extends JpaRepository<OrderDetail, Long> {
@EntityGraph(attributePaths = {"spot"})
Page<OrderDetail> findAllByMember(Member member, Pageable pageable);

Optional<OrderDetail> findBySpotAndMember(Spot spot, Member member);

List<OrderDetail> findAllBySpot(Spot spot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,160 @@

import com.ordertogether.team14_be.member.persistence.MemberRepository;
import com.ordertogether.team14_be.member.persistence.entity.Member;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailRequestDto;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailResponseDto;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailReq;
import com.ordertogether.team14_be.order.details.dto.create.CreateOrderDetailRes;
import com.ordertogether.team14_be.order.details.dto.get.GetCreatorOrderInfoRes;
import com.ordertogether.team14_be.order.details.dto.get.GetOrdersInfoReq;
import com.ordertogether.team14_be.order.details.dto.get.GetOrdersInfoRes;
import com.ordertogether.team14_be.order.details.dto.get.GetParticipantOrderInfoRes;
import com.ordertogether.team14_be.order.details.dto.get.MemberBriefInfo;
import com.ordertogether.team14_be.order.details.dto.get.OrderInfo;
import com.ordertogether.team14_be.order.details.dto.update.UpdateOrderPriceReq;
import com.ordertogether.team14_be.order.details.entity.OrderDetail;
import com.ordertogether.team14_be.order.details.repository.OrderDetailRepository;
import com.ordertogether.team14_be.spot.dto.servicedto.SpotDto;
import com.ordertogether.team14_be.spot.entity.Spot;
import com.ordertogether.team14_be.spot.repository.SimpleSpotRepository;
import com.ordertogether.team14_be.spot.repository.SpotRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class OrderDetailService {

private final SimpleSpotRepository simpleSpotRepository;
private final OrderDetailRepository orderDetailRepository;
private final MemberRepository memberRepository;
private final SpotRepository spotRepository;

// ์ฃผ๋ฌธ ์ƒ์„ธ ์ •๋ณด ์ƒ์„ฑ ๋ฉ”์„œ๋“œ
public CreateOrderDetailResponseDto createOrderDetail(
CreateOrderDetailRequestDto createOrderDetailRequestDto) {
@Transactional
public CreateOrderDetailRes createOrderDetail(CreateOrderDetailReq createOrderDetailReq) {

// ์ฐธ์—ฌ์ž ๋ณธ์ธ ์ •๋ณด ์„ค์ •
Member member =
memberRepository
.findById(createOrderDetailRequestDto.getParticipantId())
.findById(createOrderDetailReq.getParticipantId())
.orElseThrow(() -> new IllegalArgumentException("์ฐธ์—ฌ์ž ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."));

// ์ŠคํŒŸ ์ •๋ณด ์„ค์ •
SpotDto spot =
spotRepository.findByIdAndIsDeletedFalse(createOrderDetailRequestDto.getSpotId());
// ์ŠคํŒŸ ์ •๋ณด ์„ค์ • - spotMapper๊ฐ€ ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์•„์„œ Simple์‚ฌ์šฉ
// SpotDto spotDto =
// spotRepository.findByIdAndIsDeletedFalse(createOrderDetailRequestDto.getSpotId());
Spot spot =
simpleSpotRepository
.findById(createOrderDetailReq.getSpotId())
.orElseThrow(() -> new IllegalArgumentException("์ŠคํŒŸ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."));

OrderDetail orderDetail =
OrderDetail.builder()
.member(member)
// .spot(spot)
.price(createOrderDetailRequestDto.getPrice())
.isPayed(createOrderDetailRequestDto.isPayed())
.spot(spot)
.price(createOrderDetailReq.getPrice())
.isPayed(createOrderDetailReq.isPayed())
.build();

OrderDetail savedOrderDetail = orderDetailRepository.save(orderDetail);

return CreateOrderDetailResponseDto.builder()
return CreateOrderDetailRes.builder()
.id(savedOrderDetail.getId())
.price(savedOrderDetail.getPrice())
.isPayed(savedOrderDetail.isPayed())
.participantName(savedOrderDetail.getMember().getDeliveryName())
.spotName(spot.getStoreName())
.build();
}

@Transactional(readOnly = true)
public GetOrdersInfoRes getOrdersInfo(Member member, GetOrdersInfoReq dto) {
Page<OrderDetail> orderDetails =
orderDetailRepository.findAllByMember(
member,
PageRequest.of(
dto.page(),
dto.size(),
dto.sort() == null ? Sort.unsorted() : Sort.by(dto.sort().get(1))));

return new GetOrdersInfoRes(
orderDetails.getTotalPages(),
orderDetails.getTotalElements(),
orderDetails.getContent().stream()
.map(order -> new OrderInfo(member.getId(), order, order.getSpot()))
.toList());
}

@Transactional(readOnly = true)
public GetParticipantOrderInfoRes getParticipantOrderInfo(Member member, Long spotId) {
Spot spot =
simpleSpotRepository
.findById(spotId)
.orElseThrow(() -> new IllegalArgumentException("์ŠคํŒŸ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."));
Member creator = spot.getMember();

if (member.getId().equals(creator.getId()))
throw new IllegalArgumentException("๋ฐฉ์žฅ์ž…๋‹ˆ๋‹ค.(์ฐธ์—ฌ์ž๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)");

OrderDetail orderDetail =
orderDetailRepository
.findBySpotAndMember(spot, member)
.orElseThrow(() -> new IllegalArgumentException("์ฃผ๋ฌธ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."));

return new GetParticipantOrderInfoRes(
spot.getCategory().toString(),
spot.getStoreName(),
spot.getMinimumOrderAmount(),
spot.getPickUpLocation(),
spot.getDeliveryStatus(),
orderDetail.getPrice());
}

@Transactional(readOnly = true)
public GetCreatorOrderInfoRes getCreatorOrderInfo(Member member, Long spotId) {
Spot spot =
simpleSpotRepository
.findById(spotId)
.orElseThrow(() -> new IllegalArgumentException("์ŠคํŒŸ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."));
Member creator = spot.getMember();

if (!member.getId().equals(creator.getId()))
throw new IllegalArgumentException("์ฐธ์—ฌ์ž์ž…๋‹ˆ๋‹ค.(๋ฐฉ์žฅ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)");

List<OrderDetail> orders = orderDetailRepository.findAllBySpot(spot);

return new GetCreatorOrderInfoRes(
spot.getCategory().toString(),
spot.getStoreName(),
spot.getMinimumOrderAmount(),
spot.getPickUpLocation(),
spot.getDeliveryStatus(),
orders.stream()
.map(
order -> {
Member participant = order.getMember();
return new MemberBriefInfo(
participant.getId(),
participant.getDeliveryName(),
order.getPrice(),
order.isPayed());
})
.toList()); // memberInfo
}

@Transactional
public void updateOrderPrice(Member member, UpdateOrderPriceReq dto) {
OrderDetail orderDetail =
orderDetailRepository
.findById(dto.orderId())
.orElseThrow(() -> new IllegalArgumentException("์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));

if (!orderDetail.getMember().getId().equals(member.getId())) {
throw new IllegalArgumentException("์ฃผ๋ฌธ์˜ ์ฐธ์—ฌ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.");
}

orderDetail.updatePrice(dto.price());
orderDetailRepository.save(orderDetail);
}
}
Loading

0 comments on commit 14d9260

Please sign in to comment.