Last active
December 3, 2020 19:31
-
-
Save AbubakarSiddiq/4d834f6d4534a3e941229bdac3572371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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