using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ViewModels;
using WebAPIServer.Models;

namespace WebAPIServer.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CompanyController : ControllerBase
    {
        
        [HttpPost()]
        [Authorize()]
        public ReturnInfo GetCompanyInfo([FromBody] QueryAll_Or_Single S)
        {
            ReturnInfo returnInfo = new ReturnInfo();

            try
            {
                using (var q=new UtsManageContext())
                {
                    if (S.IsAll)
                    {
                        returnInfo.isok = true;
                        returnInfo.response = q.TblUtsManageCompanies.ToList();
                    }
                    else
                    {
                        returnInfo.isok = true;
                        var a = q.TblUtsManageUsers.SingleOrDefault(A => A.Id == S.ID);
                        if (a != null)
                        {
                            returnInfo.response = a;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                returnInfo.isok = false;
                returnInfo.message = ex.Message;
            }
            return returnInfo;
        }
    }
}
