Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AbubakarSiddiq/4d834f6d4534a3e941229bdac3572371 to your computer and use it in GitHub Desktop.
Save AbubakarSiddiq/4d834f6d4534a3e941229bdac3572371 to your computer and use it in GitHub Desktop.
using AspNetCore5Microservices.DataContexts;
using AspNetCore5Microservices.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AspNetCore5Microservices.Repository
{
public class ProductRepository : IProductRepository
{
private readonly ProductContext _dbContext;
public ProductRepository(ProductContext dbContext)
{
_dbContext = dbContext;
}
public void DeleteProduct(int productId)
{
var product = _dbContext.Products.Find(productId);
_dbContext.Products.Remove(product);
_dbContext.SaveChanges();
}
public Product GetProductByID(int productId)
{
return _dbContext.Products.Find(productId);
}
public IEnumerable<Product> GetProducts()
{
try
{
return _dbContext.Products.ToList();
}
catch(Exception ex)
{
throw ex;
}
}
public void CreateProduct(Product product)
{
_dbContext.Add(product);
_dbContext.SaveChanges();
}
public void EditProduct(Product product)
{
_dbContext.Entry(product).State = EntityState.Modified;
_dbContext.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment