-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from CSID-DGU/backend/feature/crew
BE: [feat] 크루 채팅 보내기 및 조회 기능 #83
- Loading branch information
Showing
10 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...backend/src/main/java/RunningMachines/R2R/domain/crew/chat/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package RunningMachines.R2R.domain.crew.chat.controller; | ||
|
||
import RunningMachines.R2R.domain.crew.chat.dto.ChatMessageCreateRequestDto; | ||
import RunningMachines.R2R.domain.crew.chat.dto.ChatMessageResponseDto; | ||
import RunningMachines.R2R.domain.crew.chat.entity.CrewChatImage; | ||
import RunningMachines.R2R.domain.crew.chat.entity.CrewChatMessage; | ||
import RunningMachines.R2R.domain.crew.chat.service.ChatCommandService; | ||
import RunningMachines.R2R.domain.crew.chat.service.ChatQueryService; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import RunningMachines.R2R.domain.user.service.AuthCommandService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/crew/{crewId}/chat") | ||
@RequiredArgsConstructor | ||
public class ChatController { | ||
private final ChatCommandService chatCommandService; | ||
private final ChatQueryService chatQueryService; | ||
private final AuthCommandService authCommandService; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<ChatMessageResponseDto>> getChatMessages(@PathVariable Long crewId) { | ||
return ResponseEntity.ok(chatQueryService.getChatMessages(crewId)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<ChatMessageResponseDto> createChatMessage(@PathVariable Long crewId, @RequestBody ChatMessageCreateRequestDto requestDto) { | ||
User currentUser = authCommandService.getCurrentUser(); | ||
ChatMessageResponseDto responseDto = chatCommandService.createChatMessage(crewId, currentUser, requestDto); | ||
return ResponseEntity.ok(responseDto); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...d/src/main/java/RunningMachines/R2R/domain/crew/chat/dto/ChatMessageCreateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package RunningMachines.R2R.domain.crew.chat.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class ChatMessageCreateRequestDto { | ||
private Long crewId; | ||
private String content; | ||
private List<MultipartFile> images; | ||
|
||
public static ChatMessageCreateRequestDto of(Long crewId, String content, List<MultipartFile> images) { | ||
return ChatMessageCreateRequestDto.builder() | ||
.crewId(crewId) | ||
.content(content) | ||
.images(images) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ackend/src/main/java/RunningMachines/R2R/domain/crew/chat/dto/ChatMessageResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package RunningMachines.R2R.domain.crew.chat.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class ChatMessageResponseDto { | ||
private Long crewId; | ||
private String senderNickname; | ||
private String content; | ||
private List<String> imageUrls; | ||
private String createdAt; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/backend/src/main/java/RunningMachines/R2R/domain/crew/chat/entity/CrewChatImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package RunningMachines.R2R.domain.crew.chat.entity; | ||
|
||
import RunningMachines.R2R.global.util.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class CrewChatImage extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String imageUrl; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "chat_message_id") | ||
private CrewChatMessage chatMessage; | ||
|
||
public void setChatMessage(CrewChatMessage chatMessage) { | ||
this.chatMessage = chatMessage; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/backend/src/main/java/RunningMachines/R2R/domain/crew/chat/entity/CrewChatMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package RunningMachines.R2R.domain.crew.chat.entity; | ||
|
||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import RunningMachines.R2R.global.util.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CrewChatMessage extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "crew_id", nullable = false) | ||
private Crew crew; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User sender; | ||
|
||
private String content; | ||
|
||
@OneToMany(mappedBy = "chatMessage", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<CrewChatImage> chatImages = new ArrayList<>(); | ||
|
||
public void addChatImage(CrewChatImage crewChatImage) { | ||
this.chatImages.add(crewChatImage); | ||
crewChatImage.setChatMessage(this); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../src/main/java/RunningMachines/R2R/domain/crew/chat/repository/ChatMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package RunningMachines.R2R.domain.crew.chat.repository; | ||
|
||
import RunningMachines.R2R.domain.crew.chat.entity.CrewChatMessage; | ||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ChatMessageRepository extends JpaRepository<CrewChatMessage, Long> { | ||
List<CrewChatMessage> findByCrewOrderByCreatedAtAsc(Crew crew); | ||
} |
63 changes: 63 additions & 0 deletions
63
...ackend/src/main/java/RunningMachines/R2R/domain/crew/chat/service/ChatCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package RunningMachines.R2R.domain.crew.chat.service; | ||
|
||
import RunningMachines.R2R.domain.crew.chat.dto.ChatMessageCreateRequestDto; | ||
import RunningMachines.R2R.domain.crew.chat.dto.ChatMessageResponseDto; | ||
import RunningMachines.R2R.domain.crew.chat.entity.CrewChatImage; | ||
import RunningMachines.R2R.domain.crew.chat.entity.CrewChatMessage; | ||
import RunningMachines.R2R.domain.crew.chat.repository.ChatMessageRepository; | ||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import RunningMachines.R2R.global.s3.S3Provider; | ||
import RunningMachines.R2R.global.s3.S3RequestDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatCommandService { | ||
private final ChatMessageRepository chatMessageRepository; | ||
private final CrewRepository crewRepository; | ||
private final S3Provider s3Provider; | ||
|
||
public ChatMessageResponseDto createChatMessage(Long crewId, User sender, ChatMessageCreateRequestDto requestDto) { | ||
Crew crew = crewRepository.findById(crewId) | ||
.orElseThrow(() -> new IllegalArgumentException("크루를 찾을 수 없습니다.")); | ||
|
||
CrewChatMessage chatMessage = CrewChatMessage.builder() | ||
.crew(crew) | ||
.sender(sender) | ||
.content(requestDto.getContent()) | ||
.build(); | ||
|
||
List<MultipartFile> images = requestDto.getImages(); | ||
if (images!=null) { | ||
for (MultipartFile image : images) { | ||
String imageUrl = s3Provider.uploadFile(image, S3RequestDto.builder() | ||
.userId(sender.getId()) | ||
.dirName("chat-images") | ||
.build()); | ||
CrewChatImage chatImage = CrewChatImage.builder() | ||
.imageUrl(imageUrl) | ||
.build(); | ||
chatMessage.addChatImage(chatImage); | ||
} | ||
} | ||
chatMessageRepository.save(chatMessage); | ||
|
||
return ChatMessageResponseDto.builder() | ||
.crewId(crew.getId()) | ||
.senderNickname(sender.getNickname()) | ||
.content(chatMessage.getContent()) | ||
.imageUrls(chatMessage.getChatImages() == null ? List.of() :chatMessage.getChatImages().stream() | ||
.map(CrewChatImage::getImageUrl) | ||
.collect(Collectors.toList())) | ||
.createdAt(chatMessage.getCreatedAt().format(DateTimeFormatter.ofPattern("MM-dd HH:mm"))) | ||
.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/backend/src/main/java/RunningMachines/R2R/domain/crew/chat/service/ChatQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package RunningMachines.R2R.domain.crew.chat.service; | ||
|
||
import RunningMachines.R2R.domain.crew.chat.dto.ChatMessageResponseDto; | ||
import RunningMachines.R2R.domain.crew.chat.repository.ChatMessageRepository; | ||
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository; | ||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatQueryService { | ||
private final ChatMessageRepository chatMessageRepository; | ||
private final CrewRepository crewRepository; | ||
|
||
public List<ChatMessageResponseDto> getChatMessages(Long crewId) { | ||
Crew crew = crewRepository.findById(crewId) | ||
.orElseThrow(() -> new IllegalArgumentException("크루를 찾을 수 없습니다.")); | ||
|
||
return chatMessageRepository.findByCrewOrderByCreatedAtAsc(crew) | ||
.stream() | ||
.map(crewChatMessage -> ChatMessageResponseDto.builder() | ||
.crewId(crew.getId()) | ||
.senderNickname(crewChatMessage.getSender().getNickname()) | ||
.content(crewChatMessage.getContent()) | ||
.imageUrls(crewChatMessage.getChatImages().stream() | ||
.map(crewChatImage -> crewChatImage.getImageUrl()) | ||
.collect(Collectors.toList())) | ||
.createdAt(crewChatMessage.getCreatedAt().format(DateTimeFormatter.ofPattern("MM-dd HH:mm"))) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters