Skip to content

Commit

Permalink
Merge pull request #76 from LIKELION-TEAM4-HACKATHON/feature/club-chat
Browse files Browse the repository at this point in the history
모임 채팅 조회 및 작성하기 / 모임 정보 수정하기
  • Loading branch information
chaeyoungeee authored Aug 2, 2024
2 parents 0f47ea4 + 51f4075 commit 2901551
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 8 deletions.
32 changes: 32 additions & 0 deletions src/main/java/likelion/MZConnent/api/club/ChatController.java
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);
}

}
12 changes: 8 additions & 4 deletions src/main/java/likelion/MZConnent/api/club/MyClubController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package likelion.MZConnent.api.club;

import likelion.MZConnent.dto.club.request.EvaluateMemberRequest;
import likelion.MZConnent.dto.club.response.EvaluateMemberResponse;
import likelion.MZConnent.dto.club.response.MemberRateResponse;
import likelion.MZConnent.dto.club.response.MyClubDetailResponse;
import likelion.MZConnent.dto.club.response.MyClubSimpleResponse;
import likelion.MZConnent.dto.club.request.UpdateClubInfoRequest;
import likelion.MZConnent.dto.club.response.*;
import likelion.MZConnent.jwt.principle.UserPrinciple;
import likelion.MZConnent.service.club.MyClubService;
import likelion.MZConnent.service.club.RateService;
Expand Down Expand Up @@ -65,4 +63,10 @@ public ResponseEntity leaveClub(@AuthenticationPrincipal UserPrinciple userPrinc
myClubService.leaveClub(userPrinciple.getEmail(), clubId);
return ResponseEntity.ok(Map.of("messege","모임 탈퇴 성공"));
}

@PutMapping("/api/clubs/{clubId}")
public ResponseEntity<UpdateClubInfoResponse> updateClubInfo(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId, @RequestBody UpdateClubInfoRequest request){
UpdateClubInfoResponse response = myClubService.updateClubInfo(userPrinciple.getEmail(), clubId, request);
return ResponseEntity.ok(response);
}
}
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;
}
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;

}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Getter
@NoArgsConstructor
public class MyClubDetailResponse {
private Long memberId;
private Long clubId;
private String title;
private LocalDate meetingDate;
Expand All @@ -23,7 +24,8 @@ public class MyClubDetailResponse {
private List<MyClubMemberDto> members;

@Builder
public MyClubDetailResponse(Long clubId, String title, LocalDate meetingDate, String content, int currentParticipant, MyClubCultureDto culture, List<MyClubMemberDto> members) {
public MyClubDetailResponse(Long memberId, Long clubId, String title, LocalDate meetingDate, String content, int currentParticipant, MyClubCultureDto culture, List<MyClubMemberDto> members) {
this.memberId = memberId;
this.clubId = clubId;
this.title = title;
this.meetingDate = meetingDate;
Expand Down
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;
}
}
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;
}
}
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 src/main/java/likelion/MZConnent/service/club/ChatService.java
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;
}
}
Loading

0 comments on commit 2901551

Please sign in to comment.