Skip to content

Commit

Permalink
feat: API endpoint to fetch latest journal
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Nov 18, 2024
1 parent 80c960e commit 5326f46
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
68 changes: 39 additions & 29 deletions Journal-Limpet/Controllers/IntegrationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,73 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Minio;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Journal_Limpet.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize(AuthenticationSchemes = "APITokenAuthentication")]
public class IntegrationController : ControllerBase
public class IntegrationController(MSSQLDB db, MinioClient minioClient) : ControllerBase
{
private readonly MSSQLDB _db;
private readonly IConfiguration _configuration;
private readonly IMemoryCache _memoryCache;
private readonly MinioClient _minioClient;
private readonly IHttpClientFactory _httpClientFactory;

public IntegrationController(MSSQLDB db, IConfiguration configuration, IMemoryCache memoryCache, MinioClient minioClient, IHttpClientFactory httpClientFactory)
{
_db = db;
_configuration = configuration;
_memoryCache = memoryCache;
_minioClient = minioClient;
_httpClientFactory = httpClientFactory;
}

[HttpGet("info")]
public async Task<JsonResult> GetInfo()
public async Task<IActionResult> GetInfo()
{
var user = (await _db.ExecuteListAsync<Profile>(
var user = (await db.ExecuteListAsync<Profile>(
"SELECT * FROM user_profile WHERE user_identifier = @id",
new SqlParameter("@id", Guid.Parse(User.Identity.Name))))
.FirstOrDefault();

if (user != null)
{
// This UUID has a user!
return new JsonResult(user);
return Ok(user);
}

return new JsonResult(new
{
success = false,
error = "Missing account"
});
return NotFound();
}

[HttpGet("all-journals/download")]
public async Task<IActionResult> DownloadAllJournalsAsync()
{
var allUserJournals = await _db.ExecuteListAsync<UserJournal>("SELECT * FROM user_journal WHERE user_identifier = @user_identifier AND last_processed_line_number > 0 ORDER BY journal_date ASC",
var allUserJournals = await db.ExecuteListAsync<UserJournal>("SELECT * FROM user_journal WHERE user_identifier = @user_identifier AND last_processed_line_number > 0 ORDER BY journal_date ASC",
new SqlParameter("user_identifier", User.Identity.Name));

byte[] outBytes;

using (var ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var journal in allUserJournals)
{
var journalData = await JournalLoader.GetJournalForDate(db, minioClient, User, journal.JournalDate.Date);
var fileEntry = archive.CreateEntry(journalData.fileName);

using var fs = fileEntry.Open();
using var sw = new StreamWriter(fs);
{
await sw.WriteAsync(journalData.journalContent);
}
}
}
ms.Seek(0, SeekOrigin.Begin);

outBytes = ms.ToArray();
}

return File(outBytes, "application/zip", $"JL-JournalBackup-{DateTime.Now.Date.ToShortDateString()}.zip");
}

[HttpGet("latest-journal/download")]
public async Task<IActionResult> DownloadLatestJournalsAsync()
{
var allUserJournals = await db.ExecuteListAsync<UserJournal>("SELECT TOP 1 * FROM user_journal WHERE user_identifier = @user_identifier AND last_processed_line_number > 0 ORDER BY journal_date DESC",
new SqlParameter("user_identifier", User.Identity.Name));

byte[] outBytes;
Expand All @@ -72,7 +82,7 @@ public async Task<IActionResult> DownloadAllJournalsAsync()
{
foreach (var journal in allUserJournals)
{
var journalData = await JournalLoader.GetJournalForDate(_db, _minioClient, User, journal.JournalDate.Date);
var journalData = await JournalLoader.GetJournalForDate(db, minioClient, User, journal.JournalDate.Date);
var fileEntry = archive.CreateEntry(journalData.fileName);

using var fs = fileEntry.Open();
Expand Down
1 change: 0 additions & 1 deletion Journal-Limpet/Journal-Limpet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<None Remove="News\2021-02-01-canonn-integration-released.md" />
<None Remove="News\2021-03-05-added-faq-page.md" />
<None Remove="News\2021-05-22-odyssey-ready.md" />
<None Remove="News\2022-06-26-tweet-changes-and-eddn-changes.md" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions Journal-Limpet/News/2024-11-18-new-api-endpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
pubdate: 2024-11-18T05:30:00Z
category: general
---

# New API endpoint!

Hey everyone! We're releasing a new API endpoint.. oh yeah, I have an API endpoint..

## API Endpoint for downloading journals

I kinda forgot to say anything about it earlier,
but we have an API endpoint for downloading journals.
You can find it at https://journal-limpet.com/api/integration/

The endpoints we have are:

- `/api/integration/info` - Gets your profile information with the info we have about your user.
- `/api/integration/all-journals/download` - Downloads all journals you have on your Journal Limpet account.
- `/api/integration/latest-journal/download` - Downloads the latest journal you have on your Journal Limpet account.

0 comments on commit 5326f46

Please sign in to comment.