Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/UMC-ON/ON-server into feat/…
Browse files Browse the repository at this point in the history
…alert
  • Loading branch information
ri-naa committed Jan 7, 2025
2 parents fd844b2 + 3a3df69 commit 84bfdf3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public Page<CompanyChatRoomListDto> getCompanyChatRoomList(User user, Pageable p
lastChatTime
);
})
.sorted(Comparator.comparing(CompanyRoomDto::getLastChatTime).reversed()) // 최신순 정렬
.toList();


Expand Down Expand Up @@ -129,7 +128,6 @@ public Page<MarketChatRoomListDto> getMarketChatRoomList(User user, Pageable pag
lastChatTime
);
})
.sorted(Comparator.comparing(MarketRoomDto::getLastChatTime).reversed()) // 최신순 정렬
.toList();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

public interface ChattingRoomRepository extends JpaRepository<ChattingRoom, Long> {

@Query("SELECT cr FROM ChattingRoom cr WHERE (cr.chatUserOne = :user OR cr.chatUserTwo = :user) AND cr.chattingRoomType = :chattingRoomType")
@Query("SELECT cr FROM ChattingRoom cr " +
"LEFT JOIN Chat c ON c.chattingRoom = cr " +
"WHERE (cr.chatUserOne = :user OR cr.chatUserTwo = :user) " +
"AND cr.chattingRoomType = :chattingRoomType " +
"GROUP BY cr " +
"ORDER BY COALESCE(MAX(c.createdAt), cr.createdAt) DESC")
Page<ChattingRoom> findByChatUserOneOrChatUserTwoAndChattingRoomType(
@Param("user") User user,
@Param("chattingRoomType") ChatType chattingRoomType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.on.server.domain.alarm.application.AlertService;
import com.on.server.domain.alarm.application.FcmService;
import com.on.server.domain.alarm.domain.AlertType;
import com.on.server.domain.alarm.dto.FcmRequestDto;
import com.on.server.domain.chat.domain.ChattingRoom;
import com.on.server.domain.chat.domain.SpecialChat;
import com.on.server.domain.chat.domain.repository.ChattingRoomRepository;
import com.on.server.domain.chat.domain.repository.SpecialChatRepository;
import com.on.server.domain.companyParticipant.domain.repository.CompanyParticipantRepository;
import com.on.server.domain.companyParticipant.dto.CompanyParticipantRequestDTO;
import com.on.server.domain.companyParticipant.dto.CompanyParticipantResponseDTO;
Expand All @@ -12,21 +15,26 @@
import com.on.server.domain.companyPost.domain.CompanyPost;
import com.on.server.domain.companyPost.domain.repository.CompanyPostRepository;
import com.on.server.domain.user.domain.User;
import com.on.server.domain.user.domain.repository.UserRepository;
import com.on.server.global.common.ResponseCode;
import com.on.server.global.common.exceptions.BadRequestException;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
@RequiredArgsConstructor
@Transactional
public class CompanyParticipantService {

private final CompanyParticipantRepository companyParticipantRepository;
private final CompanyPostRepository companyPostRepository;
private final UserRepository userRepository;
private final FcmService fcmService;
private final AlertService alertService;
private final ChattingRoomRepository chattingRoomRepository;
private final SpecialChatRepository specialChatRepository;

public CompanyParticipantResponseDTO applyToCompanyPost(User user, CompanyParticipantRequestDTO requestDTO) {

Expand Down Expand Up @@ -57,4 +65,33 @@ public CompanyParticipantResponseDTO applyToCompanyPost(User user, CompanyPartic
.companyParticipantStatus(companyParticipant.getCompanyParticipantstatus())
.build();
}


// 특정 채팅방의 동행 신청자의 동행 신청 상태 확인
@Transactional(readOnly = true)
public CompanyParticipantResponseDTO getCompanyParticipantStatus(Long userId, Long chattingRoomId) {

// 채팅방이 존재하는지 확인
ChattingRoom chattingRoom = chattingRoomRepository.findById(chattingRoomId)
.orElseThrow(() -> new BadRequestException(ResponseCode.ROW_DOES_NOT_EXIST, "채팅방을 찾을 수 없습니다."));

// userId로 User 조회
userRepository.findById(userId)
.orElseThrow(() -> new BadRequestException(ResponseCode.ROW_DOES_NOT_EXIST, "사용자를 찾을 수 없습니다."));

// SpecialChat 조회
SpecialChat specialChat = specialChatRepository.findByChattingRoom(chattingRoom);

// SpecialChat에서 CompanyPost 정보 가져오기
CompanyPost companyPost = specialChat.getCompanyPost();

// CompanyParticipant 조회 (status 확인용)
CompanyParticipant companyParticipant = companyParticipantRepository.findByUser_IdAndCompanyPost_Id(userId, companyPost.getId());

if (companyParticipant == null) { // 없으면 null 반환
return null;
}

return CompanyParticipantResponseDTO.from(companyParticipant, chattingRoomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CompanyParticipantRepository extends JpaRepository<CompanyParticipant, Long> {

CompanyParticipant findByUserAndCompanyPostId(User userParticipant, Long companyPostId);

CompanyParticipant findByUser_IdAndCompanyPost_Id(Long userId, Long companyPostId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.on.server.domain.companyParticipant.dto;
import com.on.server.domain.companyParticipant.domain.CompanyParticipant;
import com.on.server.domain.companyParticipant.domain.CompanyParticipantStatus;
import com.on.server.domain.companyPost.domain.CompanyPost;
import com.on.server.domain.companyPost.dto.CompanyPostResponseDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -17,9 +20,22 @@ public class CompanyParticipantResponseDTO {
// 동행 구하기 글 ID
private Long companyPostId;

// 채팅룸 번호
private Long chattingRoomId;

// 작성자 ID
private Long userId;

// 상태
private CompanyParticipantStatus companyParticipantStatus;

public static CompanyParticipantResponseDTO from(CompanyParticipant companyParticipant, Long chattingRoomId) {
return CompanyParticipantResponseDTO.builder()
.companyParticipantId(companyParticipant.getId())
.companyPostId(companyParticipant.getCompanyPost().getId())
.chattingRoomId(chattingRoomId)
.userId(companyParticipant.getUser().getId())
.companyParticipantStatus(companyParticipant.getCompanyParticipantstatus())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "동행 신청하기")
@RestController
Expand All @@ -39,4 +38,14 @@ public ResponseEntity<CompanyParticipantResponseDTO> applyToCompanyPost (

return ResponseEntity.ok(responseDTO);
}

@Operation(summary = "특정 채팅방의 동행 신청자의 동행 신청 상태 확인")
@PreAuthorize("@securityService.isNotTemporaryUser()")
@GetMapping("/status/{userId}/{chattingRoomId}")
public ResponseEntity<CompanyParticipantResponseDTO> getCompanyParticipantStatus(@PathVariable Long userId,
@PathVariable Long chattingRoomId) {

CompanyParticipantResponseDTO status = companyParticipantService.getCompanyParticipantStatus(userId, chattingRoomId);
return ResponseEntity.ok(status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.on.server.global.common.exceptions.UnauthorizedException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -36,18 +35,7 @@ public class CompanyPostService {

// 필터링 기능 추가
public Page<CompanyPostResponseDTO> getFilteredCompanyPosts(LocalDate startDate, LocalDate endDate, Gender gender, String country, Pageable pageable) {
Page<CompanyPost> posts = companyPostRepository.findFilteredCompanyPostsWithoutCountry(startDate, endDate, gender, pageable);

if (country != null && !country.isEmpty()) {
posts = posts.stream()
.filter(post -> post.getTravelArea().stream()
.anyMatch(area -> {
String firstWord = area.split(" ")[0]; // travelArea의 첫 번째 단어 추출
return firstWord.equalsIgnoreCase(country);
}))
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> new PageImpl<>(list, pageable, list.size())));
}

Page<CompanyPost> posts = companyPostRepository.findFilteredCompanyPosts(startDate, endDate, gender, country, pageable);
return posts.map(CompanyPostResponseDTO::from);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
public interface CompanyPostRepository extends JpaRepository<CompanyPost, Long> {

// 필터링을 위한 쿼리
@Query("SELECT cp FROM CompanyPost cp WHERE (:startDate IS NULL OR cp.startDate >= :startDate) " +
@Query("SELECT DISTINCT cp FROM CompanyPost cp JOIN cp.travelArea ta WHERE " +
"(:startDate IS NULL OR cp.startDate >= :startDate) " +
"AND (:endDate IS NULL OR cp.endDate <= :endDate) " +
"AND (:gender IS NULL OR cp.user.gender = :gender) " +
"AND (:country IS NULL OR SUBSTRING(ta, 1, LOCATE(' ', ta) - 1) = :country) " +
"ORDER BY cp.createdAt DESC")
Page<CompanyPost> findFilteredCompanyPostsWithoutCountry(
Page<CompanyPost> findFilteredCompanyPosts(
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate,
@Param("gender") Gender gender,
@Param("country") String country,
Pageable pageable);

// 최신 4개의 글을 최신순으로 가져오기
Expand Down

0 comments on commit 84bfdf3

Please sign in to comment.