diff options
Diffstat (limited to 'code/api/Models')
| -rw-r--r-- | code/api/Models/KnownProblem.cs | 26 | ||||
| -rw-r--r-- | code/api/Models/LoggedInUserModel.cs | 31 |
2 files changed, 57 insertions, 0 deletions
diff --git a/code/api/Models/KnownProblem.cs b/code/api/Models/KnownProblem.cs new file mode 100644 index 0000000..007f79a --- /dev/null +++ b/code/api/Models/KnownProblem.cs @@ -0,0 +1,26 @@ +namespace I2R.Storage.Api.Models; + +public class KnownProblemModel +{ + public KnownProblemModel(string title = default, string subtitle = default, Dictionary<string, string[]> errors = default) { + Title = title; + Subtitle = subtitle; + Errors = errors ?? new Dictionary<string, string[]>(); + } + + public string Title { get; set; } + public string Subtitle { get; set; } + public Dictionary<string, string[]> Errors { get; set; } + public string TraceId { get; set; } + + public void AddError(string field, string errorText) { + if (!Errors.ContainsKey(field)) { + Errors.Add(field, new[] {errorText}); + } else { + var currentErrors = Errors[field]; + var newErrors = currentErrors.Concat(new[] {errorText}); + Errors.Remove(field); + Errors.Add(field, newErrors.ToArray()); + } + } +}
\ No newline at end of file diff --git a/code/api/Models/LoggedInUserModel.cs b/code/api/Models/LoggedInUserModel.cs new file mode 100644 index 0000000..9d9b944 --- /dev/null +++ b/code/api/Models/LoggedInUserModel.cs @@ -0,0 +1,31 @@ +namespace I2R.Storage.Api.Models; + +public class LoggedInUserModel +{ + public LoggedInUserModel() { } + + public LoggedInUserModel(ClaimsPrincipal user) { + Username = user.FindFirstValue(AppClaims.USERNAME); + Id = user.FindFirstValue(AppClaims.USER_ID).AsGuid(); + Role = UserRole.FromString(user.FindFirstValue(AppClaims.USER_ROLE)); + } + + public string Username { get; set; } + public Guid Id { get; set; } + public EUserRole Role { get; set; } + + public class Public + { + public string Id { get; set; } + public string Username { get; set; } + public string Role { get; set; } + } + + public static Public ForThePeople(HttpContext httpContext) { + return new Public() { + Id = httpContext.User.FindFirstValue(AppClaims.USER_ID), + Username = httpContext.User.FindFirstValue(AppClaims.USERNAME), + Role = httpContext.User.FindFirstValue(AppClaims.USER_ROLE) + }; + } +}
\ No newline at end of file |
