Skip to content

Commit

Permalink
메시지 id 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomeo184 committed Jan 14, 2024
1 parent 9d5882b commit 0a55dcb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/offer/msg/application/MsgService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ public CommonCreationResponse sendMsg(Long msgRoomId, MsgCreateRequest request,
}

@Transactional(readOnly = true)
public List<MsgInfoResponse> getMsgs(int page, Long msgRoomId) {
public List<MsgInfoResponse> getMsgs(int page, Long msgRoomId, Long loginMemberId) {
PageRequest pageRequest = PageRequest.of(SliceUtils.getSliceNumber(page), Properties.DEFAULT_SLICE_SIZE);

Slice<Msg> msgs = msgRepository.findSliceByRoomId(pageRequest, msgRoomId);
Slice<Msg> msgs = msgRepository.findSliceByRoomIdOrderByCreatedAtAsc(pageRequest, msgRoomId);

msgs.stream()
.filter(m -> !m.isRead())
.filter(m -> !m.getSenderId().equals(loginMemberId) && !m.isRead())
.forEach(m -> {
m.markRead();
msgRepository.save(m);
});

return msgs.stream()
.map(m -> MsgInfoResponse.from(
m.getId(),
m.getContent(),
memberRepository.getById(m.getSenderId()),
m.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@Getter
@RequiredArgsConstructor
public class MsgInfoResponse {

private final Long id;
private final String content;

private final PartnerBriefResponse member;
Expand All @@ -27,8 +29,9 @@ public static class PartnerBriefResponse {
private final String nickname;
}

public static MsgInfoResponse from(String content, Member member, LocalDateTime sendTime) {
public static MsgInfoResponse from(Long id, String content, Member member, LocalDateTime sendTime) {
return new MsgInfoResponse(
id,
content,
new PartnerBriefResponse(member.getId(), member.getProfileImageUrl(), member.getNickname()),
sendTime
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/offer/msg/domain/MsgRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public interface MsgRepository extends JpaRepository<Msg, Long> {
@Override
<S extends Msg> S save(S entity);

Slice<Msg> findSliceByRoomId(PageRequest pageRequest, Long roomId);
Slice<Msg> findSliceByRoomIdOrderByCreatedAtAsc(PageRequest pageRequest, Long roomId);

List<Msg> findAllByRoomAndSenderId(MsgRoom room, Long sellerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public ResponseEntity<ApiResponse<List<MsgInfoResponse>>> getAllMsgs(
@PathVariable Long msgRoomId,
@RequestParam(required = true) int page) {

List<MsgInfoResponse> response = msgService.getMsgs(page, msgRoomId);
List<MsgInfoResponse> response = msgService.getMsgs(page, msgRoomId, loginMember.getId());

return ResponseEntity.ok(
ApiResponse.of(ResponseMessage.SUCCESS, response)
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,27 @@ insert into offer(is_selected, price, created_at, offerer_id, post_id, trade_typ
insert into offer(is_selected, price, created_at, offerer_id, post_id, trade_type) values (false, 30000, now(), 3, 18, 'FACE_TO_FACE');

insert into msg_room(created_at, offer_id, offerer_id, seller_id) values (now(), 5, 3, 1);
insert into msg_room(created_at, offer_id, offerer_id, seller_id) values (now(), 6, 3, 4);

insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 1, 1, '판매자로부터의 첫 메시지입니다.');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '팔아요~');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '이거 사세요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '사시라구요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '얼른');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '사시에료');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, 'ㅎㅎ');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕하세요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '구매 원하세요?');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '구매해주세요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '반가워요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕하세요');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '바보');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '바보');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕');
insert into msg(is_read, created_at, room_id, sender_id, content) values (false, now(), 5, 4, '안녕하세요');

insert into review(reviewee_is_buyer, score, created_at, modified_at, post_id, reviewee_id, reviewer_id, content) values (false, 1, now(), now(), 20, 1, 3, '조타이 조타이~?');
insert into review(reviewee_is_buyer, score, created_at, modified_at, post_id, reviewee_id, reviewer_id, content) values (false, 1, now(), now(), 18, 1, 3, '조타이 조타이~?');
Expand Down

0 comments on commit 0a55dcb

Please sign in to comment.