-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat : 공지 읽음 표시 로직 추가 및 response 수정 #151
Changes from 3 commits
a46396d
7ca8580
b9b215d
a9a934a
d126d95
43d1134
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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) { | ||
|
@@ -67,13 +70,16 @@ public GetNoticeResponse getNotice(@AuthedUser User user, Long noticeId) { | |
if (!groupMemberRepository.existsByUserAndStudyGroup(user, notice.getStudyGroup())) | ||
throw new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 스터디 그룹 입니다."); | ||
|
||
readNotice(user, notice); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개인적으로 이름이 "공지읽기" 여서 뭔가.. 역할을 잘 표현하는 것 같다는 느낌이 와닿지는 않네요. 그렇다고 추천해주자니 애매하고..ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네이밍 때문에 머리 터질 것 같아요.. 저도 찝찝한 이름이지만 더 생각이 안나서 :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 굳 👍 |
||
|
||
log.info("success to get notice"); | ||
return GetNoticeResponse.builder() | ||
.author(notice.getAuthor().getNickname()) | ||
.noticeId(notice.getId()) | ||
.noticeTitle(notice.getTitle()) | ||
.noticeContent(notice.getContent()) | ||
.createAt(DateFormatUtil.formatDate(notice.getCreatedAt().toLocalDate())) | ||
.isRead(noticeReadRepository.existsByNoticeAndUser(notice, user)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 무조건 true 아닌가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 공지글 하나 조회니 그러겠네요! 수정해야겠어요 감삼다 |
||
.build(); | ||
} | ||
|
||
|
@@ -85,7 +91,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; | ||
} | ||
|
@@ -121,4 +129,12 @@ private void validateStudyGroupExists(Notice notice) { | |
.orElseThrow(() -> new StudyGroupValidationException(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 스터디 그룹입니다")); | ||
} | ||
|
||
private void readNotice(User user, Notice notice) { | ||
if (!noticeReadRepository.existsByNoticeAndUser(notice, user)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 참고차 코멘트입니다 사실 이렇게 ifNonExist -> Insert 구조일때 타이밍 이슈가 생길수 있습니다. 그래서 엄밀해야하는 상황에서는 유저, 노티스 묶어서 Unique index를 걸고, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코멘트에 대한 질문인데 ..! 서버가 좀 느린 상황에서 광클을 하면 똑같은 요청이 두세번 가게 되고 그에 대해 똑같은 NoticeRead객체가 저장될 수 있으니 까지는 이해했는데 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코멘트에 대한 은수 답이 달리면 머지할게용 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어우 이제봤네요 늦어져서 죄송합니담 만약에 그럼 |
||
noticeReadRepository.save( | ||
NoticeRead.builder().notice(notice).user(user).build() | ||
); | ||
} | ||
log.info("success to read notice. userId: {}, noticeId: {}", user.getId(), notice.getId()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테이블 이름 챗지랑 합의해서 지은건가요? ㅋㅋㅋ 그냥 느낌이 와서..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정답-!