aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-10-30 16:40:03 +0100
committerivarlovlie <git@ivarlovlie.no>2022-10-30 16:40:03 +0100
commit0725e4f7cf4c6f723264b6d461b91c660d144cb7 (patch)
treeaae5876b5760c80679161d918c34d753ec0e2582 /code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs
parentd76c180c9631df015d37138045c79a46cca350e8 (diff)
downloadgreatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.tar.xz
greatoffice-0725e4f7cf4c6f723264b6d461b91c660d144cb7.zip
feat: Apiwork
Diffstat (limited to 'code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs')
-rw-r--r--code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs99
1 files changed, 49 insertions, 50 deletions
diff --git a/code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs b/code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs
index f1b70f3..0d9f817 100644
--- a/code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs
+++ b/code/api/src/Endpoints/Internal/Account/GetArchiveRoute.cs
@@ -2,61 +2,60 @@ namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;
public class GetAccountArchiveRoute : RouteBaseAsync.WithoutRequest.WithActionResult<UserArchiveDto>
{
- private readonly AppDbContext _context;
+ private readonly MainAppDatabase _database;
- /// <inheritdoc />
- public GetAccountArchiveRoute(AppDbContext context) {
- _context = context;
- }
+ public GetAccountArchiveRoute(MainAppDatabase database) {
+ _database = database;
+ }
- /// <summary>
- /// Get a data archive with the currently logged on user's data.
- /// </summary>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- [HttpGet("~/_/account/archive")]
- public override async Task<ActionResult<UserArchiveDto>> HandleAsync(CancellationToken cancellationToken = default) {
- var user = _context.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id);
- if (user == default) {
- await HttpContext.SignOutAsync();
- return Unauthorized();
- }
+ /// <summary>
+ /// Get a data archive with the currently logged on user's data.
+ /// </summary>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ [HttpGet("~/_/account/archive")]
+ public override async Task<ActionResult<UserArchiveDto>> HandleAsync(CancellationToken cancellationToken = default) {
+ var user = _database.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id);
+ if (user == default) {
+ await HttpContext.SignOutAsync();
+ return Unauthorized();
+ }
- var entries = _context.TimeEntries
- .AsNoTracking()
- .Include(c => c.Labels)
- .Include(c => c.Category)
- .Where(c => c.UserId == user.Id)
- .ToList();
+ var entries = _database.TimeEntries
+ .AsNoTracking()
+ .Include(c => c.Labels)
+ .Include(c => c.Category)
+ .Where(c => c.UserId == user.Id)
+ .ToList();
- var jsonOptions = new JsonSerializerOptions {
- WriteIndented = true
- };
+ var jsonOptions = new JsonSerializerOptions {
+ WriteIndented = true
+ };
- var dto = new UserArchiveDto(user);
- dto.Entries.AddRange(entries.Select(entry => new UserArchiveDto.EntryDto {
- CreatedAt = entry.CreatedAt.ToString("yyyy-MM-ddTHH:mm:ssZ"),
- StartDateTime = entry.Start,
- StopDateTime = entry.Stop,
- Description = entry.Description,
- Labels = entry.Labels
- .Select(c => new UserArchiveDto.LabelDto {
- Name = c.Name,
- Color = c.Color
- })
- .ToList(),
- Category = new UserArchiveDto.CategoryDto {
- Name = entry.Category.Name,
- Color = entry.Category.Color
- },
- }));
+ var dto = new UserArchiveDto(user);
+ dto.Entries.AddRange(entries.Select(entry => new UserArchiveDto.EntryDto {
+ CreatedAt = entry.CreatedAt.ToString("yyyy-MM-ddTHH:mm:ssZ"),
+ StartDateTime = entry.Start,
+ StopDateTime = entry.Stop,
+ Description = entry.Description,
+ Labels = entry.Labels
+ .Select(c => new UserArchiveDto.LabelDto {
+ Name = c.Name,
+ Color = c.Color
+ })
+ .ToList(),
+ Category = new UserArchiveDto.CategoryDto {
+ Name = entry.Category.Name,
+ Color = entry.Category.Color
+ },
+ }));
- dto.CountEntries();
+ dto.CountEntries();
- var entriesSerialized = JsonSerializer.SerializeToUtf8Bytes(dto, jsonOptions);
+ var entriesSerialized = JsonSerializer.SerializeToUtf8Bytes(dto, jsonOptions);
- return File(entriesSerialized,
- "application/json",
- user.Username + "-time-tracker-archive-" + AppDateTime.UtcNow.ToString("yyyyMMddTHHmmss") + ".json");
- }
-}
+ return File(entriesSerialized,
+ "application/json",
+ user.Username + "-time-tracker-archive-" + AppDateTime.UtcNow.ToString("yyyyMMddTHHmmss") + ".json");
+ }
+} \ No newline at end of file