package com.kaygb.kzblog.controller;/**
 * @Author: kaygb
 * @Date: 2021/7/16 15:14
 * @Blog: https://blog.kaygb.com/
 * @Description:
 */

import com.kaygb.kzblog.entity.Content;
import com.kaygb.kzblog.mapper.ContentMapper;
import com.kaygb.kzblog.mapper.UserMapper;
import com.kaygb.kzblog.utils.DataResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HengtaiController
 * @Description:
 * @Author: kaygb
 * @Date: 2021/07/2021/7/16 15:14
 * @Blog: https://blog.kaygb.com/
 */
@RestController
@RequestMapping("/hengtai")
public class HengtaiController {

	@Resource
	UserMapper userMapper;
	@Resource
	ContentMapper contentMapper;

	@GetMapping("/articles")
	public ModelAndView articlesMV(Model model){
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		model.addAttribute("username",authentication.getName());
		return new ModelAndView("back/articles");
	}

	@GetMapping("/articles/table")
	public DataResponse articleList(){
		List<Content> contentList = contentMapper.selectList(null);
//		Map<String, Content> dataTable  = new HashMap<>();
//		dataTable.put("data",contentList);
		return new DataResponse().success().data(contentList);
	}
	@GetMapping("/articles/create")
	public ModelAndView createArticleMV(Model model){
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		model.addAttribute("username",authentication.getName());
		return new ModelAndView("back/articleAdd");
	}
	@PostMapping("/articles/create")
	public Integer createArticle(@RequestParam("title") String title,@RequestParam("content") String article){
		Content content1 = new Content();
		content1.setCreateTime(new Date());
		content1.setTitle(title);
		content1.setArticle(article);
		contentMapper.insert(content1);
		return 0;
	}

}
