package com.hc.gpweb.controller.api;

import com.hc.gpbusiness.model.WebUser;
import com.hc.gpbusiness.service.WebUserService;
import com.hc.gpcore.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 用户表(User)表控制层
 *
 * @author java
 * @since 2020-07-29 16:23:12
 */

@RestController
@RequestMapping("/api/user")
public class UserController {

    /**
     * 服务对象
     */
    private final WebUserService userService;

    public UserController(WebUserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 User", response = Result.class)
    public Result selectUser(@PathVariable("id") Integer id) {
        return Result.success(this.userService.queryById(id));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增 User", response = Result.class)
    public Result addUser(@Valid @RequestBody WebUser user) {
        return Result.success(userService.insert(user));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 User", response = Result.class)
    public Result updateUser(@RequestBody WebUser user) {
        return Result.success(userService.update(user));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 User", response = Result.class)
    public Result deleteUser(@PathVariable("id") Integer id) {
        return Result.success(userService.deleteById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取 User列表(分页)", response = Result.class)
    public Result listUser(@RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(userService.queryAllByLimit(page, size));
    }


}