DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/CrudController.cs

56 lines
1.7 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
2021-08-02 14:45:13 +05:00
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc;
2021-08-10 14:36:35 +05:00
using System.Threading;
2021-08-02 14:45:13 +05:00
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace AsbCloudWebApi.Controllers
{
[ApiController]
2021-08-10 14:36:35 +05:00
public abstract class CrudController<T, TService> : ControllerBase
where T : IId
where TService : ICrudService<T>
2021-08-02 14:45:13 +05:00
{
2021-08-10 14:36:35 +05:00
protected readonly TService service;
2021-08-02 14:45:13 +05:00
2021-08-10 14:36:35 +05:00
public CrudController(TService service)
2021-08-02 14:45:13 +05:00
{
this.service = service;
}
// GET api/<CrudController>/5
[HttpGet("{id}")]
2021-08-10 14:36:35 +05:00
public virtual IActionResult Get(int id, CancellationToken token = default)
2021-08-02 14:45:13 +05:00
{
2021-08-10 14:36:35 +05:00
var result = service.GetAsync(id, token).ConfigureAwait(false);
2021-08-02 14:45:13 +05:00
return Ok(result);
}
// POST api/<CrudController>
[HttpPost]
2021-08-10 14:36:35 +05:00
public virtual IActionResult Insert([FromBody] T value, CancellationToken token = default)
2021-08-02 14:45:13 +05:00
{
2021-08-10 14:36:35 +05:00
var result = service.InsertAsync(value, token).ConfigureAwait(false);
2021-08-02 14:45:13 +05:00
return Ok(result);
}
// PUT api/<CrudController>/5
[HttpPut("{id}")]
2021-08-10 14:36:35 +05:00
public virtual IActionResult Put(int id, [FromBody] T value, CancellationToken token = default)
2021-08-02 14:45:13 +05:00
{
2021-08-10 14:36:35 +05:00
var result = service.UpdateAsync(value, token).ConfigureAwait(false);
2021-08-02 14:45:13 +05:00
return Ok(result);
}
// DELETE api/<CrudController>/5
[HttpDelete("{id}")]
2021-08-10 14:36:35 +05:00
public virtual IActionResult Delete(int id, CancellationToken token = default)
2021-08-02 14:45:13 +05:00
{
2021-08-10 14:36:35 +05:00
var result = service.DeleteAsync(id, token).ConfigureAwait(false);
2021-08-02 14:45:13 +05:00
return Ok(result);
}
}
}