eunja511005 / AutoCoding

0 stars 0 forks source link

:new: 아이디어 저장 기능 구현 #152

Open eunja511005 opened 1 year ago

eunja511005 commented 1 year ago

1. 아이디어 테이블 생성

drop table ZTMA_IDEA;
CREATE TABLE ZTMA_IDEA (
    id VARCHAR2(200),
    title VARCHAR2(255) NOT NULL,
    explanation VARCHAR2(1000) NOT NULL,
    start_date DATE,
    end_date DATE,
    status VARCHAR2(50),
    manager VARCHAR2(100),
    participants VARCHAR2(1000),
    attach_id VARCHAR2(500),
    del_yn CHAR(1),
    create_id VARCHAR2(50),
    create_dt TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    update_id VARCHAR2(50),
    update_dt TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT pk_ztma_idea PRIMARY KEY (id)
);
eunja511005 commented 1 year ago

2. IdeaMapper.xml

<?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>
eunja511005 commented 1 year ago

3. IdeaMapper.java

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);
}
eunja511005 commented 1 year ago

4. IDeaDTO.java

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;

}
eunja511005 commented 1 year ago

5. IdeaService.java

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);
}
eunja511005 commented 1 year ago

6. IdeaServiceImpl.java

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();
    }

}
eunja511005 commented 1 year ago

7. IdeaListRequest.java

import lombok.Data;

@Data
public class IdeaListRequest {
    private int page;
    private int size;
}
eunja511005 commented 1 year ago

8. IdeaListResponse.java

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;
    }
}
eunja511005 commented 1 year ago

9. IdeaController.java

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");
        }
    }
}