generated from ThalesGroup/template-project
/
StoreApi.cs
284 lines (270 loc) · 11.7 KB
/
StoreApi.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
/*
* Simple Store API
*
* A simple store API
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Store.Attributes;
using Store.Middleware.WebApi.Middleware;
using Store.Models;
using Store.Services;
namespace Store.Controllers
{
using System;
/// <summary>
/// Store API Controller
/// </summary>
[ApiController]
public sealed class StoreApiController : ControllerBase
{
private readonly IInventoryDatabase _inventoryDatabase;
private const string JwtClaimMustContainManager = JwtMiddleware.JwtClaimMustContainManagerPolicy;
private const string JwtClaimMustContainManagerOrEmployee = JwtMiddleware.JwtClaimMustContainManagerOrEmployeePolicy;
/// <summary>
/// Constructor
/// </summary>
/// <param name="inventoryDatabase"></param>
public StoreApiController(IInventoryDatabase inventoryDatabase)
{
this._inventoryDatabase = inventoryDatabase;
}
/// <summary>
/// Get a list of shops
/// </summary>
/// <remarks>returns a list of shops</remarks>
/// <response code="200">Successfully returned a list of shops</response>
[HttpGet]
[Route("/shop")]
[ValidateModelState]
[Produces("application/json")]
[SwaggerOperation("ShopGet")]
[Authorize]
[SwaggerResponse(statusCode: 200, type: typeof(List<ShopInfo>), description: "Successfully returned a list of shops")]
public IActionResult ShopGet()
{
var info = this._inventoryDatabase.GetShops().Select(shop => new ShopInfo {Name = shop.Name, Id = shop.Id, Location = shop.Location}).ToList();
return StatusCode(200, info);
}
/// <summary>
/// Get shop information
/// </summary>
/// <remarks>gets information for a given shop</remarks>
/// <param name="shopId">The identifier of the shop</param>
/// <response code="200">Successfully returned a shop info</response>
/// <response code="404">Shop not found</response>
[HttpGet]
[Route("/shop/{shopId}")]
[ValidateModelState]
[Produces("application/json")]
[SwaggerOperation("ShopShopIdGet")]
[Authorize]
[SwaggerResponse(statusCode: 200, type: typeof(Shop), description: "Successfully returned a shop info")]
public IActionResult ShopShopIdGet([FromRoute][Required]string shopId)
{
try
{
var shop = this._inventoryDatabase.GetShop(shopId);
return StatusCode(200, new Shop()
{
Name = shop.Name,
Id = shop.Id,
Location = shop.Location,
Description = shop.Description
});
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Gets the stock information
/// </summary>
/// <remarks>gets the stock information for a given shop</remarks>
/// <param name="shopId">The identifier of the shop</param>
/// <response code="200">Successfully returned a list of stock items in a shop</response>
/// <response code="404">Shop not found</response>
[HttpGet]
[Route("/shop/{shopId}/stock")]
[ValidateModelState]
[Produces("application/json")]
[Authorize]
[SwaggerOperation("ShopShopIdStockGet")]
[SwaggerResponse(statusCode: 200, type: typeof(List<StockItem>), description: "Successfully returned a list of stock items in a shop")]
public IActionResult ShopShopIdStockGet([FromRoute][Required]string shopId)
{
try
{
var products = this._inventoryDatabase.GetInventory(shopId, LocationType.Store);
return StatusCode(200, products.Select(item=> new StockItem {Id = item.Product.Id, Count = item.Listing.Count, Name = item.Product.Name, Description = item.Product.Description}));
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Gets a list of products available
/// </summary>
/// <remarks>gets a list of products available at a store</remarks>
/// <param name="shopId">The identifier of the shop</param>
/// <response code="200">Successfully returned a list of products available at the store</response>
/// <response code="404">Shop not found</response>
[HttpGet]
[Route("/shop/{shopId}/products")]
[ValidateModelState]
[Produces("application/json")]
[Authorize]
[SwaggerOperation("ShopShopIdProductsGet")]
[SwaggerResponse(statusCode: 200, type: typeof(List<Product>), description: "Successfully returned a list of products available at the store")]
public IActionResult ShopShopIdProductsGet([FromRoute][Required]string shopId)
{
try
{
var products = this._inventoryDatabase.GetProducts(shopId, LocationType.Store);
return StatusCode(200, products.Select(item=> new Product {Id = item.Id, Name = item.Name, Description = item.Description}));
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Get a list of warehouses
/// </summary>
/// <remarks>returns a list of warehouses</remarks>
/// <response code="200">Successfully returned a list of warehouses</response>
[HttpGet]
[Route("/warehouse")]
[ValidateModelState]
[Produces("application/json")]
[Authorize(Policy = JwtClaimMustContainManagerOrEmployee)]
[SwaggerOperation("WarehouseGet")]
[SwaggerResponse(statusCode: 200, type: typeof(List<WarehouseInfo>), description: "Successfully returned a list of warehouses")]
public IActionResult WarehouseGet()
{
var info = this._inventoryDatabase.GetWarehouses().Select(shop => new WarehouseInfo {Name = shop.Name, Id = shop.Id, Location = shop.Location}).ToList();
return StatusCode(200, info);
}
/// <summary>
/// Get warehouse information
/// </summary>
/// <remarks>gets information for a given warehouse</remarks>
/// <param name="warehouseId">The identifier of the warehouse</param>
/// <response code="200">Successfully returned a warehouse info</response>
/// <response code="404">Warehouse not found</response>
[HttpGet]
[Route("/warehouse/{warehouseId}")]
[ValidateModelState]
[Produces("application/json")]
[Authorize(Policy = JwtClaimMustContainManagerOrEmployee)]
[SwaggerOperation("WarehouseWarehouseIdGet")]
[SwaggerResponse(statusCode: 200, type: typeof(Warehouse), description: "Successfully returned a warehouse info")]
public IActionResult WarehouseWarehouseIdGet([FromRoute][Required]string warehouseId)
{
try
{
var warehouse = this._inventoryDatabase.GetWarehouse(warehouseId);
return StatusCode(200, new Warehouse()
{
Name = warehouse.Name,
Id = warehouse.Id,
Location = warehouse.Location,
Description = warehouse.Description
});
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Gets the stock information
/// </summary>
/// <remarks>gets the stock information for a given warehouse</remarks>
/// <param name="warehouseId">The identifier of the warehouse</param>
/// <response code="200">Successfully returned a list of stock items in a warehouse</response>
/// <response code="404">Warehouse not found</response>
[HttpGet]
[Route("/warehouse/{warehouseId}/stock")]
[ValidateModelState]
[Produces("application/json")]
[Authorize(Policy = JwtClaimMustContainManagerOrEmployee)]
[SwaggerOperation("WarehouseWarehouseIdStockGet")]
[SwaggerResponse(statusCode: 200, type: typeof(List<StockItem>), description: "Successfully returned a list of stock items in a warehouse")]
public IActionResult WarehouseWarehouseIdStockGet([FromRoute][Required]string warehouseId)
{
try
{
var products = this._inventoryDatabase.GetInventory(warehouseId, LocationType.Warehouse);
return StatusCode(200, products.Select(item=> new StockItem {Id = item.Product.Id, Count = item.Listing.Count, Name = item.Product.Name, Description = item.Product.Description}));
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Move stock from warehouse to shop
/// </summary>
/// <remarks>move in stock items from warehouse to shop</remarks>
/// <param name="warehouseId">The identifier of the warehouse</param>
/// <param name="stockId">The identifier of the stock item</param>
/// <param name="shopId">The identifier of the shop</param>
/// <param name="body">object with the number of items to move</param>
/// <response code="200">Successfully moved items to shop</response>
/// <response code="400">Invalid request</response>
/// <response code="404">Unable to find Shop, Warehouse or Stock Item</response>
/// <response code="406">Unable to move items</response>
[HttpPost]
[Route("/warehouse/{warehouseId}/stock/{stockId}/move/{shopId}")]
[ValidateModelState]
[Consumes("application/json")]
[Authorize(Policy = JwtClaimMustContainManager)]
[SwaggerOperation("WarehouseWarehouseIdStockStockIdMoveShopIdPost")]
public IActionResult WarehouseWarehouseIdStockStockIdMoveShopIdPost([FromRoute][Required]string warehouseId, [FromRoute][Required]string stockId, [FromRoute][Required]string shopId, [FromBody]MoveRequest body)
{
if (body == null || !body.Count.HasValue || body.Count.Value < 1)
{
return StatusCode(400);
}
try
{
return StatusCode(this._inventoryDatabase.MoveStock(warehouseId, shopId, stockId, body.Count.Value) ? 200 : 406);
}
catch (InventoryDatabase.NotFoundException e)
{
Console.WriteLine(e.Message);
return StatusCode(404);
}
}
/// <summary>
/// Reset Stock
/// </summary>
/// <remarks>resets stock levels to default.</remarks>
/// <response code="200">Successfully reset the inventory</response>
[HttpPost]
[Route("/reset")]
[ValidateModelState]
[SwaggerOperation("Reset")]
public IActionResult Reset()
{
this._inventoryDatabase.Reset();
return StatusCode(200);
}
}
}