Skip to content

Commit

Permalink
Merge pull request #60 from Unithon11th-Team5/feat/#57
Browse files Browse the repository at this point in the history
feat: add relationship between message and event
  • Loading branch information
chominho96 authored Apr 6, 2024
2 parents 47251b0 + 8502dbd commit 870f0b1
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
import unithon.team5.event.dto.*;
import unithon.team5.event.service.EventService;
import unithon.team5.member.Member;
import unithon.team5.message.dto.MessageListResponse;

import java.net.URI;
import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
@RestController
@RequestMapping("/events")
public class EventController implements EventControllerDocs {

private final EventService eventService;

@PostMapping("/events")
@PostMapping
public ResponseEntity<Void> addEvent(@RequestBody @Valid final EventAddRequest eventAddRequest, final Member member) {
final String uuid = eventService.addEvent(member, eventAddRequest.plannedAt(), eventAddRequest.content(), eventAddRequest.type());
return ResponseEntity.created(URI.create("/events/" + uuid)).build();
}


@GetMapping("/events")
public ResponseEntity<EventListResponse> readAllEvent(@RequestParam final String memberId) {
final var events = eventService.findMemberEventAfterToday(memberId);
Expand All @@ -38,4 +42,9 @@ public ResponseEntity<TypeListResponse> readAllTypes() {

return ResponseEntity.ok(new TypeListResponse(responses));
}

@GetMapping("/messages")
public ResponseEntity<MessageListResponse> getMessages(@RequestParam UUID eventId, final Member member) {
return ResponseEntity.ok(new MessageListResponse(eventService.getMessagesFromEvent(eventId, member)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import unithon.team5.event.dto.EventListResponse;
import unithon.team5.event.dto.TypeListResponse;
import unithon.team5.member.Member;
import unithon.team5.message.dto.MessageListResponse;

import java.util.UUID;


public interface EventControllerDocs {

Expand Down Expand Up @@ -57,4 +61,22 @@ ResponseEntity<EventListResponse> readAllEvent(
})
@Operation(summary = "이벤트 타입 조회")
ResponseEntity<TypeListResponse> readAllTypes();

@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "SUCCESS"),
@ApiResponse(responseCode = "400", description = "BAD REQUEST",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}),
@ApiResponse(responseCode = "403", description = "FORBIDDEN",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}),
@ApiResponse(responseCode = "404", description = "NOT FOUND",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))}),
@ApiResponse(responseCode = "500", description = "SERVER ERROR",
content = {@Content(schema = @Schema(implementation = ErrorResponse.class))})
})
@Operation(summary = "이벤트 관련 메시지 조회")
ResponseEntity<MessageListResponse> getMessages(
@Parameter(description = "event id", example = "ete-dfdfd-fdfder", required = true)
@RequestParam UUID eventId,
@Parameter(hidden = true)
final Member member);
}
19 changes: 19 additions & 0 deletions src/main/java/unithon/team5/event/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import unithon.team5.common.error.ForbiddenException;
import unithon.team5.common.error.NotFoundException;
import unithon.team5.event.Event;
import unithon.team5.event.EventType;
import unithon.team5.event.repository.EventRepository;
import unithon.team5.member.Member;
import unithon.team5.message.dto.MessageResponse;
import unithon.team5.message.repository.MessageRepository;

