-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShoppingListFunctionApp.cs
More file actions
140 lines (130 loc) · 5.87 KB
/
ShoppingListFunctionApp.cs
File metadata and controls
140 lines (130 loc) · 5.87 KB
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
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace DotnetGuildTraining;
public class ShoppingListFunctionApp
{
private readonly JsonSerializerOptions _jsonOptions;
private readonly ILogger<ShoppingListFunctionApp> _logger;
public ShoppingListFunctionApp(ILogger<ShoppingListFunctionApp> logger)
{
_logger = logger;
_jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
}
/// <summary>
/// Implement this function to return the total price of the shopping list provided in the request body.
/// </summary>
/// <param name="req">The HTTP request for the Function App call</param>
/// <returns></returns>
[Function("ShoppingListFunctionApp-GetTotalPrice")]
public async Task<IActionResult> GetTotalPriceAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "shopping-list/total-price")] HttpRequest req)
{
try
{
_logger.LogInformation($"Started {nameof(ShoppingListFunctionApp)}");
var shoppingList = await GetShoppingListFromRequest(req);
// TODO: Implement the TotalPrice property in the ShoppingList class and return it here
return new OkObjectResult(shoppingList);
}
catch (Exception e)
{
_logger.LogError($"Error at {nameof(ShoppingListFunctionApp)}: {e}");
if (e is ArgumentException)
{
return new BadRequestObjectResult(e.Message);
}
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
/// <summary>
/// Implement this function to return a specific item from the shopping list provided in the request body, based on the id provided in the route.
/// </summary>
/// <param name="req">The HTTP request for the Function App call</param>
/// <param name="id">The ID of the item to retrieve</param>
/// <returns></returns>
[Function("ShoppingListFunctionApp-GetById")]
public async Task<IActionResult> GetByIdAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "shopping-list/{id:int}")] HttpRequest req, int id)
{
try
{
_logger.LogInformation($"Started {nameof(ShoppingListFunctionApp)}");
var shoppingList = await GetShoppingListFromRequest(req);
// TODO: Implement the GetItemById method in the ShoppingList class and return the item here
return new OkObjectResult(shoppingList);
}
catch (Exception e)
{
_logger.LogError($"Error at {nameof(ShoppingListFunctionApp)}: {e}");
if (e is ArgumentException)
{
return new BadRequestObjectResult(e.Message);
}
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
/// <summary>
/// Implement this function to add a new item to the shopping list provided in the request body. The item to be added will also be provided in the request body.
/// </summary>
/// <param name="req">The HTTP request for the Function App call</param>
/// <returns></returns>
[Function("ShoppingListFunctionApp-InsertItem")]
public async Task<IActionResult> InsertItemAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "shopping-list/insert-item")] HttpRequest req)
{
try
{
_logger.LogInformation($"Started {nameof(ShoppingListFunctionApp)}");
var shoppingList = await GetShoppingListFromRequest(req);
// TODO: Implement the AddItems method in the ShoppingList class and call it here
return new OkObjectResult(shoppingList);
}
catch (Exception e)
{
_logger.LogError($"Error at {nameof(ShoppingListFunctionApp)}: {e}");
if (e is ArgumentException)
{
return new BadRequestObjectResult(e.Message);
}
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
/// <summary>
/// Implement this function to remove an item from the shopping list provided in the request body, based on the id provided in the route.
/// </summary>
/// <param name="req">The HTTP request for the Function App call</param>
/// <param name="id">The ID of the item to remove</param>
/// <returns></returns>
[Function("ShoppingListFunctionApp-DeleteItem")]
public async Task<IActionResult> DeleteItemAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "shopping-list/delete-item/{id:int}")] HttpRequest req, int id)
{
try
{
_logger.LogInformation($"Started {nameof(ShoppingListFunctionApp)}");
var shoppingList = await GetShoppingListFromRequest(req);
// TODO: Implement the RemoveItemById method in the ShoppingList class and call it here
return new OkObjectResult(shoppingList);
}
catch (Exception e)
{
_logger.LogError($"Error at {nameof(ShoppingListFunctionApp)}: {e}");
if (e is ArgumentException)
{
return new BadRequestObjectResult(e.Message);
}
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
private async Task<ShoppingList.ShoppingList> GetShoppingListFromRequest(HttpRequest req)
{
var shoppingList = await JsonSerializer.DeserializeAsync<ShoppingList.ShoppingList>(req.Body, _jsonOptions);
if (shoppingList == null)
{
throw new ArgumentException("Invalid shopping list data");
}
return shoppingList;
}
}