package cn.reghao.tnb.search.app.blog.controller;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.search.app.blog.model.CategoryType;
import cn.reghao.tnb.search.app.blog.model.vo.CategoryCount;
import cn.reghao.tnb.search.app.blog.service.CategoryService;
import cn.reghao.jutil.jdk.web.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-04-10 16:56:10
 */
@Tag(name = "博客文章分类接口")
@RestController
@RequestMapping("/api/blog/v2")
public class AdminCategoryController {
    private final CategoryService categoryService;

    public AdminCategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Operation(summary = "文章分类列表页面", description = "N")
    @GetMapping("/category/list")
    public String categoryList(@RequestParam("type") String type) {
        int type1 = 0;
        if ("category".equals(type)) {
            type1 = CategoryType.Category.getValue();
        } else if ("tag".equals(type)) {
            type1 = CategoryType.Tag.getValue();
        } else {
            return WebResult.failWithMsg("type invalide");
        }

        List<CategoryCount> list = categoryService.findCategoryCountByPage(type1, true);
        return WebResult.success(list);
    }

    @Operation(summary = "添加文章分类接口", description = "N")
    @PostMapping(value = "/category/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCategory(String name) {
        Result result = categoryService.add(name);
        return WebResult.result(result);
    }

    @Operation(summary = "删除文章分类接口", description = "N")
    @PostMapping(value = "/category/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String delete(@RequestParam("id") int id) {
        Result result = categoryService.deleteCategory(id);
        return WebResult.result(result);
    }
}
