5
5
using Microsoft . AspNetCore . Authorization ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
7
using Microsoft . Data . SqlClient ;
8
- using Microsoft . Extensions . Caching . Memory ;
9
- using Microsoft . Extensions . Configuration ;
10
8
using Minio ;
11
9
using System ;
12
10
using System . IO ;
13
11
using System . IO . Compression ;
14
12
using System . Linq ;
15
- using System . Net . Http ;
16
13
using System . Threading . Tasks ;
17
14
18
15
namespace Journal_Limpet . Controllers
19
16
{
20
17
[ Route ( "api/[controller]" ) ]
21
18
[ ApiController ]
22
19
[ Authorize ( AuthenticationSchemes = "APITokenAuthentication" ) ]
23
- public class IntegrationController : ControllerBase
20
+ public class IntegrationController ( MSSQLDB db , MinioClient minioClient ) : ControllerBase
24
21
{
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
-
40
22
[ HttpGet ( "info" ) ]
41
- public async Task < JsonResult > GetInfo ( )
23
+ public async Task < IActionResult > GetInfo ( )
42
24
{
43
- var user = ( await _db . ExecuteListAsync < Profile > (
25
+ var user = ( await db . ExecuteListAsync < Profile > (
44
26
"SELECT * FROM user_profile WHERE user_identifier = @id" ,
45
27
new SqlParameter ( "@id" , Guid . Parse ( User . Identity . Name ) ) ) )
46
28
. FirstOrDefault ( ) ;
47
29
48
30
if ( user != null )
49
31
{
50
32
// This UUID has a user!
51
- return new JsonResult ( user ) ;
33
+ return Ok ( user ) ;
52
34
}
53
35
54
- return new JsonResult ( new
55
- {
56
- success = false ,
57
- error = "Missing account"
58
- } ) ;
36
+ return NotFound ( ) ;
59
37
}
60
38
61
39
[ HttpGet ( "all-journals/download" ) ]
62
40
public async Task < IActionResult > DownloadAllJournalsAsync ( )
63
41
{
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" ,
65
75
new SqlParameter ( "user_identifier" , User . Identity . Name ) ) ;
66
76
67
77
byte [ ] outBytes ;
@@ -72,7 +82,7 @@ public async Task<IActionResult> DownloadAllJournalsAsync()
72
82
{
73
83
foreach ( var journal in allUserJournals )
74
84
{
75
- var journalData = await JournalLoader . GetJournalForDate ( _db , _minioClient , User , journal . JournalDate . Date ) ;
85
+ var journalData = await JournalLoader . GetJournalForDate ( db , minioClient , User , journal . JournalDate . Date ) ;
76
86
var fileEntry = archive . CreateEntry ( journalData . fileName ) ;
77
87
78
88
using var fs = fileEntry . Open ( ) ;
0 commit comments