Documente Academic
Documente Profesional
Documente Cultură
IQueryable<T> FindAll();
En Shop.Infraestructure
Creamos la carpeta Data y dentro de esta creamos las carpetas Contexts y Repositories
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Scaffold-DbContext "Server=localhost;Database=Shop;User
ID=sa;Password=1234;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir
Data\Contexts
La clase Productos pasar a la carpeta Entities en el proyecto Shop.Core y borrar esa clase de la carpeta
Data/Contexts
En Shop.Core
En el proyecto Shop.Infraestructure
}
}
result.Marca = product.Marca;
result.Nombre = product.Nombre;
result.PrecioUnitario = product.PrecioUnitario;
result.Stock = product.Stock;
result.Tipo = product.Tipo;
result.Unidades = product.Unidades;
this._productRepository.Update(result);
await _unitOfWork.CompleteAsync();
this._productRepository.Delete(result);
await _unitOfWork.CompleteAsync();
En el Proyecto Shop.Api
{
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-LF8HS6S\\SQLEXPRESS;Database=Shop;User
ID=sa;Password=1234;Trusted_Connection=True;"
},
Al inicio:
Al final:
//Repositorios
services.AddScoped<IProductRepository, ProductRepository>();
//Servicios
services.AddScoped<IProductService, ProductService>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
[HttpGet]
public async Task<IEnumerable<Productos>> ListAsync()
{
var result = await _productService.FindAllAsync();
return result;
}
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] Productos product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!result.IsSuccess())
{
return BadRequest(result as ErrorCRUDResponse);
}
[HttpPut("{id}")]
public async Task<IActionResult> PutAsync(int id, [FromBody] Productos product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!result.IsSuccess())
{
var errorResponse = result as ErrorCRUDResponse;
if (errorResponse.status == 404)
{
return NotFound();
}
else
{
return BadRequest(errorResponse);
}
}
[HttpGet("{id}")]
public ActionResult<Productos> Get(int id)
{
var result = _productService.FindById(id);
if (result == null)
{
return NotFound();
}
return result;
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteAsync(int id)
{
var result = await _productService.DeleteAsync(id);
if (!result.IsSuccess())
{
return NotFound();
}
return NoContent();
}
}