package com.nokia.controller;

import com.nokia.common.R;
import com.nokia.pojo.User;
import com.nokia.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("rest/api/user")
public class UserController {
    private final UserService userServiceImpl;

    @Autowired
    public UserController(UserService userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    @PostMapping("list")
    @ResponseBody
    public R list() {
        List<User> userEntities = userServiceImpl.list();
        return R.ok().data(userEntities);
    }

    @PostMapping("add")
    @ResponseBody
    public R add(@RequestBody User user) {
        if (user.getLoginName() == null || user.getLoginName().equals("")) {
            return R.error().message("LoginName不能为空");
        }
        String msg = userServiceImpl.add(user);
        if (msg.equals("ok")) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message(msg);
        }
    }
}
