Skip to content

Commit 416eeff

Browse files
authored
Merge pull request #292 from sebader/dev
Admin edit features
2 parents e4a24c8 + bfac704 commit 416eeff

23 files changed

+704
-46
lines changed

.github/workflows/azure-static-web-apps-white-pond-0976e3603.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ on:
1212
- 'WebsiteBackendFunctions/**'
1313
- '.github/workflows/azure-static-web-apps-white-pond-0976e3603.yml'
1414

15+
env:
16+
DOTNET_VERSION: '8.0.x' # set this to the dotnet version to use
17+
1518
jobs:
1619
staticwebbapp_build_and_deploy:
1720
runs-on: ubuntu-latest
@@ -21,6 +24,11 @@ jobs:
2124
with:
2225
submodules: true
2326

27+
- name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
28+
uses: actions/setup-dotnet@v4
29+
with:
30+
dotnet-version: ${{ env.DOTNET_VERSION }}
31+
2432
- name: Inject commit version
2533
shell: pwsh
2634
run: |

.github/workflows/main_alpinehutscrawler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
uses: actions/checkout@v4
2828

2929
- name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
30-
uses: actions/setup-dotnet@v1
30+
uses: actions/setup-dotnet@v4
3131
with:
3232
dotnet-version: ${{ env.DOTNET_VERSION }}
3333

WebsiteBackendFunctions/Models/Huts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ public class Hut
1717
public DateTime? LastUpdated { get; set; }
1818
public DateTime? Added { get; set; }
1919
public DateTime? Activated { get; set; }
20+
21+
public bool? ManuallyEdited { get; set; }
2022
}
2123
}

