using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AsbCloudWebApi.Controllers
{
    [ApiController]
    public abstract class CrudController<T, TService> : ControllerBase
        where T : IId
        where TService: ICrudService<T>
    {
        protected readonly TService service;

        public CrudController(TService service)
        {
            this.service = service;
        }

        // GET api/<CrudController>/5
        [HttpGet("{id}")]

        public virtual IActionResult Get(int id, CancellationToken token = default)
        {
            var result = service.GetAsync(id, token).ConfigureAwait(false);
            return Ok(result);
        }

        // POST api/<CrudController>
        [HttpPost]
        public virtual IActionResult Insert([FromBody] T value, CancellationToken token = default)
        {
            var result = service.InsertAsync(value, token).ConfigureAwait(false);
            return Ok(result);
        }

        // PUT api/<CrudController>/5
        [HttpPut("{id}")]
        public virtual IActionResult Put(int id, [FromBody] T value, CancellationToken token = default)
        {
            var result = service.UpdateAsync(value, token).ConfigureAwait(false);
            return Ok(result);
        }

        // DELETE api/<CrudController>/5
        [HttpDelete("{id}")]
        public virtual IActionResult Delete(int id, CancellationToken token = default)
        {
            var result = service.DeleteAsync(id, token).ConfigureAwait(false);
            return Ok(result);
        }
    }
}