using Microsoft.AspNetCore.Mvc;
using Strela.Models;
using Strela.Services;

namespace Strela.Controllers;

[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    private readonly ILogger<UserController> _logger;

    public UserController(ILogger<UserController> logger)
    {
        _logger = logger;
    }
    //=========================== GET =================================//
    [HttpGet("GetUsers")]
    public List<User> GetList()
    {
        using (var db = new SQLiteService())
        {
            var usrs = db.Users.ToList();
            return usrs;
        }
    }

    [HttpGet("GetById")]
    public User GetById(int id)
    {
        using (var db = new SQLiteService())
        {
            var usr = db.Users.Where(u => u.Id == id).FirstOrDefault();
            return usr;
        }
    }

    [HttpGet("GetByName")]
    public User GetByName(string name)
    {
        using (var db = new SQLiteService())
        {
            var usr = db.Users.Where(u => u.Name == name).FirstOrDefault();
            return usr;
        }
    }

    //========================= POST ====================================//
    [HttpPost("PostUser")]
    public IActionResult PostUser(User usr)
    {
        using (var db = new SQLiteService())
        {
            db.Users.Add(usr);//.Where(u => u.Name == name).FirstOrDefault();
            db.SaveChanges();
            return Created();
        }
    }
}