WebsiteBackendFunctions/Utils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ namespace WebsiteBackendFunctions;
33
public class Utils
44
{
55
public const string CacheControlHeader = "public, max-age=600";
6-
}
6+
public const string AdminCacheControlHeader = "no-store, no-cache, must-revalidate";
7+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Azure.Functions.Worker;
6+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
7+
using Microsoft.Extensions.Logging;
8+
using WebsiteBackendFunctions.Models;
9+
10+
namespace WebsiteBackendFunctions.WebsiteFunctions.Admin
11+
{
12+
public class DeleteHutFunction
13+
{
14+
private readonly ILogger<DeleteHutFunction> _logger;
15+
16+
public DeleteHutFunction(ILogger<DeleteHutFunction> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[Function(nameof(DeleteHut))]
22+
public IActionResult DeleteHut(
23+
[HttpTrigger(AuthorizationLevel.Function, "delete", Route = "huts/{id}")]
24+
HttpRequest req,
25+
[SqlInput(commandText: "DeleteHutById", commandType: CommandType.StoredProcedure,
26+
parameters: "@HutId={id}", connectionStringSetting: "DatabaseConnectionString")]
27+
IEnumerable<Hut> huts,
28+
int id)
29+
{
30+
_logger.LogInformation("DeleteHut function processed a request for hut id {id}", id);
31+
return new OkObjectResult(new { message = "Hut deleted successfully" });
32+
}
33+
}
34+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Azure.Functions.Worker;
8+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
9+
using Microsoft.Extensions.Logging;
10+
using WebsiteBackendFunctions.Models;
11+
using System.Text.Json;
12+
using System.IO;
13+
using System.Threading.Tasks;
14+
15+
namespace WebsiteBackendFunctions.WebsiteFunctions.Admin
16+
{
17+
public class UpdateHutFunction
18+
{
19+
private readonly ILogger<UpdateHutFunction> _logger;
20+
21+
public UpdateHutFunction(ILogger<UpdateHutFunction> logger)
22+
{
23+
_logger = logger;
24+
}
25+
private static readonly JsonSerializerOptions JsonSerializerOptions =
26+
new() { PropertyNameCaseInsensitive = true };
27+
28+
[Function(nameof(UpdateHut))]
29+
public async Task<HutOutput> UpdateHut(
30+
[HttpTrigger(AuthorizationLevel.Function, "put", Route = "huts/{id}")]
31+
HttpRequest req,
32+
[SqlInput("SELECT * FROM Huts WHERE Id = @Id",
33+
"DatabaseConnectionString",
34+
CommandType.Text,
35+
parameters: "@Id={id}")]
36+
IEnumerable<Hut> existingHuts,
37+
int id)
38+
{
39+
var hut = existingHuts.FirstOrDefault();
40+
if (hut == null)
41+
{
42+
_logger.LogWarning("Hut with id {id} not found", id);
43+
return new HutOutput(new NotFoundObjectResult(new { message = "Hut not found" }));
44+
}
45+
46+
Hut? requestBody;
47+
48+
try {
49+
var requestBodyString = await new StreamReader(req.Body).ReadToEndAsync();
50+
requestBody = JsonSerializer.Deserialize<Hut>(requestBodyString, JsonSerializerOptions);
51+
52+
if (requestBody == null)
53+
{
54+
_logger.LogWarning("Request body could not be deserialized");
55+
return new HutOutput(new BadRequestObjectResult(new { message = "Invalid request body format" }));
56+
}
57+
}
58+
catch (JsonException ex)
59+
{
60+
_logger.LogError(ex, "Error deserializing request body");
61+
return new HutOutput(new BadRequestObjectResult(new { message = "Error parsing request data: " + ex.Message }));
62+
}
63+
64+
if (requestBody.Id != id)
65+
{
66+
return new HutOutput(new BadRequestObjectResult(new { message = "Invalid request body or ID mismatch" }));
67+
}
68+
69+
try
70+
{
71+
requestBody.Added = hut.Added;
72+
requestBody.Activated = hut.Activated;
73+
requestBody.LastUpdated = DateTime.UtcNow;
74+
requestBody.ManuallyEdited = true;
75+
_logger.LogInformation("Updating hut with id {id}", id);
76+
77+
return new HutOutput(new OkObjectResult(requestBody), requestBody); // Pass requestBody as UpdatedHut to trigger SQL output binding
78+
}
79+
catch (Exception ex)
80+
{
81+
_logger.LogError(ex, "Error updating hut with id {id}", id);
82+
return new HutOutput(new StatusCodeResult(StatusCodes.Status500InternalServerError));
83+
}
84+
}
85+
}
86+
87+
public class HutOutput
88+
{
89+
[SqlOutput("dbo.Huts", connectionStringSetting: "DatabaseConnectionString")]
90+
public Hut? UpdatedHut { get; set; }
91+
92+
[HttpResult]
93+
public IActionResult HttpResponse { get; set; }
94+
95+
public HutOutput(IActionResult response, Hut? updatedHut = null)
96+
{
97+
HttpResponse = response;
98+
UpdatedHut = updatedHut;
99+
}
100+
}
101+
}

WebsiteBackendFunctions/WebsiteFunctions/GetAllBedCategoriesFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IActionResult GetAllBedCategories(
3030
IEnumerable<BedCategoryViewModel> result)
3131
{
3232
_logger.LogInformation("Retrieved {count} bed categories from database", result?.Count());
33-
33+
req.HttpContext.Response.Headers.Append("cache-control", Utils.CacheControlHeader);
3434
return new OkObjectResult(result);
3535
}
3636
}

WebsiteBackendFunctions/WebsiteFunctions/GetAllHutsFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public IActionResult GetAllHuts(
2929
IEnumerable<Hut> result)
3030
{
3131
_logger.LogInformation("Retrieved {count} huts from database", result?.Count());
32-
req.HttpContext.Response.Headers.Append("cache-control", Utils.CacheControlHeader);
3332
return new OkObjectResult(result);
3433
}
3534
}

WebsiteBackendFunctions/WebsiteFunctions/GetHutByIdFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public IActionResult GetHut(
3838
}
3939

4040
_logger.LogInformation("Retrieved hut {hutName} for hut id {hutId}", hut.Name, hutId);
41-
req.HttpContext.Response.Headers.Append("cache-control", Utils.CacheControlHeader);
4241
return new OkObjectResult(hut);
4342
}
4443
}

global.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.407"
4+
}
5+
}

0 commit comments

Comments
 (0)