-
-
Save mjgartendev/c508b04854bf12c975db83a4d2d0da9c to your computer and use it in GitHub Desktop.
ASP.NET Core - Generic web API controller with generic repository pattern (created on, modified on, soft delete)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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