Skip to content

Instantly share code, notes, and snippets.

@ozgurrgul
Created October 29, 2017 11:19
Show Gist options
  • Select an option

  • Save ozgurrgul/50b2f8165b95e7bc4efb3b391448174d to your computer and use it in GitHub Desktop.

Select an option

Save ozgurrgul/50b2f8165b95e7bc4efb3b391448174d to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SimpleCrudApi.Entities;
namespace SimpleCrudApi.Controllers
{
public class SimpleCrudController<T, TKey> : Controller
where T : class
where TKey : IEquatable<TKey>
{
private readonly ISimpleCrudService<T, TKey> _crudService;
public SimpleCrudController(ApplicationDbContext dbContext)
{
_crudService = new SimpleCrudService<T, TKey>(dbContext);
}
[HttpGet]
public async Task<object> List()
{
return await _crudService.List();
}
[HttpGet]
public async Task<object> Get(TKey id)
{
try
{
return await _crudService.Get(id);
}
catch (ApplicationException e)
{
return BadRequest(e.Message);
}
}
[HttpPost]
public async Task<object> Update([FromBody] T model)
{
try
{
await _crudService.Update(model);
}
catch (ApplicationException e)
{
return BadRequest(e.Message);
}
return "SUCCESS";
}
[HttpPost]
public async Task<object> Create([FromBody] T model)
{
try
{
await _crudService.Create(model);
}
catch (ApplicationException e)
{
return BadRequest(e.Message);
}
return "SUCCESS";
}
[HttpDelete]
public async Task<object> Delete(TKey id)
{
try
{
await _crudService.Delete(id);
}
catch (ApplicationException e)
{
return BadRequest(e.Message);
}
return "SUCCESS";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment