using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.BusinessLogicContracts;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace CaseAccountingRestApi.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ContractController : Controller
	{
		private readonly IContractLogic _logic;
		
		public ContractController(IContractLogic logic)
		{
			_logic = logic;
		}

		[HttpGet]
		public ContractViewModel? Get(int id)
		{
			try
			{
				return _logic.ReadElement(new ContractSearchModel { Id = id });
			}
			catch (Exception)
			{
				throw;
			}
		}

		[HttpGet]
		public List<ContractViewModel>? GetAllByUser(int userId)
		{
			try
			{
				return _logic.ReadList(new ContractSearchModel { UserId = userId });
			}
			catch (Exception)
			{
				throw;
			}
		}

		[HttpPost]
		public void Create(ContractBindingModel model)
		{
			try
			{
				_logic.Create(model);
			}
			catch (Exception)
			{
				throw;
			}
		}

		[HttpPost]
		public void Update(ContractBindingModel model)
		{
			try
			{
				_logic.Update(model);
			}
			catch (Exception)
			{
				throw;
			}
		}

		[HttpPost]
		public void Delete(ContractBindingModel model)
		{
			try
			{
				_logic.Delete(new() { Id = model.Id });
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
