[DIOS] ์ƒํ’ˆ ์‚ญ์ œ

๐Ÿ“Œ ์ƒํ’ˆ ์‚ญ์ œ(D)

- ์กฐ๊ฑด : ๊ด€๋ฆฌ์ž๋งŒ ์ ‘๊ทผ๊ฐ€๋Šฅ

- ๊ธฐ๋Šฅ : ์ƒํ’ˆ ์ •๋ณด ์‚ญ์ œ

 

โœ… ์กฐ๊ฑด์„ค์ •  :  ๊ด€๋ฆฌ์ž๋ง‰ ์ƒํ’ˆ์‚ญ์ œ ๊ฐ€๋Šฅ

<a class="title" id="itemDelete" th:if="${session.user!=null && session.user.isAdmin}">
        <span class="text">์ƒํ’ˆ ์‚ญ์ œํ•˜๊ธฐ</span>
</a>

 

์ƒํ’ˆ ์ฝ๊ธฐ html ์—์„œ '์ƒํ’ˆ ์‚ญ์ œํ•˜๊ธฐ' ๋ฒ„ํŠผ์„ ๋งŒ๋“  ํƒœ๊ทธ์— ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ด€๋ฆฌ์ž์ผ๋•Œ์—๋งŒ ๋ณด์—ฌ์ง€๋„๋ก ์กฐ๊ฑด์„ ์„ค์ •ํ•ด์ค€๋‹ค.

 

โ„น๏ธ Controller 

@RequestMapping(value = "read",
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody 
    public String deleteRead(@SessionAttribute(value = "user", required = false) UserEntity user,
                             @RequestParam(value = "gid") int gid) {
        ItemEntity item = new ItemEntity();
        item.setIndex(gid);

        Enum<?> result = this.goodsService.deleteItem(item, user);
        JSONObject responseJson = new JSONObject();
        responseJson.put("result", result.name().toLowerCase());
        if (result == CommonResult.SUCCESS) {
            responseJson.put("cad", item.getCategoryId());
        }
        return responseJson.toString();
    }

โ„น๏ธ Service

@Transactional
    public Enum<? extends IResult> deleteItem(ItemEntity item, UserEntity user) {
        ItemEntity existingItem = this.goodsMapper.selectItemByIndex(item.getIndex());

        if (existingItem == null) {
            return CommonResult.FAILURE;
        }

        if (user == null || !user.isAdmin()) { 
            return CommonResult.FAILURE;
        }

        item.setCategoryId(existingItem.getCategoryId());
        return this.goodsMapper.deleteItemByIndex(item.getIndex()) > 0
                ? CommonResult.SUCCESS
                : CommonResult.FAILURE;
    }

โ„น๏ธ JavaScript

itemDelete.addEventListener('click', e => {
    e.preventDefault();
    if (!confirm('์ •๋ง๋กœ ์ด ์ƒํ’ˆ์„ ์‚ญ์ œํ• ๊นŒ์š”?')) {
        return;
    }
    const xhr = new XMLHttpRequest();
    xhr.open('DELETE', window.location.href);
    xhr.onreadystatechange = () => {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status >= 200 && xhr.status < 300) {
                const responseObject = JSON.parse(xhr.responseText);
                switch (responseObject['result']) {
                    case 'success':
                        window.location.href = '/store/list'
                        break;
                    default:
                        alert('์•Œ ์ˆ˜ ์—†๋Š” ์ด์œ ๋กœ ๊ฒŒ์‹œ๊ธ€์„ ์‚ญ์ œํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.\n\n์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.');
                }
            } else {
                alert('์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜์ง€ ๋ชปํ•˜์˜€์Šต๋‹ˆ๋‹ค.\n\n์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.');
            }
        }
    };
    xhr.send();
});

์ƒํ’ˆ ์‚ญ์ œ์— ์„ฑ๊ณต ์‹œ ์ƒํ’ˆ ๋ชฉ๋ก(S) ์ฃผ์†Œ๋กœ ์ด๋™ ํ•˜๊ณ  ์‹คํŒจ์‹œ ๊ทธ์— ๋งž๋Š” ์˜ค๋ฅ˜ ๊ฒฝ๊ณ ์ฐฝ์„ ๋„์šด๋‹ค.