-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask.cs
319 lines (257 loc) · 8.21 KB
/
Task.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Models
public class Product
{
public Guid ProductId { get; set; }
public string Name { get; set; }
public ICollection<Lot> Lots { get; set; }
}
public class Lot
{
public Guid LotId { get; set; }
public Guid ProductId { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public Product Product { get; set; }
public decimal Quantity { get; set; }
}
public class ProductLotQuantity
{
public Guid LotId { get; set; }
public Guid ProductId { get; set; }
public decimal Quantity { get; set; }
}
public class ProductLotTransaction
{
public Guid ProductLotTransactionId { get; set; }
public Guid? LotId { get; set; }
public Guid ProductId { get; set; }
public decimal Quantity { get; set; }
public TransactionType Type { get; set; }
public Lot Lot { get; set; }
}
public enum TransactionType
{
LIFO,
FIFO
}
// Entity context, Fluent API
public class InventoryContext : DbContext
{
public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
public DbSet<Lot> Lots { get; set; }
public DbSet<ProductLotTransaction> ProductLotTransactions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasMany(p => p.Lots)
.WithOne(l => l.Product)
.HasForeignKey(l => l.ProductId);
modelBuilder.Entity<ProductLotTransaction>()
.HasOne(t => t.Lot)
.WithMany()
.HasForeignKey(t => t.LotId);
}
}
// Strategy Pattern
public interface IProductLotSelectionStrategy
{
Lot SelectLot(InventoryContext context, Guid productId, TransactionType type);
}
public class LifoSelectionStrategy : IProductLotSelectionStrategy
{
public Lot SelectLot(InventoryContext context, Guid productId, TransactionType type)
{
return context.Lots.Where(l => l.ProductId == productId)
.OrderBy(l => l.CreatedAt)
.FirstOrDefault();
}
}
public class FifoSelectionStrategy : IProductLotSelectionStrategy
{
public Lot SelectLot(InventoryContext context, Guid productId, TransactionType type)
{
return context.Lots.Where(l => l.ProductId == productId)
.OrderByDescending(l => l.CreatedAt)
.FirstOrDefault();
}
}
// Controllers, CRUD Operations
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
[ApiController]
public class ProductController : ControllerBase
{
private readonly InventoryContext _context;
public ProductController(InventoryContext context)
{
_context = context;
}
[HttpPost("/products")]
public IActionResult CreateProduct(Product product)
{
_context.Products.Add(product);
_context.SaveChanges();
return Ok(product);
}
[HttpGet("/products")]
public IActionResult GetAllProducts()
{
var products = _context.Products.ToList();
return Ok(products);
}
[HttpGet("/products/{id}")]
public IActionResult GetProduct(Guid id)
{
var product = _context.Products.Find(id);
if (product == null)
return NotFound();
return Ok(product);
}
[HttpPut("/products/{id}")]
public IActionResult UpdateProduct(Guid id, Product updatedProduct)
{
var existingProduct = _context.Products.Find(id);
if (existingProduct == null)
return NotFound();
existingProduct.Name = updatedProduct.Name;
_context.SaveChanges();
return Ok(existingProduct);
}
[HttpDelete("/products/{id}")]
public IActionResult DeleteProduct(Guid id)
{
var product = _context.Products.Find(id);
if (product == null)
return NotFound();
_context.Products.Remove(product);
_context.SaveChanges();
return Ok();
}
}
[ApiController]
public class LotController : ControllerBase
{
private readonly InventoryContext _context;
public LotController(InventoryContext context)
{
_context = context;
}
[HttpPost("/lots")]
public IActionResult CreateLot(Lot lot)
{
_context.Lots.Add(lot);
_context.SaveChanges();
return Ok(lot);
}
[HttpGet("/lots")]
public IActionResult GetAllLots()
{
var lots = _context.Lots.ToList();
return Ok(lots);
}
[HttpGet("/lots/{id}")]
public IActionResult GetLot(Guid id)
{
var lot = _context.Lots.Find(id);
if (lot == null)
return NotFound();
return Ok(lot);
}
[HttpPut("/lots/{id}")]
public IActionResult UpdateLot(Guid id, Lot updatedLot)
{
var existingLot = _context.Lots.Find(id);
if (existingLot == null)
return NotFound();
existingLot.Description = updatedLot.Description;
_context.SaveChanges();
return Ok(existingLot);
}
[HttpDelete("/lots/{id}")]
public IActionResult DeleteLot(Guid id)
{
var lot = _context.Lots.Find(id);
if (lot == null)
return NotFound();
_context.Lots.Remove(lot);
_context.SaveChanges();
return Ok();
}
}
[ApiController]
public class ProductLotQuantityController : ControllerBase
{
private readonly InventoryContext _context;
public ProductLotQuantityController(InventoryContext context)
{
_context = context;
}
[HttpGet("/product-lot-quantities")]
public IActionResult GetProductLotQuantities()
{
var productLotQuantities = _context.Lots
.Select(l => new ProductLotQuantity
{
LotId = l.LotId,
ProductId = l.ProductId,
Quantity = l.Quantity
})
.ToList();
return Ok(productLotQuantities);
}
}
[ApiController]
public class ProductLotTransactionController : ControllerBase
{
private readonly InventoryContext _context;
private readonly Dictionary<TransactionType, IProductLotSelectionStrategy> _strategies;
public ProductLotTransactionController(InventoryContext context, Dictionary<TransactionType, IProductLotSelectionStrategy> strategies)
{
_context = context;
_strategies = strategies;
}
[HttpPost("/product-lot-transactions")]
public IActionResult CreateProductLotTransaction(ProductLotTransaction transaction)
{
Lot selectedLot = null;
if (transaction.LotId == null)
{
var strategy = _strategies[transaction.Type];
selectedLot = strategy.SelectLot(_context, transaction.ProductId, transaction.Type);
transaction.LotId = selectedLot.LotId;
}
_context.ProductLotTransactions.Add(transaction);
// Update corresponding ProductLotQuantity
if (selectedLot != null)
{
var productLotQuantity = _context.Lots
.Where(l => l.LotId == selectedLot.LotId && l.ProductId == transaction.ProductId)
.Select(l => new ProductLotQuantity
{
LotId = l.LotId,
ProductId = l.ProductId,
Quantity = l.Quantity
})
.FirstOrDefault();
if (productLotQuantity != null)
productLotQuantity.Quantity += transaction.Quantity;
}
_context.SaveChanges();
return Ok(transaction);
}
[HttpGet("/product-lot-transactions")]
public IActionResult GetProductLotTransactions()
{
var transactions = _context.ProductLotTransactions.ToList();
return Ok(transactions);
}
[HttpPut("/product-lot-transactions")]
[HttpDelete("/product-lot-transactions")]
public IActionResult MethodNotAllowed()
{
return StatusCode(405, "Method Not Allowed");
}
}