Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be/#251 : 쪽지 목록 조회 API 구현 #252

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.message.dto.request.CreateMessageRequest;
import com.graphy.backend.domain.message.dto.response.GetMessageDetailResponse;
import com.graphy.backend.domain.message.dto.response.GetMessageResponse;
import com.graphy.backend.domain.message.service.MessageService;
import com.graphy.backend.global.common.PageRequest;
import com.graphy.backend.global.result.ResultCode;
import com.graphy.backend.global.result.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "MessageController", description = "쪽지 관련 API")
@RestController
@RequestMapping("api/v1/messages")
Expand All @@ -37,4 +42,13 @@ public ResponseEntity<ResultResponse> messageDetails(@PathVariable Long messageI
GetMessageDetailResponse result = messageService.findMessageById(messageId);
return ResponseEntity.ok(ResultResponse.of(ResultCode.MESSAGE_GET_SUCCESS, result));
}

@Operation(summary = "findMessageList", description = "쪽지 목록 조회")
@GetMapping
public ResponseEntity<ResultResponse> messageList(@CurrentUser Member loginUser,
PageRequest pageRequest) {
Pageable pageable = pageRequest.of();
List<GetMessageResponse> result = messageService.findMessageList(loginUser, pageable);
return ResponseEntity.ok(ResultResponse.of(ResultCode.MESSAGE_PAGING_GET_SUCCESS, result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.graphy.backend.domain.message.dto.response;

import com.querydsl.core.annotations.QueryProjection;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@Builder
@NoArgsConstructor
public class GetMessageResponse {
private Long senderId;
private String memberNickname;
private String content;
private LocalDateTime sentAt;

@QueryProjection
public GetMessageResponse(Long senderId, String memberNickname, String content, LocalDateTime sentAt) {
this.senderId = senderId;
this.memberNickname = memberNickname;
this.content = content;
this.sentAt = sentAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.graphy.backend.domain.message.repository;

import com.graphy.backend.domain.message.dto.response.GetMessageResponse;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface MessageCustomRepository {
List<GetMessageResponse> findMessages(Pageable pageable, Long memberId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import com.graphy.backend.domain.message.domain.Message;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MessageRepository extends JpaRepository<Message, Long> {
public interface MessageRepository extends JpaRepository<Message, Long>, MessageCustomRepository {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.graphy.backend.domain.message.repository.custom;

import com.graphy.backend.domain.message.dto.response.GetMessageResponse;
import com.graphy.backend.domain.message.dto.response.QGetMessageResponse;
import com.graphy.backend.domain.message.repository.MessageCustomRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static com.graphy.backend.domain.member.domain.QMember.member;
import static com.graphy.backend.domain.message.domain.QMessage.message;

@RequiredArgsConstructor
public class MessageCustomRepositoryImpl implements MessageCustomRepository {
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<GetMessageResponse> findMessages(Pageable pageable, Long memberId) {
return jpaQueryFactory
.select(new QGetMessageResponse(
member.id,
member.nickname,
message.content,
message.createdAt
))
.from(message)
.join(message.sender, member)
.where(message.receiver.id.eq(memberId))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
import com.graphy.backend.domain.message.domain.Message;
import com.graphy.backend.domain.message.dto.request.CreateMessageRequest;
import com.graphy.backend.domain.message.dto.response.GetMessageDetailResponse;
import com.graphy.backend.domain.message.dto.response.GetMessageResponse;
import com.graphy.backend.domain.message.repository.MessageRepository;
import com.graphy.backend.domain.notification.domain.NotificationType;
import com.graphy.backend.domain.notification.dto.NotificationDto;
import com.graphy.backend.domain.notification.service.NotificationService;
import com.graphy.backend.global.error.ErrorCode;
import com.graphy.backend.global.error.exception.EmptyResultException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MessageService {
private final MessageRepository messageRepository;
private final MemberService memberService;
private final NotificationService notificationService;

@Transactional
public void addMessage(CreateMessageRequest request, Member loginUser) {
Member receiver = memberService.findMemberById(request.getToMemberId());
messageRepository.save(request.toEntity(loginUser, receiver));
Expand All @@ -41,4 +48,11 @@ public GetMessageDetailResponse findMessageById(Long messageId) {
);
return GetMessageDetailResponse.from(message);
}

public List<GetMessageResponse> findMessageList(Member loginUser, Pageable pageable) {
List<GetMessageResponse> messages = messageRepository.findMessages(pageable, loginUser.getId());

if (messages.isEmpty()) throw new EmptyResultException(ErrorCode.MESSAGE_NOT_EXIST);
else return messages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.graphy.backend.global.error.exception.EmptyResultException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -28,11 +29,7 @@ public class ApplicationService {
private final NotificationService notificationService;
private final TagService tagService;

/**
*TODO
* @Transactional 적용
* 중복 지원 방지 로직 추가
*/
@Transactional
public void addApplication(CreateApplicationRequest request, Member loginUser) {
Recruitment recruitment = recruitmentService.getRecruitmentById(request.getRecruitmentId());

Expand All @@ -56,6 +53,7 @@ public void addApplication(CreateApplicationRequest request, Member loginUser) {
notificationService.addNotification(notificationDto, recruitment.getMember().getId());
}

@Transactional(readOnly = true)
public GetApplicationDetailResponse findApplicationById(Long applicationId) {
Application application = applicationRepository.findApplicationWithFetch(applicationId)
.orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ public void removeRecruitment(Long recruitmentId, Member loginUser) {
recruitment.delete();
}

@Transactional(readOnly = true)
public Recruitment getRecruitmentById(Long id) {
return recruitmentRepository.findById(id).orElseThrow(() -> new EmptyResultException(ErrorCode.RECRUITMENT_NOT_EXIST));
}

@Transactional(readOnly = true)
public List<GetApplicationResponse> findApplicationList(Long recruitmentId, Pageable pageable) {
Page<Application> applicationList = applicationRepository.findAllByRecruitmentId(recruitmentId, pageable);
return applicationList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class RecruitmentTagService {
private final RecruitmentTagRepository recruitmentTagRepository;

@Transactional
public void removeProjectTag(Long projectId) {
recruitmentTagRepository.deleteAllByRecruitmentId(projectId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public enum ResultCode {

// Message
MESSAGE_CREATE_SUCCESS("MSG001", "쪽지 전송 성공"),
MESSAGE_GET_SUCCESS("MSG002", "쪽지 단건 조회 성공");
MESSAGE_GET_SUCCESS("MSG002", "쪽지 단건 조회 성공"),
MESSAGE_PAGING_GET_SUCCESS("MSG003", "쪽지 페이징 조회 성공");

private final String code;
private final String message;
Expand Down
Loading