인생 디벨로퍼

[2단계] Admin Court 삭제 본문

Project/Final Project - Sporting (매칭)

[2단계] Admin Court 삭제

뫄뫙뫄 2023. 5. 1. 21:00
728x90
반응형

삭제 기능은, db  delete 가 아닌, status 관리로 비활성화 시키기로 함!

 

Controller

 

@PostMapping("/admin/court/delete")
    public ResponseEntity<Object> courtDelete(@RequestParam("courtId") Long courtId) {
        boolean delete = stadiumCourtService.courtDelete(courtId);
        if (delete) {
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping("/admin/court/inactive")
    public String court_inactive(String keyword,
            @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.ASC) Pageable pageable,
            Model model) {

        Page<StadiumCourt> courts;
        if (keyword != null && !keyword.isEmpty()) {
            courts = stadiumCourtService.getCourtListByTitleContaining(keyword, pageable);
        } else {
            courts = stadiumCourtService.getStadiumCourtDeleteList(pageable);
        }

        int nowPage = courts.getPageable().getPageNumber() + 1;
        int startPage = ((nowPage - 1) / 5) * 5 + 1; // 버튼에서 첫 숫자
        int endPage = Math.min(nowPage + 5, courts.getTotalPages()); // 버튼에서 마지막 숫자

        model.addAttribute("courtList", courts.getContent());
        model.addAttribute("nowPage", nowPage);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("totalPage", courts.getTotalPages());
        model.addAttribute("keyword", keyword);
        return "/admin_court/inactive";
    }

 

status 변경과, 비활성화 페이지를 위한 GetMapping controller 도 만들어 줬다.

앞의 포스팅과 동일.


Service

    @Transactional
    public boolean courtDelete(Long courtId) {
        Optional<StadiumCourt> StadiumCourt = stadiumCourtRepository.findById(courtId);
        if (StadiumCourt.isPresent()) {
            StadiumCourt stadiumCourt = StadiumCourt.get();
            stadiumCourt.setStatus(StadiumCourtStatus.비활성);
            stadiumCourtRepository.save(stadiumCourt);

            return true;
        } else {
            return false;
        }
    }

    public Page<StadiumCourt> getStadiumCourtDeleteList(Pageable pageable) {
        StadiumCourt stadiumCourt = new StadiumCourt();
        stadiumCourt.setStatus(StadiumCourtStatus.비활성);
        Example<StadiumCourt> example = Example.of(stadiumCourt);
        return stadiumCourtRepository.findAll(example, pageable);
    }

삭제 된 코트는 status 가 '비활성' 을 save (update) 한다.


Repository

 public Page<StadiumCourt> getCourtListByTitleContaining(String title, Pageable pageable) {
        return stadiumCourtRepository.findByTitleContaining(title, pageable);
    }


public Page<StadiumCourt> getStadiumCourtDeleteList(Pageable pageable) {
        StadiumCourt stadiumCourt = new StadiumCourt();
        stadiumCourt.setStatus(StadiumCourtStatus.비활성);
        Example<StadiumCourt> example = Example.of(stadiumCourt);
        return stadiumCourtRepository.findAll(example, pageable);
    }

 

 


View

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

    <%@ include file="../layout/header.jsp" %>



        <div style="position :relative; height: 700px;">

            <div class="d-flex" style="position: absolute; left: 2px">

                <div class="container my-3">
                    <div class="list-group">
                        <a href="/admin/court" class="list-group-item list-group-item-action">
                            코트
                        </a>
                        <a href="/admin/court/wait" class="list-group-item list-group-item-action">승인대기</a>
                        <a href="/admin/court/inactive" class="list-group-item list-group-item-action active" aria-current="true">비활성화</a>
                    </div>
                </div>

                <div class="vl">

                </div>

            </div>

            <div class="d-flex justify-content-center">
                <div style="position: relative; top: 50px">
                    <table class="table">

                        <tr class="my-text-align">
                            <th scope="col" class="text-center">번호</th>
                            <th scope="col" class="text-center">코트이름</th>
                            <th scope="col" class="text-center">코트가격</th>
                            <th scope="col" class="text-center">수용 인원</th>
                            <th scope="col" class="text-center">등록 일자</th>
                            <th scope="col" class="text-center"></th>
                        </tr>

                        <c:forEach items="${courtList}" var="courtList">
                            <tr class="my-text-align">
                                <td class="text-center">${courtList.id}</td>
                                <td>${courtList.title}</td>
                                <td>${courtList.courtPrice}</td>
                                <td>${courtList.capacity}</td>
                                <td class="text-center">${MyDateUtils.toStringFormat(courtList.createdAt)}</td>
                            </tr>
                        </c:forEach>
                    </table>


                    <div class="d-flex justify-content-center mb-3">
                        <ul class="pagination">

                            <li class='page-item ${nowPage == 1 ? "disabled" : ""}'><a class="page-link"
                                    href="javascript:void(0);" onclick="callPrev();">Prev</a></li>

                            <c:forEach var="num" begin="${startPage}" end="${endPage}">

                            <li class="page-item ${num == nowPage ? 'active' : ''}">
                                <a class="page-link" href="/admin/court?page=${num-1}&keyword=${keyword}">${num}</a>
                            </li>
                            </c:forEach>

                            <li class='page-item ${nowPage == totalPage ? "disabled" : ""}'><a class="page-link"
                                    href="javascript:void(0);" onclick="callNext();">Next</a></li>

                        </ul>
                    </div>
                    <div class="input-group justify-content-center" style="position: absolute; bottom: 30;">
                        <div class="col-6">
                            <input id="keyword" name="query" type="text" class="form-control" placeholder="검색어 입력"
                                aria-label="search" value="" aria-describedby="button-addon2">
                        </div>
                        <div class="col-auto">
                            <button id="button-addon2" class="btn btn-primary" type="submit"
                                onclick="searchGet()">검색</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </div>
        <%@ include file="../layout/footer.jsp" %>

결과

여기서 삭제 버튼을 누르면,

비활성화 되서 여기 넘어온다!

728x90
반응형

'Project > Final Project - Sporting (매칭)' 카테고리의 다른 글

Sporting App 정리  (0) 2023.07.02
[2단계] Compony Info Update (S3)  (0) 2023.05.01
[2단계] Admin court 등록 승인  (0) 2023.05.01
[2단계] Admin page Court view  (0) 2023.05.01
[1단계] Stadium Detail  (0) 2023.05.01