changes
This commit is contained in:
96
Repositories/JournalRepository.cs
Normal file
96
Repositories/JournalRepository.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using public_valetax.Data;
|
||||
using public_valetax.Models;
|
||||
using public_valetax.DTOs;
|
||||
|
||||
namespace public_valetax.Repositories
|
||||
{
|
||||
public class JournalRepository(ApplicationDbContext _context) : IJournalRepository
|
||||
{
|
||||
public async Task<JournalEntry> CreateJournalEntryAsync(
|
||||
long eventId,
|
||||
string exceptionType,
|
||||
string message,
|
||||
string? queryParameters = null,
|
||||
string? bodyParameters = null,
|
||||
string? stackTrace = null)
|
||||
{
|
||||
var journalEntry = new JournalEntry
|
||||
{
|
||||
EventId = eventId,
|
||||
Timestamp = DateTime.UtcNow,
|
||||
ExceptionType = exceptionType,
|
||||
Message = message,
|
||||
QueryParameters = queryParameters,
|
||||
BodyParameters = bodyParameters,
|
||||
StackTrace = stackTrace
|
||||
};
|
||||
|
||||
_context.JournalEntries.Add(journalEntry);
|
||||
await _context.SaveChangesAsync();
|
||||
return journalEntry;
|
||||
}
|
||||
|
||||
public async Task<MJournal?> GetJournalEntryAsync(long id)
|
||||
{
|
||||
var journalEntry = await _context.JournalEntries.FindAsync(id);
|
||||
|
||||
if (journalEntry == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new MJournal
|
||||
{
|
||||
Id = journalEntry.Id,
|
||||
EventId = journalEntry.EventId,
|
||||
Text = journalEntry.Message,
|
||||
CreatedAt = journalEntry.Timestamp
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<MRange_MJournalInfo> GetJournalEntriesRangeAsync(int skip, int take, VJournalFilter filter)
|
||||
{
|
||||
var query = _context.JournalEntries.AsQueryable();
|
||||
|
||||
// Apply filters if provided
|
||||
if (filter.From.HasValue)
|
||||
{
|
||||
query = query.Where(j => j.Timestamp >= filter.From.Value);
|
||||
}
|
||||
|
||||
if (filter.To.HasValue)
|
||||
{
|
||||
query = query.Where(j => j.Timestamp <= filter.To.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(filter.Search))
|
||||
{
|
||||
query = query.Where(j => j.Message.Contains(filter.Search) || j.ExceptionType.Contains(filter.Search));
|
||||
}
|
||||
|
||||
// Get total count before pagination
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
// Apply pagination
|
||||
var journalEntries = await query
|
||||
.OrderByDescending(j => j.Timestamp)
|
||||
.Skip(skip)
|
||||
.Take(take)
|
||||
.Select(j => new MJournalInfo
|
||||
{
|
||||
Id = j.Id,
|
||||
EventId = j.EventId,
|
||||
CreatedAt = j.Timestamp
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return new MRange_MJournalInfo
|
||||
{
|
||||
Skip = skip,
|
||||
Count = totalCount,
|
||||
Items = journalEntries
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user