aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/Internal/Account
diff options
context:
space:
mode:
Diffstat (limited to 'code/api/src/Endpoints/Internal/Account')
-rw-r--r--code/api/src/Endpoints/Internal/Account/CreateAccountRoute.cs6
-rw-r--r--code/api/src/Endpoints/Internal/Account/LoginRoute.cs2
-rw-r--r--code/api/src/Endpoints/Internal/Account/UpdateAccountRoute.cs6
-rw-r--r--code/api/src/Endpoints/Internal/Account/UserArchiveDto.cs131
4 files changed, 138 insertions, 7 deletions
diff --git a/code/api/src/Endpoints/Internal/Account/CreateAccountRoute.cs b/code/api/src/Endpoints/Internal/Account/CreateAccountRoute.cs
index 954fbf5..0f4a383 100644
--- a/code/api/src/Endpoints/Internal/Account/CreateAccountRoute.cs
+++ b/code/api/src/Endpoints/Internal/Account/CreateAccountRoute.cs
@@ -22,16 +22,16 @@ public class CreateAccountRoute : RouteBaseAsync.WithRequest<CreateAccountPayloa
[HttpPost("~/_/account/create")]
public override async Task<ActionResult> HandleAsync(CreateAccountPayload request, CancellationToken cancellationToken = default) {
if (request.Username.IsValidEmailAddress() == false) {
- return BadRequest(new ErrorResult("Invalid form", request.Username + " does not look like a valid email"));
+ return BadRequest(new KnownProblemModel("Invalid form", request.Username + " does not look like a valid email"));
}
if (request.Password.Length < 6) {
- return BadRequest(new ErrorResult("Invalid form", "The password requires 6 or more characters."));
+ return BadRequest(new KnownProblemModel("Invalid form", "The password requires 6 or more characters."));
}
var username = request.Username.Trim();
if (_context.Users.Any(c => c.Username == username)) {
- return BadRequest(new ErrorResult("Username is not available", "There is already a user registered with email: " + username));
+ return BadRequest(new KnownProblemModel("Username is not available", "There is already a user registered with email: " + username));
}
var user = new User(username);
diff --git a/code/api/src/Endpoints/Internal/Account/LoginRoute.cs b/code/api/src/Endpoints/Internal/Account/LoginRoute.cs
index 5b41c61..e4ef54c 100644
--- a/code/api/src/Endpoints/Internal/Account/LoginRoute.cs
+++ b/code/api/src/Endpoints/Internal/Account/LoginRoute.cs
@@ -28,7 +28,7 @@ public class LoginRoute : RouteBaseAsync
var user = _context.Users.SingleOrDefault(u => u.Username == request.Username);
if (user == default || !user.VerifyPassword(request.Password)) {
- return BadRequest(new ErrorResult("Invalid username or password"));
+ return BadRequest(new KnownProblemModel("Invalid username or password"));
}
await _userService.LogInUser(HttpContext, user, request.Persist);
diff --git a/code/api/src/Endpoints/Internal/Account/UpdateAccountRoute.cs b/code/api/src/Endpoints/Internal/Account/UpdateAccountRoute.cs
index a997dcb..31ff10b 100644
--- a/code/api/src/Endpoints/Internal/Account/UpdateAccountRoute.cs
+++ b/code/api/src/Endpoints/Internal/Account/UpdateAccountRoute.cs
@@ -24,11 +24,11 @@ public class UpdateAccountRoute : RouteBaseAsync.WithRequest<UpdatePayload>.With
}
if (request.Password.IsNullOrWhiteSpace() && request.Username.IsNullOrWhiteSpace()) {
- return BadRequest(new ErrorResult("Invalid request", "No data was submitted"));
+ return BadRequest(new KnownProblemModel("Invalid request", "No data was submitted"));
}
if (request.Password.HasValue() && request.Password.Length < 6) {
- return BadRequest(new ErrorResult("Invalid request",
+ return BadRequest(new KnownProblemModel("Invalid request",
"The new password must contain at least 6 characters"));
}
@@ -37,7 +37,7 @@ public class UpdateAccountRoute : RouteBaseAsync.WithRequest<UpdatePayload>.With
}
if (request.Username.HasValue() && !request.Username.IsValidEmailAddress()) {
- return BadRequest(new ErrorResult("Invalid request",
+ return BadRequest(new KnownProblemModel("Invalid request",
"The new username does not look like a valid email address"));
}
diff --git a/code/api/src/Endpoints/Internal/Account/UserArchiveDto.cs b/code/api/src/Endpoints/Internal/Account/UserArchiveDto.cs
new file mode 100644
index 0000000..5d259ab
--- /dev/null
+++ b/code/api/src/Endpoints/Internal/Account/UserArchiveDto.cs
@@ -0,0 +1,131 @@
+
+namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;
+
+/// <summary>
+/// Represents a user archive as it is provided to users.
+/// </summary>
+public class UserArchiveDto
+{
+ /// <inheritdoc cref="UserArchiveDto"/>
+ public UserArchiveDto(User user) {
+ Meta = new MetaDto {
+ GeneratedAt = AppDateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
+ };
+ User = new UserDto(user);
+ Entries = new List<EntryDto>();
+ }
+
+ /// <summary>
+ /// Metadata for the user archive.
+ /// </summary>
+ public MetaDto Meta { get; }
+
+ /// <summary>
+ /// Relevant user data for the archive.
+ /// </summary>
+ public UserDto User { get; }
+
+ /// <summary>
+ /// List of entries that the user has created.
+ /// </summary>
+ public List<EntryDto> Entries { get; }
+
+ public void CountEntries() {
+ Meta.EntryCount = Entries.Count;
+ }
+
+ /// <summary>
+ /// Represents a time entry in the data archive.
+ /// </summary>
+ public class EntryDto
+ {
+ public string CreatedAt { get; init; }
+
+ [JsonIgnore]
+ public DateTime StartDateTime { get; init; }
+
+ /// <summary>
+ /// ISO 8601 string of the UTC date the time entry started.
+ /// </summary>
+ public string Start => StartDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
+
+ [JsonIgnore]
+ public DateTime StopDateTime { get; init; }
+
+ /// <summary>
+ /// ISO 8601 string of the UTC date the time entry stopped.
+ /// </summary>
+ public string Stop => StopDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
+
+ /// <summary>
+ /// Total amount of minutes elapsed from start to stop on this time entry.
+ /// </summary>
+ public double Minutes => StopDateTime.Subtract(StartDateTime).TotalMinutes;
+
+ public string Description { get; init; }
+
+ /// <summary>
+ /// Archive spesific category for this time entry.
+ /// </summary>
+ public CategoryDto Category { get; init; }
+
+ /// <summary>
+ /// Archive spesific list of labels for this time entry.
+ /// </summary>
+ public List<LabelDto> Labels { get; init; }
+ }
+
+ /// <summary>
+ /// Time entry category as it is written to the user archive.
+ /// </summary>
+ public class CategoryDto
+ {
+ public string Name { get; init; }
+ public string Color { get; init; }
+ }
+
+ /// <summary>
+ /// Time entry label as it is written to the user archive.
+ /// </summary>
+ public class LabelDto
+ {
+ public string Name { get; init; }
+ public string Color { get; init; }
+ }
+
+
+ /// <summary>
+ /// Represents the user who this archive's data is based on.
+ /// </summary>
+ public class UserDto
+ {
+ /// <inheritdoc cref="UserDto"/>
+ public UserDto(User user) {
+ Username = user.Username;
+ CreatedAt = user.CreatedAt;
+ }
+
+ /// <summary>
+ /// UTC date this user was created.
+ /// </summary>
+ public DateTime CreatedAt { get; }
+
+ public string Username { get; }
+ }
+
+ /// <summary>
+ /// Represents the meta object which contains metdata for this archive.
+ /// </summary>
+ public class MetaDto
+ {
+ /// <summary>
+ /// ISO 8601 UTC date string for when this archive was created.
+ /// </summary>
+ public string GeneratedAt { get; init; }
+
+ /// <summary>
+ /// Amount of entries in the archive.
+ /// </summary>
+ public int EntryCount { get; set; }
+ }
+}