-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
StockApiController.cs
159 lines (139 loc) · 6.02 KB
/
StockApiController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Infrastructure.Web.SmartTable;
using SimplCommerce.Module.Core.Extensions;
using SimplCommerce.Module.Inventory.Areas.Inventory.ViewModels;
using SimplCommerce.Module.Inventory.Models;
using SimplCommerce.Module.Inventory.Services;
namespace SimplCommerce.Module.Inventory.Areas.Inventory.Controllers
{
[Area("Inventory")]
[Authorize(Roles = "admin, vendor")]
[Route("api/stocks")]
public class StockApiController : Controller
{
private readonly IRepository<Stock> _stockRepository;
private readonly IStockService _stockService;
private readonly IStockSubscriptionService _stockSubscriptionService;
private readonly IWorkContext _workContext;
private readonly IRepository<Warehouse> _warehouseRepository;
private readonly IRepository<StockHistory> _stockHistoryRepository;
private readonly IRepository<BackInStockSubscription> _backInStockSubscriptionRepository;
public StockApiController(IRepository<Stock> stockRepository, IStockService stockService, IWorkContext workContext, IRepository<Warehouse> warehouseRepository, IRepository<StockHistory> stockHistoryRepository, IRepository<BackInStockSubscription> backInStockSubscriptionRepository, IStockSubscriptionService stockSubscriptionService)
{
_stockRepository = stockRepository;
_stockService = stockService;
_workContext = workContext;
_warehouseRepository = warehouseRepository;
_stockHistoryRepository = stockHistoryRepository;
_backInStockSubscriptionRepository = backInStockSubscriptionRepository;
_stockSubscriptionService = stockSubscriptionService;
}
[HttpPost("grid")]
public async Task<IActionResult> List(long warehouseId, [FromBody] SmartTableParam param)
{
var currentUser = await _workContext.GetCurrentUser();
var warehouse = _warehouseRepository.Query().FirstOrDefault(x => x.Id == warehouseId);
if(warehouse == null)
{
return NotFound();
}
if (!User.IsInRole("admin") && warehouse.VendorId != currentUser.VendorId)
{
return BadRequest(new { error = "You don't have permission to manage this warehouse" });
}
var query = _stockRepository.Query().Where(x => x.WarehouseId == warehouseId && !x.Product.HasOptions && !x.Product.IsDeleted);
if (param.Search.PredicateObject != null)
{
dynamic search = param.Search.PredicateObject;
if (search.ProductName != null)
{
string productName = search.ProductName;
query = query.Where(x => x.Product.Name.Contains(productName));
}
if (search.ProductSku != null)
{
string productSku = search.productSku;
query = query.Where(x => x.Product.Sku.Contains(productSku));
}
}
var products = query.ToSmartTableResult(
param,
x => new
{
x.Id,
x.ProductId,
ProductName = x.Product.Name,
ProductSku = x.Product.Sku,
x.Quantity,
AdjustedQuantity = 0
});
return Ok(products);
}
[HttpPut]
public async Task<IActionResult> Put(long warehouseId, [FromBody] IList<StockVm> stockVms)
{
var currentUser = await _workContext.GetCurrentUser();
var warehouse = _warehouseRepository.Query().FirstOrDefault(x => x.Id == warehouseId);
if (warehouse == null)
{
return NotFound();
}
if (!User.IsInRole("admin") && warehouse.VendorId != currentUser.VendorId)
{
return BadRequest(new { error = "You don't have permission to manage this warehouse" });
}
foreach(var item in stockVms)
{
if(item.AdjustedQuantity == 0)
{
continue;
}
var stockUpdateRequest = new StockUpdateRequest
{
WarehouseId = warehouseId,
ProductId = item.ProductId,
AdjustedQuantity = item.AdjustedQuantity,
Note = item.Note,
UserId = currentUser.Id
};
await _stockService.UpdateStock(stockUpdateRequest);
}
return Accepted();
}
[HttpGet("history")]
public async Task<IActionResult> GetStockHistory(int warehouseId, int productId)
{
var query = _stockHistoryRepository.Query();
query = query.Where(x => x.WarehouseId == warehouseId && x.ProductId == productId);
var stockHistory = await query.Select(x => new
{
x.Id,
x.Product.Name,
x.CreatedOn,
x.CreatedBy.FullName,
x.AdjustedQuantity,
x.Note
}).ToListAsync();
return Ok(stockHistory);
}
[AllowAnonymous]
[HttpPost("back-in-stock")]
public async Task<IActionResult> BackInStockSubscribe(long productId, string customerEmail)
{
if (await _backInStockSubscriptionRepository.Query()
.Where(o => o.ProductId == productId && o.CustomerEmail == customerEmail)
.AnyAsync())
{
return Conflict();
}
await _stockSubscriptionService.BackInStockSubscribeAsync(productId, customerEmail);
return Ok();
}
}
}