Skip to content

Commit

Permalink
#237 feat: 배달 채팅방 생성 API(createChatRoom) 구현, rabbitMQ 채팅방 exchange생성까…
Browse files Browse the repository at this point in the history
…지 연결
  • Loading branch information
xhaktmchl committed Nov 29, 2022
1 parent 4271b8a commit a772eb9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import shop.geeksasang.domain.chat.ChatRoom;
import shop.geeksasang.dto.chat.PostChattingReq;
import shop.geeksasang.dto.chat.chatmember.PostPartyChatRoomMemberReq;
import shop.geeksasang.dto.chat.partyChatRoom.PostPartyChatRoomReq;
import shop.geeksasang.dto.chat.partychatroom.PartyChatRoomRes;
import shop.geeksasang.dto.chat.partychatroom.post.PostPartyChatRoomReq;
import shop.geeksasang.dto.login.JwtInfo;
import shop.geeksasang.service.chat.DeliveryPartyChatService;
import shop.geeksasang.utils.jwt.NoIntercept;
Expand All @@ -27,17 +28,17 @@ public class DeliveryPartyChatController {

private final DeliveryPartyChatService deliveryPartyChattingService;

@ApiOperation(value = "채팅방 생성", notes = "(jwt 토큰 필요)마이페이지-공지사항에서 공지사항을 전체 조회")
@ApiOperation(value = "채팅방 생성", notes = "(jwt 토큰 필요)배달 채팅방 생성 요청")
@ApiResponses({
@ApiResponse(code = 1000 ,message ="요청에 성공하셨습니다."),
@ApiResponse(code = 2009, message ="존재하지 않는 멤버입니다"),
@ApiResponse(code = 4000 ,message ="서버 오류입니다.")
})
@PostMapping
public BaseResponse<Long> createPartyChatRoom(HttpServletRequest request, @RequestBody @Validated PostPartyChatRoomReq postPartyChatRoomReq){
public BaseResponse<PartyChatRoomRes> createPartyChatRoom(HttpServletRequest request, @RequestBody @Validated PostPartyChatRoomReq dto){
JwtInfo jwtInfo = (JwtInfo) request.getAttribute("jwtInfo");
String id = deliveryPartyChattingService.createChatRoom(jwtInfo.getUserId(), postPartyChatRoomReq.getTitle());
return new BaseResponse(id);
PartyChatRoomRes res = deliveryPartyChattingService.createChatRoom(jwtInfo.getUserId(), dto.getTitle(), dto.getAccountNumber(), dto.getBank(), dto.getCategory(), dto.getMaxMatching());
return new BaseResponse<>(res);
}

@PostMapping("/chatting")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package shop.geeksasang.dto.chat.partychatroom;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import shop.geeksasang.domain.chat.PartyChatRoom;

@NoArgsConstructor
@Getter
public class PartyChatRoomRes {

@ApiModelProperty(example = "637fa741bba4cf6c34bc13ef")
@ApiParam(value = "배달 파티 채팅방 제목")
private String partyChatRoomId;

@ApiModelProperty(example = "치킨 시키실 분 구합니다.")
@ApiParam(value = "배달 파티 채팅방 제목")
private String title;

/*
연관관계 편의 메서드
*/
@Builder
public PartyChatRoomRes(String partyChatRoomId, String title) {
this.partyChatRoomId = partyChatRoomId;
this.title = title;
}

public static PartyChatRoomRes toDto(PartyChatRoom partyChatRoom){
return PartyChatRoomRes.builder()
.partyChatRoomId(partyChatRoom.getId())
.title(partyChatRoom.getTitle())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package shop.geeksasang.dto.chat.partychatroom.post;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@NoArgsConstructor
@Getter
public class PostPartyChatRoomReq {

@ApiModelProperty(example = "치킨 시키실 분 구합니다.")
@ApiParam(value = "배달 파티 채팅방 제목", required = true)
@Size(min = 1, max = 20)
@NotBlank(message = "배달 파티 채팅방 제목을 입력하세요.")
private String title;

@ApiModelProperty(example = "111-22222-33333")
@ApiParam(value = "계좌번호", required = true)
@NotBlank(message = "계좌번호를 입력하세요.")
private String accountNumber;

@ApiModelProperty(example = "신한은행")
@ApiParam(value = "은행이름", required = true)
@NotBlank(message = "은행이름을 입력하세요.")
private String bank;

@ApiModelProperty(example = "Delivery")
@ApiParam(value = "채팅방 종류", required = true)
@NotBlank(message = "배달 파티 채팅방 종류를 입력하세요.")
private String category;

@ApiModelProperty(example = "4")
@ApiParam(value = "매칭 최대 인원", required = true)
private Integer maxMatching;
}
10 changes: 1 addition & 9 deletions src/main/java/shop/geeksasang/rabbitmq/MQController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ public String createChatRoom(@RequestParam String email, @RequestParam String Ch
String exchangeName = "chatting-" + "exchange-" + ChatRoomUUID;
Exchange exchange = new FanoutExchange(exchangeName);
admin.declareExchange(exchange);
System.out.println("rabbitMQ fanOutExchange가 생성되었습니다.");

Queue queue = new Queue(email, true, false, false);
QueueInformation queueInfo = admin.getQueueInfo(email); // 기존 큐 조회
// 큐가 없으면 생성
if(queueInfo == null){
admin.declareQueue(queue);
}

Binding binding = new Binding(email, Binding.DestinationType.QUEUE, exchangeName, "asdf",null); //TODO: fanoutExchange는 routingKey가 필요없지만 없으면 에러나서 임시로 입력 함.
admin.declareBinding(binding);
return "OK";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import shop.geeksasang.domain.chat.ChatRoom;
import shop.geeksasang.domain.chat.PartyChatRoomMember;
import shop.geeksasang.domain.chat.PartyChatRoom;
import shop.geeksasang.domain.member.Member;
import shop.geeksasang.dto.chat.PostChatRes;
import shop.geeksasang.dto.chat.partychatroom.PartyChatRoomRes;
import shop.geeksasang.repository.chat.PartyChatRoomMemberRepository;
import shop.geeksasang.repository.chat.PartyChatRoomRepository;
import shop.geeksasang.rabbitmq.MQController;
import shop.geeksasang.repository.chat.ChatRepository;
import shop.geeksasang.repository.chat.ChatRoomRepository;
import shop.geeksasang.repository.member.MemberRepository;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -33,14 +36,28 @@ public class DeliveryPartyChatService {
private final PartyChatRoomRepository partyChatRoomRepository;
private final MQController mqController;
private final PartyChatRoomMemberRepository partyChatRoomMemberRepository;
private final MemberRepository memberRepository;

@Transactional(readOnly = false)
public String createChatRoom(int memberId, String title){
public PartyChatRoomRes createChatRoom(int memberId, String title, String accountNumber, String bank, String category, Integer maxMatching){

Member member = memberRepository.findMemberById(memberId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_EXIST_USER));

List<Chat> chattings = new ArrayList<>();
List<PartyChatRoomMember> participants = new ArrayList<>();
PartyChatRoom ChatRoom = new PartyChatRoom(title, chattings, participants, "123", "국민", "Delivery", false, 5);
PartyChatRoom saveChatRoom = partyChatRoomRepository.save(ChatRoom);
return saveChatRoom.getId();
PartyChatRoom chatRoom = new PartyChatRoom(title, chattings, participants, accountNumber, bank, category, false, maxMatching);
partyChatRoomRepository.save(chatRoom);

//rabbitMQ 채팅방 생성 요청
try{
mqController.createChatRoom(member.getEmail().toString(), chatRoom.getId());
}catch (Exception e){
System.out.println("mqController에서 채팅방 생성 에러 발생");
}

PartyChatRoomRes res = PartyChatRoomRes.toDto(chatRoom);
return res;
}

@Transactional(readOnly = false)
Expand Down

0 comments on commit a772eb9

Please sign in to comment.