This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddInventory.cs
58 lines (50 loc) · 1.95 KB
/
AddInventory.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
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos;
namespace InventoryFunction
{
public class AddInventory
{
private readonly InventoryService _inventoryService;
public AddInventory(InventoryService inventoryService)
{
_inventoryService = inventoryService;
}
[FunctionName("AddInventory")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "inventories/{companyName}")] HttpRequest req,
[CosmosDB(
databaseName: "IMS",
containerName: "IMS",
Connection = "CosmosDBConnection")] CosmosClient client,
ILogger log,
string companyName)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic? data = JsonConvert.DeserializeObject(requestBody);
if (data == null || data!.inventoryName == null)
{
return new BadRequestObjectResult("Please provide 'inventoryName' in the request body.");
}
Inventory newInventory = new()
{
id = data!.inventoryName,
name = data!.inventoryName,
companyName = companyName
};
var result = await _inventoryService.AddInventoryAsync(newInventory, client, log);
string responseMessage = $"Added inventory \"{newInventory.name}\" to company \"{companyName}\" ";
log.LogInformation(responseMessage);
if (result) {
return new OkObjectResult(responseMessage);
}
return new BadRequestObjectResult("Failed to add new inventory.");
}
}
}