package com.remy.user.controller;

import com.remy.common.helper.BeanHelper;
import com.remy.user.entity.User;
import com.remy.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("users")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("{userId}")
    public User findById(@PathVariable("userId") Long userId) {
        log.info("query user by userId: {}", userId);
        return userService.findByID(userId);
    }

    @GetMapping("queryByIds")
    public List<User> findById(@RequestParam("userIds") List<Long> userIds) {
        log.info("query user by userIds: {}", BeanHelper.toJSON(userIds));
        return userService.findAllByIds(userIds);
    }

    @GetMapping("query")
    public User query(@RequestParam("username") String username) {
        return userService.findByUsername(username);
    }
}
