Skip to content

Instantly share code, notes, and snippets.

@mantaskaveckas
Created December 20, 2023 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mantaskaveckas/ec5a2bbacecdfa75786fecfccf723b6b to your computer and use it in GitHub Desktop.
Save mantaskaveckas/ec5a2bbacecdfa75786fecfccf723b6b to your computer and use it in GitHub Desktop.
EF entity CRUD
// BaseEntity for common properties
public abstract class BaseEntity
{
public int Id { get; set; }
// Add other common properties if needed
}
// Example entities
public class Invoice : BaseEntity
{
// Invoice-specific properties
}
public class User : BaseEntity
{
// User-specific properties
}
public class Account : BaseEntity
{
// Account-specific properties
}
public class Storage : BaseEntity
{
// Storage-specific properties
}
// Generic BaseController
[ApiController]
[Route("api/[controller]")]
public class BaseController<TEntity> : ControllerBase where TEntity : BaseEntity
{
private readonly YourDbContext _context;
public BaseController(YourDbContext context)
{
_context = context;
}
// GET: api/[controller]
[HttpGet]
public async Task<ActionResult<IEnumerable<TEntity>>> Get()
{
return await _context.Set<TEntity>().ToListAsync();
}
// GET: api/[controller]/5
[HttpGet("{id}")]
public async Task<ActionResult<TEntity>> GetById(int id)
{
var entity = await _context.Set<TEntity>().FindAsync(id);
if (entity == null)
{
return NotFound();
}
return entity;
}
// POST: api/[controller]
[HttpPost]
public async Task<ActionResult<TEntity>> Post(TEntity entity)
{
_context.Set<TEntity>().Add(entity);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = entity.Id }, entity);
}
// PUT: api/[controller]/5
[HttpPut("{id}")]
public async Task<IActionResult> Put(int id, TEntity entity)
{
if (id != entity.Id)
{
return BadRequest();
}
_context.Entry(entity).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EntityExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// DELETE: api/[controller]/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var entity = await _context.Set<TEntity>().FindAsync(id);
if (entity == null)
{
return NotFound();
}
_context.Set<TEntity>().Remove(entity);
await _context.SaveChangesAsync();
return NoContent();
}
private bool EntityExists(int id)
{
return _context.Set<TEntity>().Any(e => e.Id == id);
}
}
// Specific controllers for each entity
[ApiController]
[Route("api/invoices")]
public class InvoiceController : BaseController<Invoice>
{
public InvoiceController(YourDbContext context) : base(context)
{
}
}
[ApiController]
[Route("api/users")]
public class UserController : BaseController<User>
{
public UserController(YourDbContext context) : base(context)
{
}
}
[ApiController]
[Route("api/accounts")]
public class AccountController : BaseController<Account>
{
public AccountController(YourDbContext context) : base(context)
{
}
}
[ApiController]
[Route("api/storages")]
public class StorageController : BaseController<Storage>
{
public StorageController(YourDbContext context) : base(context)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment