Skip to content

Commit

Permalink
feat : 공지 읽음 표시 로직 추가 및 response 수정 (#151)
Browse files Browse the repository at this point in the history
* feat : 공지 조회 API에서 읽음 표시 로직 추가

* feat : 공지 목록 조회 시 읽음 여부 필드 추가

* feat : @joincolumn 누락 추가

* refactor : 공지 읽음 표시 메소드 리네이밍 및 코멘트 반영
  • Loading branch information
rladmstn authored Nov 13, 2024
1 parent f9ce138 commit c7f8043
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.gamzabat.algohub.feature.notice.domain;

import com.gamzabat.algohub.feature.user.domain.User;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class NoticeRead {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "notice_id")
private Notice notice;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Builder
public NoticeRead(Notice notice, User user) {
this.notice = notice;
this.user = user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ public record GetNoticeResponse(String author,
String content,
String title,
String category,
String createAt) {
String createAt,
boolean isRead) {

public static GetNoticeResponse toDTO(Notice notice) {
public static GetNoticeResponse toDTO(Notice notice, boolean isRead) {
return GetNoticeResponse.builder()
.author(notice.getAuthor().getNickname())
.noticeId(notice.getId())
.title(notice.getTitle())
.content(notice.getContent())
.category(notice.getCategory())
.createAt(DateFormatUtil.formatDate(notice.getCreatedAt().toLocalDate()))
.isRead(isRead)
.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gamzabat.algohub.feature.notice.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.gamzabat.algohub.feature.notice.domain.Notice;
import com.gamzabat.algohub.feature.notice.domain.NoticeRead;
import com.gamzabat.algohub.feature.user.domain.User;

public interface NoticeReadRepository extends JpaRepository<NoticeRead, Long> {
boolean existsByNoticeAndUser(Notice notice, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.notice.domain.Notice;
import com.gamzabat.algohub.feature.notice.domain.NoticeRead;
import com.gamzabat.algohub.feature.notice.dto.CreateNoticeRequest;
import com.gamzabat.algohub.feature.notice.dto.GetNoticeResponse;
import com.gamzabat.algohub.feature.notice.dto.UpdateNoticeRequest;
import com.gamzabat.algohub.feature.notice.exception.NoticeValidationException;
import com.gamzabat.algohub.feature.notice.repository.NoticeCommentRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeReadRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeRepository;
import com.gamzabat.algohub.feature.user.domain.User;

Expand All @@ -38,6 +40,7 @@ public class NoticeService {
private final NoticeCommentRepository noticeCommentRepository;
private final StudyGroupRepository studyGroupRepository;
private final GroupMemberRepository groupMemberRepository;
private final NoticeReadRepository noticeReadRepository;

@Transactional
public void createNotice(@AuthedUser User user, CreateNoticeRequest request) {
Expand Down Expand Up @@ -68,6 +71,8 @@ public GetNoticeResponse getNotice(@AuthedUser User user, Long noticeId) {
if (!groupMemberRepository.existsByUserAndStudyGroup(user, notice.getStudyGroup()))
throw new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 스터디 그룹 입니다.");

markNoticeAsRead(user, notice);

log.info("success to get notice");
return GetNoticeResponse.builder()
.author(notice.getAuthor().getNickname())
Expand All @@ -76,6 +81,7 @@ public GetNoticeResponse getNotice(@AuthedUser User user, Long noticeId) {
.content(notice.getContent())
.category(notice.getCategory())
.createAt(DateFormatUtil.formatDate(notice.getCreatedAt().toLocalDate()))
.isRead(true)
.build();
}

Expand All @@ -87,7 +93,9 @@ public List<GetNoticeResponse> getNoticeList(@AuthedUser User user, Long studyGr
throw new GroupMemberValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 스터디 그룹입니다");

List<Notice> list = noticeRepository.findAllByStudyGroup(studyGroup);
List<GetNoticeResponse> result = list.stream().map(GetNoticeResponse::toDTO).toList();
List<GetNoticeResponse> result = list.stream().map(
notice -> GetNoticeResponse.toDTO(notice, noticeReadRepository.existsByNoticeAndUser(notice, user))
).toList();
log.info("success to get notice list");
return result;
}
Expand Down Expand Up @@ -123,4 +131,12 @@ private void validateStudyGroupExists(Notice notice) {
.orElseThrow(() -> new StudyGroupValidationException(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 스터디 그룹입니다"));
}

private void markNoticeAsRead(User user, Notice notice) {
if (!noticeReadRepository.existsByNoticeAndUser(notice, user)) {
noticeReadRepository.save(
NoticeRead.builder().notice(notice).user(user).build()
);
}
log.info("success to read notice. userId: {}, noticeId: {}", user.getId(), notice.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.notice.domain.Notice;
import com.gamzabat.algohub.feature.notice.domain.NoticeRead;
import com.gamzabat.algohub.feature.notice.dto.CreateNoticeRequest;
import com.gamzabat.algohub.feature.notice.dto.GetNoticeResponse;
import com.gamzabat.algohub.feature.notice.dto.UpdateNoticeRequest;
import com.gamzabat.algohub.feature.notice.exception.NoticeValidationException;
import com.gamzabat.algohub.feature.notice.repository.NoticeCommentRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeReadRepository;
import com.gamzabat.algohub.feature.notice.repository.NoticeRepository;
import com.gamzabat.algohub.feature.notice.service.NoticeService;
import com.gamzabat.algohub.feature.user.domain.User;
Expand All @@ -52,6 +54,8 @@ public class NoticeServiceTest {
GroupMemberRepository groupMemberRepository;
@Mock
private NoticeCommentRepository noticeCommentRepository;
@Mock
private NoticeReadRepository noticeReadRepository;
@Captor
private ArgumentCaptor<Notice> noticeCaptor;

Expand Down Expand Up @@ -183,6 +187,7 @@ void getNoticeSuccess_1() {
assertThat(response.category()).isEqualTo("category");
assertThat(response.createAt()).isEqualTo(DateFormatUtil.formatDate(LocalDateTime.now().toLocalDate()));
assertThat(response.noticeId()).isEqualTo(1000L);
verify(noticeReadRepository, times(1)).save(any(NoticeRead.class));
}

@Test
Expand Down Expand Up @@ -247,6 +252,7 @@ void getNoticeListSuccess_1() {
assertThat(result.get(i).content()).isEqualTo("content" + i);
assertThat(result.get(i).title()).isEqualTo("title" + i);
assertThat(result.get(i).category()).isEqualTo("category" + i);
assertThat(result.get(i).isRead()).isFalse();
}
}

Expand Down

0 comments on commit c7f8043

Please sign in to comment.