Skip to content

Commit 5326f46

Browse files
committed
feat: API endpoint to fetch latest journal
1 parent 80c960e commit 5326f46

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

Journal-Limpet/Controllers/IntegrationController.cs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,73 @@
55
using Microsoft.AspNetCore.Authorization;
66
using Microsoft.AspNetCore.Mvc;
77
using Microsoft.Data.SqlClient;
8-
using Microsoft.Extensions.Caching.Memory;
9-
using Microsoft.Extensions.Configuration;
108
using Minio;
119
using System;
1210
using System.IO;
1311
using System.IO.Compression;
1412
using System.Linq;
15-
using System.Net.Http;
1613
using System.Threading.Tasks;
1714

1815
namespace Journal_Limpet.Controllers
1916
{
2017
[Route("api/[controller]")]
2118
[ApiController]
2219
[Authorize(AuthenticationSchemes = "APITokenAuthentication")]
23-
public class IntegrationController : ControllerBase
20+
public class IntegrationController(MSSQLDB db, MinioClient minioClient) : ControllerBase
2421
{
25-
private readonly MSSQLDB _db;
26-
private readonly IConfiguration _configuration;
27-
private readonly IMemoryCache _memoryCache;
28-
private readonly MinioClient _minioClient;
29-
private readonly IHttpClientFactory _httpClientFactory;
30-
31-
public IntegrationController(MSSQLDB db, IConfiguration configuration, IMemoryCache memoryCache, MinioClient minioClient, IHttpClientFactory httpClientFactory)
32-
{
33-
_db = db;
34-
_configuration = configuration;
35-
_memoryCache = memoryCache;
36-
_minioClient = minioClient;
37-
_httpClientFactory = httpClientFactory;
38-
}
39-
4022
[HttpGet("info")]
41-
public async Task<JsonResult> GetInfo()
23+
public async Task<IActionResult> GetInfo()
4224
{
43-
var user = (await _db.ExecuteListAsync<Profile>(
25+
var user = (await db.ExecuteListAsync<Profile>(
4426
"SELECT * FROM user_profile WHERE user_identifier = @id",
4527
new SqlParameter("@id", Guid.Parse(User.Identity.Name))))
4628
.FirstOrDefault();
4729

4830
if (user != null)
4931
{
5032
// This UUID has a user!
51-
return new JsonResult(user);
33+
return Ok(user);
5234
}
5335

54-
return new JsonResult(new
55-
{
56-
success = false,
57-
error = "Missing account"
58-
});
36+
return NotFound();
5937
}
6038

6139
[HttpGet("all-journals/download")]
6240
public async Task<IActionResult> DownloadAllJournalsAsync()
6341
{
64-
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",
42+
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",
43+
new SqlParameter("user_identifier", User.Identity.Name));
44+
45+
byte[] outBytes;
46+
47+
using (var ms = new MemoryStream())
48+
{
49+
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
50+
{
51+
foreach (var journal in allUserJournals)
52+
{
53+
var journalData = await JournalLoader.GetJournalForDate(db, minioClient, User, journal.JournalDate.Date);
54+
var fileEntry = archive.CreateEntry(journalData.fileName);
55+
56+
using var fs = fileEntry.Open();
57+
using var sw = new StreamWriter(fs);
58+
{
59+
await sw.WriteAsync(journalData.journalContent);
60+
}
61+
}
62+
}
63+
ms.Seek(0, SeekOrigin.Begin);
64+
65+
outBytes = ms.ToArray();
66+
}
67+
68+
return File(outBytes, "application/zip", $"JL-JournalBackup-{DateTime.Now.Date.ToShortDateString()}.zip");
69+
}
70+
71+
[HttpGet("latest-journal/download")]
72+
public async Task<IActionResult> DownloadLatestJournalsAsync()
73+
{
74+
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",
6575
new SqlParameter("user_identifier", User.Identity.Name));
6676

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

7888
using var fs = fileEntry.Open();

Journal-Limpet/Journal-Limpet.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<None Remove="News\2021-02-01-canonn-integration-released.md" />
2424
<None Remove="News\2021-03-05-added-faq-page.md" />
2525
<None Remove="News\2021-05-22-odyssey-ready.md" />
26-
<None Remove="News\2022-06-26-tweet-changes-and-eddn-changes.md" />
2726
</ItemGroup>
2827

2928
<ItemGroup>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
pubdate: 2024-11-18T05:30:00Z
3+
category: general
4+
---
5+
6+
# New API endpoint!
7+
8+
Hey everyone! We're releasing a new API endpoint.. oh yeah, I have an API endpoint..
9+
10+
## API Endpoint for downloading journals
11+
12+
I kinda forgot to say anything about it earlier,
13+
but we have an API endpoint for downloading journals.
14+
You can find it at https://journal-limpet.com/api/integration/
15+
16+
The endpoints we have are:
17+
18+
- `/api/integration/info` - Gets your profile information with the info we have about your user.
19+
- `/api/integration/all-journals/download` - Downloads all journals you have on your Journal Limpet account.
20+
- `/api/integration/latest-journal/download` - Downloads the latest journal you have on your Journal Limpet account.

0 commit comments

Comments
 (0)