@Controller
@RequestMapping("/admin/article")
@Transactional(rollbackFor = TipException.class)
public class ArticleController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ArticleController.class);

    @Resource
    private IContentService contentsService;

    @Resource
    private IMetaService metasService;

    @Resource
    private ILogService logService;

    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "15") int limit, HttpServletRequest request) {
        ContentVoExample contentVoExample = new ContentVoExample();
        contentVoExample.setOrderByClause("created desc");
        contentVoExample.createCriteria().andTypeEqualTo(Types.ARTICLE.getType());
        PageInfo<ContentVo> contentsPaginator = contentsService.getArticlesWithpage(contentVoExample, page, limit);
        request.setAttribute("articles", contentsPaginator);
        return "admin/article_list";
    }

    @GetMapping(value = "/publish")
    public String newArticle(HttpServletRequest request) {
        List<MetaVo> categories = metasService.getMetas(Types.CATEGORY.getType());
        request.setAttribute("categories", categories);
        return "admin/article_edit";
    }

    @GetMapping(value = "/{cid}")
    public String editArticle(@PathVariable String cid, HttpServletRequest request) {
        ContentVo contents = contentsService.getContents(cid);
        request.setAttribute("contents", contents);
        List<MetaVo> categories = metasService.getMetas(Types.CATEGORY.getType());
        request.setAttribute("categories", categories);
        request.setAttribute("active", "article");
        return "admin/article_edit";
    }

    @PostMapping(value = "/publish")
    @ResponseBody
    public RestResponseBo publishArticle(ContentVo contents, HttpServletRequest request) {
        UserVo users = this.user(request);
        contents.setAuthorId(users.getUid());
        contents.setType(Types.ARTICLE.getType());
        if (StringUtils.isBlank(contents.getCategories())) {
            contents.setCategories('默认分类');
        }
        String result = contentsService.publish(contents);
        if (!WebConst.SUCCESS_RESULT.equals(result)) {
            return RestResponseBo.fail(result);
        }
        return RestResponseBo.ok();
    }

    @PostMapping(value = "/modify")
    @ResponseBody
    public RestResponseBo modifyArticle(ContentVo contents, HttpServletRequest request) {
        UserVo users = this.user(request);
        contents.setAuthorId(users.getUid());
        contents.setType(Types.ARTICLE.getType());
        String result = contentsService.updateArticle(contents);
        if (!WebConst.SUCCESS_RESULT.equals(result)) {
            return RestResponseBo.fail(result);
        }
        return RestResponseBo.ok();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public RestResponseBo delete(@RequestParam int cid, HttpServletRequest request) {
        String result = contentsService.deleteByCid(cid);
        logService.insertLog(LogActions.DEL_ARTICLE.getAction(), cid + "",
                request.getRemoteAddr(), this.getUid(request));
        if (!WebConst.SUCCESS_RESULT.equals(result)) {
            return RestResponseBo.fail(result);
        }
        return RestResponseBo.ok();
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ArticleControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IContentService contentService;

    @MockBean
    private IMetaService metaService;

    @MockBean
    private ILogService logService;

    @Test
    public void testIndex() throws Exception {
        // 模拟请求,设置参数
        mockMvc.perform(get("/admin/article")
                .param("page", "1")
                .param("limit", "15"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的视图名称
                .andExpect(view().name("admin/article_list"))
                // 验证请求属性
                .andExpect(request().attributeExists("articles"));
    }

    @Test
    public void testNewArticle() throws Exception {
        // 模拟请求
        mockMvc.perform(get("/admin/article/publish"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的视图名称
                .andExpect(view().name("admin/article_edit"))
                // 验证请求属性
                .andExpect(request().attributeExists("categories"));
    }

    @Test
    public void testEditArticle() throws Exception {
        // 模拟请求,设置参数
        mockMvc.perform(get("/admin/article/1"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的视图名称
                .andExpect(view().name("admin/article_edit"))
                // 验证请求属性
                .andExpect(request().attributeExists("contents", "categories", "active"));
    }

    @Test
    public void testPublishArticle() throws Exception {
        // 模拟请求,设置参数
        mockMvc.perform(post("/admin/article/publish")
                .param("title", "test")
                .param("content", "test")
                .param("categories", "test"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的JSON数据
                .andExpect(content().json('{"success":true}'));
    }

    @Test
    public void testModifyArticle() throws Exception {
        // 模拟请求,设置参数
        mockMvc.perform(post("/admin/article/modify")
                .param("cid", "1")
                .param("title", "test")
                .param("content", "test")
                .param("categories", "test"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的JSON数据
                .andExpect(content().json('{"success":true}'));
    }

    @Test
    public void testDelete() throws Exception {
        // 模拟请求,设置参数
        mockMvc.perform(delete("/admin/article/delete")
                .param("cid", "1"))
                // 验证请求是否成功
                .andExpect(status().isOk())
                // 验证返回的JSON数据
                .andExpect(content().json('{"success":true}'));
    }
}
文章管理后台API - 博客系统

原文地址: http://www.cveoy.top/t/topic/nsYY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录