aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/EndpointBase.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
committerivarlovlie <git@ivarlovlie.no>2023-02-25 13:15:44 +0100
commit900bb5e845c3ad44defbd427cae3d44a4a43321f (patch)
treedf3d96a93771884add571e82336c29fc3d9c7a1c /code/api/src/Endpoints/EndpointBase.cs
downloadgreatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.tar.xz
greatoffice-900bb5e845c3ad44defbd427cae3d44a4a43321f.zip
feat: Initial commit
Diffstat (limited to 'code/api/src/Endpoints/EndpointBase.cs')
-rw-r--r--code/api/src/Endpoints/EndpointBase.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/code/api/src/Endpoints/EndpointBase.cs b/code/api/src/Endpoints/EndpointBase.cs
new file mode 100644
index 0000000..105fbdf
--- /dev/null
+++ b/code/api/src/Endpoints/EndpointBase.cs
@@ -0,0 +1,54 @@
+using ILogger = Microsoft.Extensions.Logging.ILogger;
+
+namespace IOL.GreatOffice.Api.Endpoints;
+
+[ApiController]
+public class EndpointBase : ControllerBase
+{
+ /// <summary>
+ /// User data for the currently logged on user.
+ /// </summary>
+ protected LoggedInUserModel LoggedInUser => new() {
+ Username = User.FindFirstValue(AppClaims.NAME),
+ Id = User.FindFirstValue(AppClaims.USER_ID).AsGuid(),
+ };
+
+ [NonAction]
+ protected ActionResult KnownProblem(string title = default, string subtitle = default, Dictionary<string, string[]> errors = default) {
+ HttpContext.Response.Headers.Add(AppHeaders.IS_KNOWN_PROBLEM, "1");
+ return BadRequest(new KnownProblemModel {
+ Title = title,
+ Subtitle = subtitle,
+ Errors = errors,
+ TraceId = HttpContext.TraceIdentifier
+ });
+ }
+
+ [NonAction]
+ protected ActionResult KnownProblem(KnownProblemModel problem) {
+ HttpContext.Response.Headers.Add(AppHeaders.IS_KNOWN_PROBLEM, "1");
+ problem.TraceId = HttpContext.TraceIdentifier;
+ return BadRequest(problem);
+ }
+
+ [NonAction]
+ protected RequestTimeZoneInfo GetRequestTimeZone(ILogger logger = default) {
+ Request.Headers.TryGetValue(AppHeaders.BROWSER_TIME_ZONE, out var timeZoneHeader);
+ var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneHeader.ToString().HasValue() ? timeZoneHeader.ToString() : "UTC");
+ var offset = tz.BaseUtcOffset.Hours;
+
+ // This is fine as long as the client is not connecting from Australia: Lord Howe Island,
+ // according to https://en.wikipedia.org/wiki/Daylight_saving_time_by_country
+ if (tz.IsDaylightSavingTime(AppDateTime.UtcNow)) {
+ offset++;
+ }
+
+ logger?.LogInformation("Request time zone (" + tz.Id + ") offset is: " + offset + " hours");
+
+ return new RequestTimeZoneInfo() {
+ TimeZoneInfo = tz,
+ Offset = offset,
+ LocalDateTime = TimeZoneInfo.ConvertTimeFromUtc(AppDateTime.UtcNow, tz)
+ };
+ }
+} \ No newline at end of file