Skip to content

Commit

Permalink
Merge pull request #36 from handong-app/feature/detail/message
Browse files Browse the repository at this point in the history
Feature/detail/message
  • Loading branch information
Callein authored Oct 30, 2024
2 parents 4957eac + 0262bf9 commit 55e4250
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/com/thc/realspr/controller/TbsubjectController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.thc.realspr.controller;

import com.thc.realspr.dto.TbmessageDto;
import com.thc.realspr.dto.TbsubjectDto;
import com.thc.realspr.dto.UserInteractionDto;
import com.thc.realspr.service.TbKaFeedService;
import com.thc.realspr.service.TbsubjectService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/api/subject")
@RestController
public class TbsubjectController {

private final TbsubjectService tbsubjectService;

public TbsubjectController(
TbsubjectService tbsubjectService
) {
this.tbsubjectService = tbsubjectService;
}

@Operation(summary = "주제 세부내용",
description = "주제의 id와 유저 id로 세부내용 조회 <br />"
+ "@param Long subjectId <br />"
+ "@return HttpStatus.OK(200) ResponseEntity\\<TbsubjectDto.DetailResDto\\> <br />"
+ "@exception 필수 파라미터 누락하였을 때 등 <br />"
+ "@exception 404 Not Found (주제 또는 유저를 찾을 수 없는 경우) <br />"
+ "@exception 400 Bad Request (필수 파라미터 누락 시)"
)
@GetMapping("/{subjectId}")
public ResponseEntity<TbsubjectDto.DetailResDto> getSubjectDetail(@Valid @PathVariable("subjectId") Long subjectId, HttpServletRequest request) {
String userId = (String) request.getAttribute("reqUserId");
if (userId == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}

TbsubjectDto.DetailResDto detail = tbsubjectService.getDetail(
TbsubjectDto.DetailReqDto.builder()
.subjectId(subjectId)
.userId(userId)
.build()
);

if (detail == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

return ResponseEntity.status(HttpStatus.OK).body(detail);
}


}
69 changes: 69 additions & 0 deletions src/main/java/com/thc/realspr/dto/TbsubjectDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thc.realspr.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.List;

public class TbsubjectDto {
@Schema(description = "주제 세부내용 요청 DTO")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class DetailReqDto {
private Long subjectId;
private String userId;
}


@Schema(description = "주제 세부내용 응답 DTO")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class DetailResDto {
private Long subjectId;
private boolean like;
private LastMessageServDto lastMessage;
private List<MessageHistoryServDto> messageHistory;
}

@Schema(description = "주제 세부내용 서비스 DTO")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class DetailServDto {
private Long subjectId;
private boolean like;
private String lastMessageId;
}



@Schema(description = "주제 마지막 메시지 서비스 DTO")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class LastMessageServDto {
private String id;
private long sentAt;
private String message;
}

@Schema(description = "주제 메시지 이력 서비스 DTO")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class MessageHistoryServDto {
private String id;
private long sentAt;
private String message;
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/thc/realspr/mapper/TbsubjectMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.thc.realspr.mapper;

import com.thc.realspr.dto.TbsubjectDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TbsubjectMapper {
TbsubjectDto.DetailServDto getDetailById(TbsubjectDto.DetailReqDto reqDto);

TbsubjectDto.LastMessageServDto getLastMessageByMessageId(String lastMessageId);

List<TbsubjectDto.MessageHistoryServDto> getMessageHistoryById(Long subjectId);
}
13 changes: 13 additions & 0 deletions src/main/java/com/thc/realspr/service/TbsubjectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.thc.realspr.service;


import com.thc.realspr.dto.TbsubjectDto;
import org.springframework.stereotype.Service;


@Service
public interface TbsubjectService {

TbsubjectDto.DetailResDto getDetail(TbsubjectDto.DetailReqDto param);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.thc.realspr.service.impl;

import com.thc.realspr.dto.TbsubjectDto;
import com.thc.realspr.mapper.TbsubjectMapper;
import com.thc.realspr.service.TbsubjectService;
import org.springframework.stereotype.Service;


@Service
public class TbsubjectServiceImpl implements TbsubjectService {

private final TbsubjectMapper tbsubjectMapper;

public TbsubjectServiceImpl(TbsubjectMapper tbsubjectMapper) {
this.tbsubjectMapper = tbsubjectMapper;
}

public TbsubjectDto.DetailResDto getDetail(TbsubjectDto.DetailReqDto param){
TbsubjectDto.DetailServDto detailServDto = tbsubjectMapper.getDetailById(param);
return TbsubjectDto.DetailResDto.builder()
.subjectId(detailServDto.getSubjectId())
.like(detailServDto.isLike())
.lastMessage(tbsubjectMapper.getLastMessageByMessageId(detailServDto.getLastMessageId()))
.messageHistory(tbsubjectMapper.getMessageHistoryById(detailServDto.getSubjectId()))
.build();
}
}
44 changes: 44 additions & 0 deletions src/main/resources/mapper/TbsubjectMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thc.realspr.mapper.TbsubjectMapper">

<!-- 주제 세부내용 조회 쿼리 -->
<select id="getDetailById" parameterType="hashMap" resultType="com.thc.realspr.dto.TbsubjectDto$DetailServDto">
SELECT
ts.id AS subjectId,
CASE
WHEN tul.userId IS NOT NULL THEN true
ELSE false
END AS `like`,
tkm.id AS lastMessageId
FROM
TbSubject ts
LEFT JOIN TbKaMessage tkm
ON ts.last_sent_chat_id = tkm.chat_id
LEFT JOIN mydb_TbUserLike tul
ON ts.id = tul.subjectId
AND tul.userId = #{userId}
AND tul.deleted = 'N'
WHERE
ts.id = #{subjectId}
GROUP BY
ts.id, tul.userId, tkm.id;
</select>

<!-- 주제 마지막 메시지 조회 쿼리 -->
<select id="getLastMessageByMessageId" parameterType="hashMap" resultType="com.thc.realspr.dto.TbsubjectDto$LastMessageServDto">
SELECT id, last_sent_at AS sentAt, message
FROM TbKaMessage
WHERE id = #{lastMessageId}
</select>

<!-- 주제 메시지 이력 조회 쿼리 -->
<select id="getMessageHistoryById" parameterType="hashMap" resultType="com.thc.realspr.dto.TbsubjectDto$MessageHistoryServDto">
SELECT id, last_sent_at AS sentAt, message
FROM TbKaMessage
WHERE subject_id = #{subjectId}
ORDER BY last_sent_at DESC
</select>


</mapper>

0 comments on commit 55e4250

Please sign in to comment.