PageRenderTime 82ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/MvcApplicationAPI/Repositories/ProductRepository.cs

https://github.com/henrikjakobsen/WebAPI
C# | 60 lines | 52 code | 8 blank | 0 comment | 0 complexity | 279fb51047e8d532d08df3dfb19b4e0a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using MvcApplicationAPI.Areas;
  7. using MvcApplicationAPI.Models;
  8. namespace MvcApplicationAPI.Repositories
  9. {
  10. public interface IProductRepository
  11. {
  12. IEnumerable<Product> GetAll();
  13. Product Get(int id);
  14. Product Add(Product item);
  15. void Remove(int id);
  16. bool Update(Product item);
  17. }
  18. public class ProductRepository : IProductRepository
  19. {
  20. private readonly ShopContext _db;
  21. public ProductRepository()
  22. {
  23. _db = new ShopContext();
  24. }
  25. public IEnumerable<Product> GetAll()
  26. {
  27. return _db.Products;
  28. }
  29. public Product Get(int id)
  30. {
  31. return _db.Products.Find(id);
  32. }
  33. public Product Add(Product item)
  34. {
  35. _db.Products.Add(item);
  36. _db.SaveChanges();
  37. return item;
  38. }
  39. public void Remove(int id)
  40. {
  41. Product product = _db.Products.Find(id);
  42. _db.Products.Remove(product);
  43. _db.SaveChanges();
  44. }
  45. public bool Update(Product item)
  46. {
  47. _db.Entry(item).State = EntityState.Modified;
  48. _db.SaveChanges();
  49. return true;
  50. }
  51. }
  52. }