package com.sparta.springassignment1.controller;
import com.sparta.springassignment1.dto.ClientDTO;
import com.sparta.springassignment1.entity.Client;
import com.sparta.springassignment1.service.ClientService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequiredArgsConstructor
public class ClientController {
private final ClientService clientService;
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/api/blog/view/{id}")
public String blogPostView(Model model, @PathVariable Long id) {
model.addAttribute("client", clientService.getClientPostByid(id).get());
return "view";
}
@PostMapping("/api/blog/write")
public String createMemo(@RequestBody ClientDTO clientDTO) {
clientService.createPost(clientDTO);
return "redirect:/api/blog/list";
}
@GetMapping("/api/blog/write")
public String blogPostWrite() {
return "write";
}
@GetMapping("/api/blog/list")
public String getPost(Model model) {
model.addAttribute("client", clientService.getAllPost());
return "list";
}
@GetMapping("/api/blog/update/{id}")
public String updatePro(@PathVariable("id") Long id, Model model, String password) {
Client client = clientService.getClientPostByid(id).get();
// if (clientService.getClientPassWord(password)) {
ClientDTO clientDTO = new ClientDTO();
clientDTO.setUsername(client.getUsername());
clientDTO.setTitle(client.getTitle());
clientDTO.setContent(client.getContent());
model.addAttribute("clientDTO", clientDTO);
return "edit";
}
// }
@PostMapping("/api/blog/delete/{id}")
public String deleteById(@PathVariable("id") Long id, @RequestParam("password") String password, Model model) {
boolean deleteCheckFlag = clientService.deletePost(id, password);
if (!deleteCheckFlag) {
model.addAttribute("message", "비밀번호가 일치하지 않습니다.");
return "redirect:/api/blog/postDelete";
}
model.addAttribute("message", "게시글 삭제가 성공하였습니다.");
return "redirect:/api/blog/list";
}
@GetMapping("/api/blog/delete/{id}")
public String deletePrepared(@PathVariable("id") Long id, Model model) {
model.addAttribute("id", id);
return "postDelete";
}
}