Untitled
unknown
plain_text
3 years ago
3.4 kB
7
Indexable
using System.Linq.Expressions;
using System.Reflection.Metadata.Ecma335;
using System.Xml.Linq;
using Company.Common.DTOs;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Company.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CompanyController : ControllerBase
{
private readonly IDbService _db;
public CompanyController(IDbService db) => _db = db;
// GET: api/<DepartmentsController>
[HttpGet]
public async Task<IResult> Get() =>
Results.Ok(await _db.GetAsync<Business, BusinessDTO>());
// GET api/<DepartmentsController>/5
[HttpGet("{id}")]
public async Task<IResult> Get(int id)
{
var result = (await _db.SingleAsync<Business, BusinessDTO>(e => e.Id.Equals(id)));
if (result is null) return Results.NotFound();
return Results.Ok(result);
}
//public async Task<IResult> Get()
//{var company = await _DB.GetAsync<Business, BusinessDTO>();
//Return Results.Ok(Business);
//// POST api/<DepartmentsController>
[HttpPost]
public async Task<IResult> Post([FromBody]BusinessDTO dto)
{
try
{
var entity = await _db.AddAsync<Business, BusinessDTO>(dto);
if (await _db.SaveChangesAsync())
{
var node = typeof(Business).Name.ToLower();
return Results.Created($"/{node}s/{entity.Id}", entity);
}
return Results.Created("API/Company", entity);
}
catch (Exception ex)
{
return Results.BadRequest($"Couldn't add the {typeof(Business).Name} entity.\n{ex}.");
}
}
// PUT api/<DepartmentsController>/5
[HttpPut("{id}")]
public async Task<IResult> Put(int id, [FromBody] BusinessDTO dto)
{
try
{
if (!await _db.AnyAsync<Business>(e => e.Id.Equals(id)))
{
return Results.NotFound();
}
_db.Update<Business, BusinessDTO>(id, dto);
if (await _db.SaveChangesAsync()) return Results.NoContent();
}
catch (Exception ex)
{
return Results.BadRequest($"Couldn't update the {typeof(Business).Name} entity.\n{ex}.");
}
return Results.BadRequest();
}
// DELETE api/<DepartmentsController>/5
[HttpDelete("{id}")]
public async Task<IResult> Delete<TEntity>(int id) where TEntity : class, IEntity
{
try
{
if (!await _db.DeleteAsync<Business>(id)) return Results.NotFound();
if (await _db.SaveChangesAsync()) return Results.NoContent();
}
catch (Exception ex)
{
return Results.BadRequest($"Couldn't delete the {typeof(Business).Name} entity.\n{ex}.");
}
return Results.BadRequest();
}
}
}Editor is loading...