-
Notifications
You must be signed in to change notification settings - Fork 2
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 #76 from LIKELION-TEAM4-HACKATHON/feature/club-chat
모임 채팅 조회 및 작성하기 / 모임 정보 수정하기
- Loading branch information
Showing
11 changed files
with
401 additions
and
8 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/likelion/MZConnent/api/club/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,32 @@ | ||
package likelion.MZConnent.api.club; | ||
|
||
import likelion.MZConnent.dto.club.request.SendChatRequest; | ||
import likelion.MZConnent.dto.club.response.MyClubChatResponse; | ||
import likelion.MZConnent.dto.club.response.SendChatResponse; | ||
import likelion.MZConnent.jwt.principle.UserPrinciple; | ||
import likelion.MZConnent.service.club.ChatService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class ChatController { | ||
private final ChatService chatService; | ||
|
||
@GetMapping("/api/club/{clubId}/chats") | ||
public ResponseEntity<MyClubChatResponse> getClubChats(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId) { | ||
MyClubChatResponse response = chatService.getClubChats(userPrinciple.getEmail(), clubId); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@PostMapping("/api/club/{clubId}/chats") | ||
public ResponseEntity<SendChatResponse> sendChat(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId, @RequestBody SendChatRequest request) { | ||
SendChatResponse response = chatService.sendChat(userPrinciple.getEmail(), clubId, request); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/likelion/MZConnent/dto/club/request/SendChatRequest.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,10 @@ | ||
package likelion.MZConnent.dto.club.request; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class SendChatRequest { | ||
private String content; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/likelion/MZConnent/dto/club/request/UpdateClubInfoRequest.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,20 @@ | ||
package likelion.MZConnent.dto.club.request; | ||
|
||
import likelion.MZConnent.domain.club.AgeRestriction; | ||
import likelion.MZConnent.domain.club.GenderRestriction; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class UpdateClubInfoRequest { | ||
private String title; | ||
private LocalDate meetingDate; | ||
private String content; | ||
private GenderRestriction genderRestriction; | ||
private AgeRestriction ageRestriction; | ||
private Integer maxParticipant; | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/likelion/MZConnent/dto/club/response/MyClubChatResponse.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,58 @@ | ||
package likelion.MZConnent.dto.club.response; | ||
|
||
import likelion.MZConnent.domain.club.ClubRole; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class MyClubChatResponse { | ||
private Long clubId; | ||
private Long memberId; | ||
private String title; | ||
private LocalDate meetingDate; | ||
private String content; | ||
private String cultureName; | ||
private Long leaderId; | ||
private List<ChatDto> chats; | ||
|
||
@Builder | ||
public MyClubChatResponse(Long clubId, Long memberId, String title, LocalDate meetingDate, String content, String cultureName, Long leaderId, List<ChatDto> chats) { | ||
this.clubId = clubId; | ||
this.memberId = memberId; | ||
this.title = title; | ||
this.meetingDate = meetingDate; | ||
this.content = content; | ||
this.cultureName = cultureName; | ||
this.leaderId = leaderId; | ||
this.chats = chats; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class ChatDto { | ||
private Long chatId; | ||
private LocalDateTime createdDate; | ||
private String content; | ||
private Long memberId; | ||
private String userName; | ||
private String memberProfileUrl; | ||
private ClubRole role; | ||
|
||
@Builder | ||
public ChatDto(Long chatId, LocalDateTime createdDate, String content, Long memberId, String userName, String memberProfileUrl, ClubRole role) { | ||
this.chatId = chatId; | ||
this.createdDate = createdDate; | ||
this.content = content; | ||
this.memberId = memberId; | ||
this.userName = userName; | ||
this.memberProfileUrl = memberProfileUrl; | ||
this.role = role; | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/likelion/MZConnent/dto/club/response/SendChatResponse.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 likelion.MZConnent.dto.club.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class SendChatResponse { | ||
private Long chatId; | ||
private LocalDateTime createdDate; | ||
private String content; | ||
private Long memberId; | ||
private Long clubId; | ||
|
||
@Builder | ||
public SendChatResponse(Long chatId, LocalDateTime createdDate, String content, Long memberId, Long clubId) { | ||
this.chatId = chatId; | ||
this.createdDate = createdDate; | ||
this.content = content; | ||
this.memberId = memberId; | ||
this.clubId = clubId; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/likelion/MZConnent/dto/club/response/UpdateClubInfoResponse.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,34 @@ | ||
package likelion.MZConnent.dto.club.response; | ||
|
||
import likelion.MZConnent.domain.club.AgeRestriction; | ||
import likelion.MZConnent.domain.club.GenderRestriction; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class UpdateClubInfoResponse { | ||
private Long clubId; | ||
private String title; | ||
private LocalDate meetingDate; | ||
private String content; | ||
private GenderRestriction genderRestriction; | ||
private AgeRestriction ageRestriction; | ||
private int maxParticipant; | ||
private String cultureName; | ||
|
||
@Builder | ||
public UpdateClubInfoResponse(Long clubId, String title, LocalDate meetingDate, String content, GenderRestriction genderRestriction, AgeRestriction ageRestriction, int maxParticipant, String cultureName){ | ||
this.clubId = clubId; | ||
this.title = title; | ||
this.meetingDate = meetingDate; | ||
this.content = content; | ||
this.genderRestriction = genderRestriction; | ||
this.ageRestriction = ageRestriction; | ||
this.maxParticipant = maxParticipant; | ||
this.cultureName = cultureName; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/likelion/MZConnent/repository/chat/ChatRepository.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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package likelion.MZConnent.repository.chat; | ||
|
||
import likelion.MZConnent.domain.chat.Chat; | ||
import likelion.MZConnent.domain.club.Club; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ChatRepository extends JpaRepository<Chat, Long> { | ||
List<Chat> findByClubOrderByCreatedDateDesc(Club club); | ||
} |
121 changes: 121 additions & 0 deletions
121
src/main/java/likelion/MZConnent/service/club/ChatService.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,121 @@ | ||
package likelion.MZConnent.service.club; | ||
|
||
import jakarta.transaction.Transactional; | ||
import likelion.MZConnent.domain.chat.Chat; | ||
import likelion.MZConnent.domain.club.Club; | ||
import likelion.MZConnent.domain.club.ClubMember; | ||
import likelion.MZConnent.domain.club.ClubRole; | ||
import likelion.MZConnent.domain.member.Member; | ||
import likelion.MZConnent.dto.club.request.SendChatRequest; | ||
import likelion.MZConnent.dto.club.response.MyClubChatResponse; | ||
import likelion.MZConnent.dto.club.response.SendChatResponse; | ||
import likelion.MZConnent.repository.chat.ChatRepository; | ||
import likelion.MZConnent.repository.club.ClubRepository; | ||
import likelion.MZConnent.repository.member.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ChatService { | ||
private final ChatRepository chatRepository; | ||
private final ClubRepository clubRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
public MyClubChatResponse getClubChats(String email, Long clubId) { | ||
Member member = getMemberByEmail(email); | ||
Club club = getClubById(clubId); | ||
validateClubMember(club, member); | ||
|
||
List<Chat> chats = chatRepository.findByClubOrderByCreatedDateDesc(club); | ||
ClubMember clubLeader = getClubLeader(club); | ||
|
||
return MyClubChatResponse.builder() | ||
.clubId(clubId) | ||
.memberId(member.getId()) | ||
.title(club.getTitle()) | ||
.meetingDate(club.getMeetingDate()) | ||
.content(club.getContent()) | ||
.cultureName(club.getCulture().getName()) | ||
.leaderId(clubLeader.getMember().getId()) | ||
.chats(chats.stream().map(this::convertToChatDto).collect(Collectors.toList())) | ||
.build(); | ||
} | ||
|
||
public SendChatResponse sendChat(String email, Long clubId, SendChatRequest request) { | ||
Member member = getMemberByEmail(email); | ||
Club club = getClubById(clubId); | ||
validateClubMember(club, member); | ||
|
||
Chat chat = Chat.builder() | ||
.createdDate(LocalDateTime.now()) | ||
.content(request.getContent()) | ||
.club(club) | ||
.member(member) | ||
.build(); | ||
|
||
chatRepository.save(chat); | ||
|
||
return SendChatResponse.builder() | ||
.chatId(chat.getChatId()) | ||
.createdDate(chat.getCreatedDate()) | ||
.content(chat.getContent()) | ||
.memberId(chat.getMember().getId()) | ||
.clubId(chat.getClub().getClubId()) | ||
.build(); | ||
} | ||
|
||
private Member getMemberByEmail(String email) { | ||
return memberRepository.findByEmail(email) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자가 존재하지 않습니다.")); | ||
} | ||
|
||
private Club getClubById(Long clubId) { | ||
return clubRepository.findById(clubId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 동아리가 존재하지 않습니다.")); | ||
} | ||
|
||
private void validateClubMember(Club club, Member member) { | ||
boolean isMemberOfClub = club.getClubMembers().stream() | ||
.anyMatch(cm -> cm.getMember().getId().equals(member.getId())); | ||
if (!isMemberOfClub) { | ||
throw new IllegalArgumentException("해당 멤버는 해당 클럽의 멤버가 아닙니다."); | ||
} | ||
} | ||
|
||
private ClubMember getClubLeader(Club club) { | ||
return club.getClubMembers().stream() | ||
.filter(m -> m.getClubRole().equals(ClubRole.LEADER)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("동아리장이 존재하지 않습니다.")); | ||
} | ||
|
||
private MyClubChatResponse.ChatDto convertToChatDto(Chat chat) { | ||
ClubMember chatMember = getChatMember(chat); | ||
return MyClubChatResponse.ChatDto.builder() | ||
.chatId(chat.getChatId()) | ||
.createdDate(chat.getCreatedDate()) | ||
.content(chat.getContent()) | ||
.memberId(chat.getMember().getId()) | ||
.userName(chat.getMember().getUsername()) | ||
.memberProfileUrl(chat.getMember().getProfileImageUrl()) | ||
.role(chatMember.getClubRole()) | ||
.build(); | ||
} | ||
|
||
private ClubMember getChatMember(Chat chat) { | ||
ClubMember chatMember = chat.getClub().getClubMembers().stream() | ||
.filter(cm -> cm.getMember().getId().equals(chat.getMember().getId())) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
return chatMember; | ||
} | ||
} |
Oops, something went wrong.