From 88110f536f9c3843ecf5016122e101f8a424af77 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 22 Jan 2022 22:43:38 +0100 Subject: Initial commit --- src/server/Api/Internal/Account/GetArchiveRoute.cs | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/server/Api/Internal/Account/GetArchiveRoute.cs (limited to 'src/server/Api/Internal/Account/GetArchiveRoute.cs') diff --git a/src/server/Api/Internal/Account/GetArchiveRoute.cs b/src/server/Api/Internal/Account/GetArchiveRoute.cs new file mode 100644 index 0000000..5dc006e --- /dev/null +++ b/src/server/Api/Internal/Account/GetArchiveRoute.cs @@ -0,0 +1,36 @@ +using IOL.BookmarkThing.Server.Api.Internal.Dtos; + +namespace IOL.BookmarkThing.Server.Api.Internal.Account; + +public class GetArchiveRoute : RouteBaseInternalSync.WithoutRequest.WithActionResult +{ + private readonly AppDbContext _context; + + public GetArchiveRoute(AppDbContext context) { + _context = context; + } + + [ApiVersionNeutral] + [ApiExplorerSettings(IgnoreApi = true)] + [HttpGet("~/v{version:apiVersion}/account/archive")] + public override ActionResult Handle() { + var user = _context.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id); + if (user == default) { + return NotFound(); + } + + var entries = _context.Entries.Where(c => c.UserId == user.Id); + var archive = new UserArchiveDto() { + Created = DateTime.UtcNow, + User = new UserArchiveDto.UserArchiveUser(user), + Entries = entries.Select(c => new UserArchiveDto.UserArchiveEntry(c)).ToList() + }; + var jsonOptions = new JsonSerializerOptions { + WriteIndented = true + }; + var archiveBytes = JsonSerializer.SerializeToUtf8Bytes(archive, jsonOptions); + return File(archiveBytes, + "application/json", + "bookmark-thing-archive-" + user.Username + "-" + DateTime.UtcNow.ToString("yyyyMMddTHHmmss") + ".json"); + } +} -- cgit v1.3