import java.time.LocalDate;
import java.util.Arrays;
Expand All @@ -18,6 +22,7 @@
public class EventService {

private final EventRepository eventRepository;
private final MessageRepository messageRepository;

@Transactional
public String addEvent(final Member member, final LocalDate plannedAt, final String content, final EventType type) {
Expand All @@ -41,4 +46,18 @@ public List<EventType> findEventTypeAll() {
return Arrays.stream(EventType.values())
.toList();
}

public List<MessageResponse> getMessagesFromEvent(final UUID eventId, Member member) {

Event findEvent = eventRepository.findById(eventId).orElseThrow(
() -> new NotFoundException(String.format("[%s] event id not found\", message.getEventId()", eventId)));

if (!findEvent.getMemberId().equals(member.getId())) {
throw new ForbiddenException(String.format("[%s] event's member is not equal with current member [%s]", eventId, member.getId()));
}

return messageRepository.findAllByEventIdOrderBySendPlannedAtDesc(eventId)
.stream().map(message -> MessageResponse.of(message, findEvent))
.toList();
}
}
7 changes: 6 additions & 1 deletion src/main/java/unithon/team5/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ protected Message() {

private Message(final UUID senderId,
final UUID receiverId,
final UUID eventId,
final String senderName,
final String content,
final EventType type,
final LocalDate sendPlannedAt) {
super(null);
this.senderId = senderId;
this.receiverId = receiverId;
this.eventId = eventId;
this.senderName = senderName;
this.content = content;
this.type = type;
Expand All @@ -41,6 +43,8 @@ private Message(final UUID senderId,
@Column(nullable = false, updatable = false)
private UUID receiverId;

private UUID eventId;

@Column(nullable = false)
private String senderName;

Expand All @@ -60,10 +64,11 @@ public void read() {

public static Message create(final UUID senderId,
final UUID receiverId,
final UUID eventId,
final String senderName,
final String content,
final EventType type,
final LocalDate sendPlannedAt) {
return new Message(senderId, receiverId, senderName, content, type, sendPlannedAt);
return new Message(senderId, receiverId, eventId, senderName, content, type, sendPlannedAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface MessageControllerDocs {
{
\t"receiverNickname": "nick123",
\t"senderName": "보내는 닉네임",
\t"eventId": "(있는 경우만) 대상 event id",
\t"content": "보낼 메시지",
\t"type": "메시지 타입",
\t"sendPlannedAt": "2024-04-28"
Expand Down Expand Up @@ -80,13 +81,15 @@ ResponseEntity<UnreadMessageResponse> getUnreadMessages(
\t\t{
\t\t\t"id": "메시지 고유 UUID",
\t\t\t"senderName": "보낸 사람이 설정한 닉네임",
\t\t\t"eventId": "(있는 경우만) 대상 event id",
\t\t\t"content": "메시지 내용",
\t\t\t"sentAt": "2024-04-06",
\t\t\t"type": "메시지 타입"
\t\t},
\t\t{
\t\t\t"id": "메시지 고유 UUID",
\t\t\t"senderName": "보낸 사람이 설정한 닉네임",
\t\t\t"eventId": "(있는 경우만) 대상 event id",
\t\t\t"content": "메시지 내용",
\t\t\t"sentAt": "2024-04-06",
\t\t\t"type": "메시지 타입"
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/unithon/team5/message/dto/MessageEventResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package unithon.team5.message.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import unithon.team5.event.Event;
import unithon.team5.event.EventType;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class MessageEventResponse {

private UUID id;
private EventType type;
private String content;
private LocalDate plannedAt;

public static MessageEventResponse of(Event event) {
return new MessageEventResponse(
event.getId(),
event.getType(),
event.getContent(),
event.getPlannedAt()
);
}
}
3 changes: 3 additions & 0 deletions src/main/java/unithon/team5/message/dto/MessageRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import unithon.team5.event.EventType;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@AllArgsConstructor
Expand All @@ -22,6 +23,8 @@ public class MessageRequest {
@NotEmpty
private String senderName;

private UUID eventId;

@NotEmpty
@Length(max = 300)
private String content;
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/unithon/team5/message/dto/MessageResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import unithon.team5.event.Event;
import unithon.team5.event.EventType;
import unithon.team5.message.Message;

Expand All @@ -16,14 +17,26 @@ public class MessageResponse {

private UUID id;
private String senderName;
private MessageEventResponse event;
private String content;
private LocalDate sentAt;
private EventType type;

public static MessageResponse of(final Message message) {
public static MessageResponse of(final Message message, final Event event) {
if (event == null) {
return new MessageResponse(
message.getId(),
message.getSenderName(),
null,
message.getContent(),
message.getSendPlannedAt(),
message.getType()
);
}
return new MessageResponse(
message.getId(),
message.getSenderName(),
MessageEventResponse.of(event),
message.getContent(),
message.getSendPlannedAt(),
message.getType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
public interface MessageRepository extends JpaRepository<Message, UUID> {
List<Message> findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(UUID receiverId, Boolean isRead);
List<Message> findByReceiverIdOrderBySendPlannedAtDesc(UUID receiverId);
List<Message> findAllByEventIdOrderBySendPlannedAtDesc(UUID eventId);
}
34 changes: 24 additions & 10 deletions src/main/java/unithon/team5/message/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import unithon.team5.common.error.ForbiddenException;
import unithon.team5.common.error.NotFoundException;
import unithon.team5.event.repository.EventRepository;
import unithon.team5.member.Member;
import unithon.team5.member.repository.MemberRepository;
import unithon.team5.message.Message;
Expand All @@ -25,6 +26,7 @@ public class MessageService {

private final MessageRepository messageRepository;
private final MemberRepository memberRepository;
private final EventRepository eventRepository;

// TODO: 추후 알림 기능 구현하면 추가 로직 구현 필요
public void sendMessage(final MessageRequest request, final Member loginMember) {
Expand All @@ -35,6 +37,7 @@ public void sendMessage(final MessageRequest request, final Member loginMember)
messageRepository.save(Message.create(
loginMember.getId(),
receiver.getId(),
request.getEventId(),
request.getSenderName(),
request.getContent(),
request.getType(),
Expand All @@ -43,19 +46,12 @@ public void sendMessage(final MessageRequest request, final Member loginMember)

public List<MessageResponse> getUnreadMessages(final Member member) {

return messageRepository.findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(member.getId(), false)
.stream().map(
message -> {
message.read();
return MessageResponse.of(message);
}
).toList();
return getMessageWithEvent(messageRepository.findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(member.getId(), false));
}

public List<MessageResponse> getAllMessages(final Member member) {

return messageRepository.findByReceiverIdOrderBySendPlannedAtDesc(member.getId())
.stream().map(MessageResponse::of).toList();
return getMessageWithEvent(messageRepository.findByReceiverIdOrderBySendPlannedAtDesc(member.getId()));
}

public MessageResponse getMessage(final UUID id, final Member member) {
Expand All @@ -67,6 +63,24 @@ public MessageResponse getMessage(final UUID id, final Member member) {
throw new ForbiddenException(String.format("[%s] message id's receiver is not equal with current member [%s]", id, member.getId()));
}

return MessageResponse.of(findMessage);
return MessageResponse.of(
findMessage,
eventRepository.findById(findMessage.getEventId()).orElseThrow(
() -> new NotFoundException(String.format("[%s] event id not found", findMessage.getEventId()))));
}

private List<MessageResponse> getMessageWithEvent(List<Message> messages) {
return messages
.stream().map(
message -> {
if (message.getEventId() != null) {
return MessageResponse.of(
message,
eventRepository.findById(message.getEventId()).orElseThrow(
() -> new NotFoundException(String.format("[%s] event id not found", message.getEventId()))));
}
return MessageResponse.of(message, null);
}
).toList();
}
}

0 comments on commit 870f0b1

Please sign in to comment.