53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using Gremlin_BlazorServer.Data.DBClasses;
|
|
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Diagnostics;
|
|
|
|
namespace Gremlin_BlazorServer.Services
|
|
{
|
|
public class ProductService
|
|
{
|
|
private readonly GremlinDb gremlinDb;
|
|
public ProductService(GremlinDb gC) => gremlinDb = gC;
|
|
|
|
public void ConfigureServices(IServiceCollection services) => services.AddDbContext<GremlinDb>(ServiceLifetime.Scoped);
|
|
|
|
public async Task<List<Product>> GetAllProductsAsync() => await gremlinDb.Products.ToListAsync();
|
|
|
|
public async Task<bool> InsertProductAsync(Product product)
|
|
{
|
|
try
|
|
{
|
|
_ = await gremlinDb.Products.AddAsync(product);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.WriteLine(exception.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<Product> GetProductAsync(string productNumber)
|
|
{
|
|
Product? product = await gremlinDb.Products.FirstOrDefaultAsync(product => product.ProductNumber.Equals(productNumber));
|
|
return product ?? new();
|
|
}
|
|
|
|
public async Task<bool> UpdateProductAsync(Product product)
|
|
{
|
|
_ = gremlinDb.Products.Update(product);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteProductAsync(Product product)
|
|
{
|
|
_ = gremlinDb.Remove(product);
|
|
_ = await gremlinDb.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
}
|
|
}
|