Open eunja511005 opened 1 year ago
<?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.eun.tutorial.mapper.chat.IdeaMapper">
<select id="selectIdeaList" resultType="com.eun.tutorial.dto.chat.IdeaDTO">
SELECT ID, TITLE, EXPLANATION, TO_CHAR(START_DATE, 'yyyy-MM-dd'), TO_CHAR(END_DATE, 'yyyy-MM-dd'), STATUS, MANAGER, PARTICIPANTS, ATTACH_ID,
del_yn, create_id, TO_CHAR(create_dt, 'YYYY-MM-DD HH24:MI:SS') AS create_dt, update_id, TO_CHAR(update_dt, 'YYYY-MM-DD HH24:MI:SS') AS update_dt
FROM ZTMA_IDEA
where del_yn='N'
ORDER BY update_dt DESC
OFFSET #{offset} ROWS FETCH NEXT #{limit} ROWS ONLY
</select>
<select id="selectTotalIdeas" resultType="java.lang.Long">
SELECT COUNT(ID) FROM
ZTMA_IDEA
where del_yn='N'
</select>
<insert id="insertIdea">
INSERT INTO ZTMA_IDEA
(ID, TITLE, EXPLANATION, START_DATE, END_DATE, STATUS, MANAGER, PARTICIPANTS, ATTACH_ID,
del_yn, create_id, create_dt, update_id, update_dt)
VALUES (#{id}, #{title}, #{explanation}, CURRENT_DATE, CURRENT_DATE, #{status}, #{manager}, #{participants}, #{attachId},
'N', #{createId}, CURRENT_TIMESTAMP, #{updateId}, CURRENT_TIMESTAMP)
</insert>
<update id="updateIdea">
UPDATE ZTHH_CHAT
SET MESSAGE = #{message}, SENDER = #{sender}, ROOM_ID = #{roomId}, update_id = #{updateId}, update_dt = CURRENT_TIMESTAMP
WHERE id = #{id}
</update>
<delete id="deleteIdea">
UPDATE ZTMA_IDEA SET del_yn = 'Y', update_dt = CURRENT_TIMESTAMP WHERE id = #{id}
</delete>
<select id="getIdeaById" parameterType="string" resultType="com.eun.tutorial.dto.chat.IdeaDTO">
SELECT ID, TITLE, EXPLANATION, TO_CHAR(START_DATE, 'yyyy-MM-dd'), TO_CHAR(END_DATE, 'yyyy-MM-dd'), STATUS, MANAGER, PARTICIPANTS, ATTACH_ID,
del_yn, create_id, TO_CHAR(create_dt, 'YYYY-MM-DD HH24:MI:SS') AS create_dt, update_id, TO_CHAR(update_dt, 'YYYY-MM-DD HH24:MI:SS') AS update_dt
FROM ZTMA_IDEA
WHERE id = #{id}
ORDER BY update_dt, create_dt
</select>
</mapper>
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.eun.tutorial.dto.chat.ChatMessage;
import com.eun.tutorial.dto.chat.IdeaDTO;
@Mapper
public interface IdeaMapper {
List<IdeaDTO> selectIdeaList(@Param("offset") int offset, @Param("limit") int limit);
long selectTotalIdeas();
int insertIdea(IdeaDTO chatMessage);
int updateIdea(IdeaDTO chatMessage);
int deleteIdea(String id);
ChatMessage getIdeaById(String id);
}
import lombok.Data;
@Data
public class IdeaDTO {
private String id;
private String title;
private String explanation;
private String startDate;
private String endDate;
private String status;
private String manager;
private String participants;
private String attachId;
private String delYn;
private String createId;
private String createDt;
private String updateId;
private String updateDt;
}
import java.util.List;
import com.eun.tutorial.dto.chat.ChatListRequest;
import com.eun.tutorial.dto.chat.ChatMessage;
import com.eun.tutorial.dto.chat.IdeaDTO;
import com.eun.tutorial.dto.chat.IdeaListRequest;
public interface IdeaService {
List<IdeaDTO> getIdeaList(IdeaListRequest ideaListRequest);
long getTotalIdeas();
int saveIdea(IdeaDTO ideaDTO);
int updateIdea(IdeaDTO ideaDTO);
int deleteIdea(String id);
ChatMessage getIdeaById(String id);
}
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eun.tutorial.dto.chat.ChatMessage;
import com.eun.tutorial.dto.chat.IdeaDTO;
import com.eun.tutorial.dto.chat.IdeaListRequest;
import com.eun.tutorial.mapper.chat.IdeaMapper;
import lombok.AllArgsConstructor;
@Service
@AllArgsConstructor
@Transactional
public class IdeaServiceImpl implements IdeaService{
private final IdeaMapper ideaMapper;
@Override
public List<IdeaDTO> getIdeaList(IdeaListRequest ideaListRequest) {
int offset = (ideaListRequest.getPage() - 1) * ideaListRequest.getSize();
return ideaMapper.selectIdeaList(offset, ideaListRequest.getSize());
}
@Override
public int saveIdea(IdeaDTO ideaDTO) {
ideaDTO.setId("CHAT_"+UUID.randomUUID());
return ideaMapper.insertIdea(ideaDTO);
}
@Override
public int updateIdea(IdeaDTO ideaDTO) {
return ideaMapper.updateIdea(ideaDTO);
}
@Override
public int deleteIdea(String id) {
return ideaMapper.deleteIdea(id);
}
@Override
public ChatMessage getIdeaById(String id) {
return getIdeaById(id);
}
@Override
public long getTotalIdeas() {
return ideaMapper.selectTotalIdeas();
}
}
import lombok.Data;
@Data
public class IdeaListRequest {
private int page;
private int size;
}
import java.util.List;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class IdeaListResponse {
private boolean success;
private List<IdeaDTO> ideaList;
private long totalElements;
public IdeaListResponse(boolean success, List<IdeaDTO> ideaList, long totalElements) {
this.success = success;
this.ideaList = ideaList;
this.totalElements = totalElements;
}
}
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.eun.tutorial.dto.chat.IdeaDTO;
import com.eun.tutorial.dto.chat.IdeaListRequest;
import com.eun.tutorial.dto.chat.IdeaListResponse;
import com.eun.tutorial.dto.main.ApiResponse;
import com.eun.tutorial.service.chat.IdeaService;
import com.eun.tutorial.util.FileUtil;
import com.eun.tutorial.util.JwtTokenUtil;
import lombok.RequiredArgsConstructor;
@RestController
@RequiredArgsConstructor
public class IdeaController {
private final IdeaService ideaService;
private final FileUtil fileUtil;
@PostMapping("/api/idea/list")
public @ResponseBody IdeaListResponse getChatList(@RequestHeader("Authorization") String authToken, @RequestBody IdeaListRequest ideaListRequest) {
String token = authToken.substring(7); // "Bearer " 이후의 토큰 부분 추출
if (JwtTokenUtil.validateToken(token)) {
List<IdeaDTO> messages = ideaService.getIdeaList(ideaListRequest);
long totalElements = ideaService.getTotalIdeas();
return new IdeaListResponse(true, messages, totalElements);
}else {
// 토큰이 유효하지 않은 경우 예외 처리
throw new IllegalArgumentException("Invalid token");
}
}
@PostMapping("/api/idea")
public @ResponseBody ApiResponse<Map<String, Object>> saveIdea(@RequestHeader("Authorization") String token,
MultipartHttpServletRequest multipartFiles, @RequestPart("idea") IdeaDTO ideaDTO) throws IOException {
String authToken = token.substring(7); // "Bearer " 이후의 토큰 부분 추출
Map<String, Object> res;
if (JwtTokenUtil.validateToken(authToken)) {// 토큰이 유효한 경우
String username = JwtTokenUtil.extractUsername(authToken);
res = fileUtil.saveImageWithTableWithPath(multipartFiles, "openImg/chat");
return new ApiResponse<>(true, "Success save", res);
} else {
// 토큰이 유효하지 않은 경우 예외 처리
throw new IllegalArgumentException("Invalid token");
}
}
}
1. 아이디어 테이블 생성