source: StockMaster/Services/ProductService.cs

main
Last change on this file was dfe03b8, checked in by Ceyda <ceyda.huseini@…>, 3 days ago

Initialize StockMaster project

  • Property mode set to 100644
File size: 3.7 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using System.Threading.Tasks;
4using StockMaster.Data;
5using StockMaster.Models;
6using Microsoft.EntityFrameworkCore;
7
8namespace StockMaster.Services
9{
10 public class ProductService : IProductService
11 {
12 private readonly StockDbContext _context;
13 private readonly IHttpContextAccessor _httpContextAccessor;
14
15 public ProductService(StockDbContext context)
16 {
17 _context = context;
18 }
19
20 public ProductService(StockDbContext context, IHttpContextAccessor httpContextAccessor)
21 {
22 _context = context;
23 _httpContextAccessor = httpContextAccessor;
24 }
25
26 public async Task<List<Product>> GetAllProductsAsync()
27 {
28 return await _context.Products
29 .Include(p => p.Category)
30 .Include(p => p.Supplier)
31 .Where(p => p.IsActive)
32 .ToListAsync();
33 }
34
35 public async Task<Product> GetProductByIdAsync(int id)
36 {
37 return await _context.Products
38 .Include(p => p.Category)
39 .Include(p => p.Supplier)
40 .FirstOrDefaultAsync(p => p.ProductId == id);
41 }
42
43 public async Task<bool> CreateProductAsync(Product product)
44 {
45 try
46 {
47 _context.Products.Add(product);
48 await _context.SaveChangesAsync();
49 return true;
50 }
51 catch
52 {
53 return false;
54 }
55 }
56 public async Task<bool> UpdateProductWithUserAsync(Product product, string username)
57 {
58 using var transaction = await _context.Database.BeginTransactionAsync();
59 try
60 {
61
62 await _context.Database.ExecuteSqlRawAsync($"SELECT set_config('app.current_user', '{username}', false)");
63
64
65 _context.Update(product);
66
67
68 var result = await _context.SaveChangesAsync() > 0;
69
70
71 await transaction.CommitAsync();
72 return result;
73 }
74 catch (Exception)
75 {
76 await transaction.RollbackAsync();
77 throw;
78 }
79 }
80
81 public async Task<bool> DeleteProductAsync(int id)
82 {
83 using var transaction = await _context.Database.BeginTransactionAsync();
84 try
85 {
86 var product = await _context.Products.FindAsync(id);
87
88 if (product != null)
89 {
90 product.IsActive = false;
91 await _context.SaveChangesAsync();
92
93 await transaction.CommitAsync();
94 return true;
95 }
96
97 return false;
98 }
99 catch
100 {
101 await transaction.RollbackAsync();
102 return false;
103 }
104 }
105
106
107 public async Task<List<Product>> GetLowStockProductsAsync()
108 {
109 var query = from p in _context.Products
110 join ws in _context.WarehouseStocks on p.ProductId equals ws.ProductId
111 group ws by new { p.ProductId, p.Name, p.ReorderLevel } into g
112 where g.Sum(x => x.QuantityOnHand) <= g.Key.ReorderLevel
113 select new Product
114 {
115 ProductId = g.Key.ProductId,
116 Name = g.Key.Name,
117 ReorderLevel = g.Key.ReorderLevel
118 };
119
120 return await query.ToListAsync();
121 }
122 }
123}
Note: See TracBrowser for help on using the repository browser.