Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

BoardController.java 본문

SPRING/chapter04_MVC

BoardController.java

GAWON 2023. 7. 14. 14:36
package org.joonzis.controller;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.joonzis.domain.BoardAttachVO;
import org.joonzis.domain.BoardVO;
import org.joonzis.domain.Criteria;
import org.joonzis.domain.PageDTO;
import org.joonzis.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import lombok.Setter;
import lombok.extern.log4j.Log4j;

@Log4j
@Controller
@RequestMapping("/board/*")
public class BoardController {
	
	// 서비스 객체 받아와야 됨
	@Setter(onMethod_ = @Autowired)
	private BoardService service;

	@GetMapping("/list")
	public String list(Model model, Criteria cri ) {
		log.info("list...");
		int total = service.getTotal();
		log.info(total);
		model.addAttribute("list", service.getList(cri));
		model.addAttribute("pageMaker", new PageDTO(cri, total));
		return "board/list";
	}
	
	@PreAuthorize("isAuthenticated()")	//인증된 사용자라면 true
	@GetMapping("/register")
	public String register(Model model, Criteria cri) {
		model.addAttribute("cri",cri);
		return "board/register";
	}
	
	@PreAuthorize("isAuthenticated()")
	@PostMapping("/register")
	public String register(BoardVO vo, RedirectAttributes rttr) {
		log.info("register..." + vo);
		service.register(vo);
		rttr.addFlashAttribute("result", "ok");
		return "redirect:/board/list";//URL을 태워줄때 redirect:붙여준다 아니면view로 간다
		
	}
	
	@GetMapping("/get")
	public String get(@RequestParam("bno")long bno, Model model, Criteria cri) {
		log.info("/get..." + bno);
		model.addAttribute("vo", service.get(bno));
		model.addAttribute("cri", cri);
		return "board/get";
	}
	
	@GetMapping("/modify")
	public String modify(@RequestParam("bno")long bno, Model model, Criteria cri) {
		log.info("modify");
		model.addAttribute("vo", service.get(bno));
		model.addAttribute("cri", cri);
		return "board/modify";
	}//로그인했을때 보이기권한체크(글수정,글삭제) 로그인안하면 안보이기
	
	// 메소드 실행 전, 로그인한 사용자와 파라미터로 받은 작성자가 일치하는지 체크
	@PreAuthorize("principal.username == #vo.writer")
	@PostMapping("/modify")
	public String modify(@RequestParam("bno")long bno,  BoardVO vo , RedirectAttributes rttr, Criteria cri) {
		log.info("modify : " + vo );
		List<BoardAttachVO> attachList = service.getAttachList(bno);
		if(service.modify(vo)) {
			deleteFiles(attachList);
			rttr.addFlashAttribute("result" , "success");
			
		}
		return "redirect:/board/list";
	}//토큰 실어보내기(post에서는 항상 토큰을 보낸다)
	
	private void deleteFiles(List<BoardAttachVO> attachList) {
		if(attachList == null || attachList.size() == 0) {
			return;
		}
		log.info("delete attach files........");
		log.info(attachList);
		
		attachList.forEach(attach -> {
			try {
				Path file = Paths.get("c:\\upload\\"+attach.getUploadPath()+"\\"+attach.getUuid()+"_"+attach.getFileName());
				Files.deleteIfExists(file);
				if(Files.probeContentType(file).startsWith("image")) {
					Path thumNail = Paths.get("c:\\upload\\"+attach.getUploadPath()+"\\s_"+attach.getUuid()+"_"+attach.getFileName());
					Files.delete(thumNail);
				}
			} catch(Exception e) {
				log.error("delete file error" + e.getMessage());
			}
		});
	}
	
	@PreAuthorize("principal.username == #vo.writer")
	@PostMapping("/remove")
	public String remove(
		@RequestParam("bno") Long bno, Criteria cri, RedirectAttributes rttr) {
			log.info("remove..." + bno);
	
		List<BoardAttachVO> attachList = service.getAttachList(bno);
	
		if (service.remove(bno)) {
			deleteFiles(attachList);  // 첨부파일 삭제
			rttr.addFlashAttribute("result", "success");
		}
		return "redirect:/board/list" + cri.getListLink();  // 목록으로 이동
	}
	
	
	
	@GetMapping(value = "/getAttachList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ResponseEntity<List<BoardAttachVO>> getAttachList(long bno){
		log.info("getAttachList..." + bno);
		return new ResponseEntity<>(service.getAttachList(bno), HttpStatus.OK);
	}

	
	
}

'SPRING > chapter04_MVC' 카테고리의 다른 글

UploadController.java  (0) 2023.07.18
SampleController.java  (0) 2023.07.18
ReplyController.java  (0) 2023.07.18
HomeController.java  (0) 2023.07.18
CommonController.java  (0) 2023.07.14