Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ASP.NET Core - Generic web API controller with generic repository pattern (created on, modified on, soft delete) ... view demo https://github.com/matjazmav/generic-api
[Authorize]
[Route("api/[controller]")]
public abstract class ApiController<T> : Controller where T : class, IEntity
{
private IApplicationRepository<T> _repository;
public ApiController(IApplicationRepository<T> repository)
{
_repository = repository;
}
[HttpGet]
[ValidateModel]
public IActionResult Query()
{
return Ok(_repository.Get());
}
[HttpGet("{id}")]
[ValidateModel]
public IActionResult Find(Guid id)
{
var record = _repository.Get(id);
if (record == null)
return NotFound();
return Ok(record);
}
[HttpPost]
[ValidateModel]
public async Task<IActionResult> Create([FromBody] T record)
{
_repository.Create(record);
if (await _repository.SaveAsync() == 0)
return BadRequest();
return CreatedAtAction("Find", new { id = record.Id }, record);
}
[HttpPut("{id}")]
[ValidateModel]
public async Task<IActionResult> Update(Guid id, [FromBody] T record)
{
if (id != record.Id)
return BadRequest();
_repository.Update(record);
if (await _repository.SaveAsync() == 0)
return BadRequest();
return Ok(record);
}
[HttpDelete("{id}")]
[ValidateModel]
public async Task<IActionResult> Delete(Guid id)
{
_repository.Delete(id);
if (await _repository.SaveAsync() == 0)
return BadRequest();
return NoContent();
}
}
public class ApplicationRepository<T> : IApplicationRepository<T> where T : class, IEntity
{
private DbContext _context;
public ApplicationRepository(ApplicationDbContext context)
{
_context = context;
}
public IQueryable<T> Get()
{
return _context.Set<T>().Where(e => !e.IsDeleted);
}
public T Get(Guid id)
{
return Get().SingleOrDefault(e => e.Id == id);
}
public void Create(T record)
{
record.CreatedOn = DateTime.Now;
record.ModifiedOn = record.CreatedOn;
_context.Add(record);
}
public void Update(T record)
{
record.ModifiedOn = DateTime.Now;
_context.Set<T>().Attach(record);
_context.Entry(record).State = EntityState.Modified;
}
public void Delete(Guid id)
{
var record = Get(id);
if (record != null) {
record.ModifiedOn = DateTime.Now;
record.IsDeleted = true;
}
}
public int Save()
{
return _context.SaveChanges();
}
public Task<int> SaveAsync()
{
return _context.SaveChangesAsync();
}
#region Dispose
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(disposing)
{
if(_context != null)
{
_context.Dispose();
_context = null;
}
}
}
#endregion
}
public interface IApplicationRepository<T> : IDisposable
{
IQueryable<T> Get();
T Get(Guid id);
void Create(T record);
void Update(T record);
void Delete(Guid id);
int Save();
Task<int> SaveAsync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment