package io.controller;
import io.annotation.Login;
import io.annotation.LoginUser;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.entity.UserEntity;
import io.modules.item.dto.BookDTO;
import io.modules.item.service.DictService;
import io.modules.item.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/book")
@CrossOrigin
@Tag(name="书籍")
public class BookController {
    @Autowired
    private BookService bookService;

    @Login
    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<BookDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params, @Parameter(hidden = true) @LoginUser UserEntity user){
        params.put("userId",user.getId().toString());
        PageData<BookDTO> page = bookService.page(params);
        return new Result<PageData<BookDTO>>().ok(page);
    }

    @Login
    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody BookDTO dto, @Parameter(hidden = true) @LoginUser UserEntity user){
        //效验数据
        dto.setUserId(user.getId());
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        bookService.save(dto);
        return new Result();
    }


}
