본문 바로가기
Project/소경관

[소경관] : 직접 체크 로직 구현 정리하기

by 오주현 2022. 5. 23.
반응형

등록한 주민과 차량 정보를 불러와 본격적으로 근무를 할 때 사용하게 될 부분을 구현한다.

기존에 있는 리스트를 불러와 Checkbox 형태로 해당 주민의 차량이 주차되어 있는지 체크하는 로직이다.

@GetMapping("/touchCheck")
public String touchCheck(Model model) throws Exception {

    List<CarDTO> carDTOList = iCarListService.getFullCarList();

    CheckListVo checkListVo = new CheckListVo();
    checkListVo.setCarDtoList(carDTOList);

    model.addAttribute("carDTOList", carDTOList);
    model.addAttribute("checkListVo", checkListVo);

    return "carCheck/touchCheck";
}

터치 체크 로직 페이지에 Model 객체에 값을 담아 보내준다.

이때, Model 객체에 담기는 값은 MongoDB에 있는 전체 차량 리스트와 Checkbox의 true, false 값을 포함하여 담을 Vo를 담아서 보내준다.

 

<form th:action="@{/carCheck/touchCheckSave}" th:object="${checkListVo}" method="post">

    <table class="table table-striped">

        <thead>
        <tr>
            <th scope="col">이름</th>
            <th scope="col">연락처</th>
            <th scope="col">차량번호</th>
            <th scope="col">주소</th>
            <th scope="col">구분</th>
            <th scope="col">주차</th>
        </tr>
        </thead>

        <tbody>
        <tr th:each="m: ${checkListVo.carDtoList}">

            <td>
                <input type="text" class="btn btn-outline-primary" th:field="*{carDtoList[__${mStat.index}__].name}" readonly>
            </td>

            <td>
                <input type="text" class="btn btn-outline-primary" th:field="*{carDtoList[__${mStat.index}__].phoneNumber}" readonly>
            </td>

            <td>
                <input type="text" class="btn btn-outline-primary" th:field="*{carDtoList[__${mStat.index}__].carNumber}" readonly>
            </td>

            <td>
                <input type="text" class="btn btn-outline-primary" th:field="*{carDtoList[__${mStat.index}__].address}" readonly>
            </td>

            <td>
                <input type="text" class="btn btn-outline-primary" th:field="*{carDtoList[__${mStat.index}__].sort}" readonly>
            </td>

            <td>
                <input th:type="checkbox" class="form-check-input" th:field="*{carDtoList[__${mStat.index}__].check}" th:value="true" readonly>
            </td>

        </tr>
        </tbody>

    </table>

    <button th:type="submit" th:text="저장하기"></button>

</form>

주민 리스트 정보와 Checkbox의 정보를 같이 담을 checkListVo를 object로 받는다.

each문을 통해 리스트만큼 반복하게 만들고 아래서는 input으로 field값을 기존에 주민 리스트가 담겨있는 carDtoList에 맞는 인덱스 값과 매핑시켜 담아서 가져온다.

 

// 터치 체크 저장 로직
@PostMapping("/touchCheckSave")
 public String touchCheckSave(@ModelAttribute CheckListVo checkListVo) throws Exception {

    boolean res = iCheckService.saveTouchCheck(checkListVo);

    if (res == false) {
        return "carCheck/touchCheckSave";
    } else {
        return "carCheck/carCheck";
    }

}

ModelAttribute로 html에서 object에 담긴 값을 가져오고 Service 로직에 값을 보내준다. 이때, boolean으로 리턴 값을 설정해 줘서 체크가 된 경우와 안 된 경우를 구분해 주었다.

나중에 메시지를 포함시켜 Alert 처리도 해줘야한다.

디자인도 다듬어야 하고, 테이블 위치도 수정해야 하지만 일단, 로직만 정리한다.

Service는 인터페이스를 따로 만들어서 오버라이딩으로 사용했고, Controller에서는 의존성을 주입해 사용하고 있다.

인터페이스 코드는 GitHub를 참고하고 Service 로직만 다룬다.

 

@Override
public boolean saveTouchCheck(CheckListVo checkListVo) throws Exception {

    boolean res;
    List<CarDTO> list = new ArrayList<>();

    for (CarDTO carDTO : checkListVo.getCarDtoList()) {
        list.add(carDTO);
    }

		// 생성할 컬렉션 명
    String colNm = "CHECK_" + DateUtil.getDateTime("yyyyMMdd hh:mm:ss");

    // MongoDB에 데이터 저장
    res = iCheckMapper.saveTouchCheck(list, colNm);

    return res;
}

MongoDB에 체크 일 및 시간을 컬렉션 이름으로 설정하기 위해서(중복 방지 겸 언제 체크했나 확인 용도) 따로 String 값을 설정해 주었고 이 값을 Controller에서 받은 값과 같이 Mapper로 넘겨준다.

마찬가지로 인터페이스에 정의하고 오버라이딩하여 사용한다.

 

@Override
public boolean saveTouchCheck(List<CarDTO> list, String colNm) throws Exception {

    boolean res;

    // 데이터를 저장할 컬렉션 생성
    mongo.createCollection(colNm);

    MongoCollection<Document> col = mongo.getCollection(colNm);

    for (CarDTO carDTO : list) {

        if (carDTO == null) {
            carDTO = new CarDTO();
        }

        col.insertOne(new Document(new ObjectMapper().convertValue(carDTO, Map.class)));
    }

    res = true;

    return res;
}

Mapper에서는 Service에서 받아온 컬렉션 명이 될 colNm 값으로 컬렉션을 생성해주었고, 저장할 컬렉션 객체를 생성해 준 뒤, for문에서 null 체크를 거쳐 DTO를 Map 데이터 타입으로 변경하고 변경된 Map 데이터 타입을 Document로 변경하여 하나씩 저장해 준다. 이렇게 되면 MongoDB에 리스트와 같이 정상적으로 값이 저장되게 된다.


아직 최종 완성 로직이 아니라 리팩터링해야 할 부분이 많이 있다. 지금은 단지, 돌아가게 만드는 것에 집중해서 만들고 있다.

반응형

댓글