changes
This commit is contained in:
19
Repositories/IJournalRepository.cs
Normal file
19
Repositories/IJournalRepository.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using public_valetax.Models;
|
||||
using public_valetax.DTOs;
|
||||
|
||||
namespace public_valetax.Repositories
|
||||
{
|
||||
public interface IJournalRepository
|
||||
{
|
||||
Task<JournalEntry> CreateJournalEntryAsync(
|
||||
long eventId,
|
||||
string exceptionType,
|
||||
string message,
|
||||
string? queryParameters = null,
|
||||
string? bodyParameters = null,
|
||||
string? stackTrace = null);
|
||||
|
||||
Task<MJournal?> GetJournalEntryAsync(long id);
|
||||
Task<MRange_MJournalInfo> GetJournalEntriesRangeAsync(int skip, int take, VJournalFilter filter);
|
||||
}
|
||||
}
|
||||
16
Repositories/ITreeRepository.cs
Normal file
16
Repositories/ITreeRepository.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using public_valetax.Models;
|
||||
using public_valetax.DTOs;
|
||||
|
||||
namespace public_valetax.Repositories
|
||||
{
|
||||
public interface ITreeRepository
|
||||
{
|
||||
Task<Tree?> GetTreeByNameAsync(string name);
|
||||
Task<Tree> CreateTreeAsync(string name);
|
||||
Task<Node?> GetNodeByIdAsync(long nodeId);
|
||||
Task<Node> CreateNodeAsync(long treeId, long? parentId, string name);
|
||||
Task DeleteNodeAsync(long nodeId);
|
||||
Task RenameNodeAsync(long nodeId, string newName);
|
||||
Task<MNode?> GetTreeStructureAsync(string treeName);
|
||||
}
|
||||
}
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
168
Repositories/TreeRepository.cs
Normal file
168
Repositories/TreeRepository.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using public_valetax.Data;
|
||||
using public_valetax.Models;
|
||||
using public_valetax.DTOs;
|
||||
using public_valetax.Exceptions;
|
||||
|
||||
namespace public_valetax.Repositories
|
||||
{
|
||||
public class TreeRepository(ApplicationDbContext _context) : ITreeRepository
|
||||
{
|
||||
public async Task<Tree?> GetTreeByNameAsync(string name)
|
||||
{
|
||||
return await _context.Trees
|
||||
.Include(t => t.Nodes)
|
||||
.FirstOrDefaultAsync(t => t.Name == name);
|
||||
}
|
||||
|
||||
public async Task<Tree> CreateTreeAsync(string name)
|
||||
{
|
||||
var tree = new Tree
|
||||
{
|
||||
Name = name,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
if (await _context.Trees.AnyAsync(t => t.Name == name))
|
||||
{
|
||||
throw new SecureException("Tree with such name already exist");
|
||||
}
|
||||
|
||||
_context.Trees.Add(tree);
|
||||
await _context.SaveChangesAsync();
|
||||
return tree;
|
||||
}
|
||||
|
||||
public async Task<Node?> GetNodeByIdAsync(long nodeId)
|
||||
{
|
||||
return await _context.Nodes.FindAsync(nodeId);
|
||||
}
|
||||
|
||||
public async Task<Node> CreateNodeAsync(long treeId, long? parentId, string name)
|
||||
{
|
||||
// Verify that parent node belongs to the same tree if provided
|
||||
if (parentId.HasValue)
|
||||
{
|
||||
var parentNode = await _context.Nodes.FindAsync(parentId.Value);
|
||||
if (parentNode == null || parentNode.TreeId != treeId)
|
||||
{
|
||||
throw new SecureException("Parent node does not belong to the specified tree");
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a node with the same name already exists in the same tree
|
||||
var existingNode = await _context.Nodes
|
||||
.FirstOrDefaultAsync(n => n.TreeId == treeId && n.Name == name && n.ParentId == parentId);
|
||||
|
||||
if (existingNode != null)
|
||||
{
|
||||
throw new SecureException("A node with the same name already exists in this location");
|
||||
}
|
||||
|
||||
var node = new Node
|
||||
{
|
||||
TreeId = treeId,
|
||||
ParentId = parentId,
|
||||
Name = name,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.Nodes.Add(node);
|
||||
await _context.SaveChangesAsync();
|
||||
return node;
|
||||
}
|
||||
|
||||
public async Task DeleteNodeAsync(long nodeId)
|
||||
{
|
||||
var node = await _context.Nodes
|
||||
.Include(n => n.Children)
|
||||
.FirstOrDefaultAsync(n => n.Id == nodeId);
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
throw new SecureException("Node not found");
|
||||
}
|
||||
|
||||
// Check if node has children
|
||||
if (node.Children.Any())
|
||||
{
|
||||
throw new SecureException("You have to delete all children nodes first");
|
||||
}
|
||||
|
||||
_context.Nodes.Remove(node);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task RenameNodeAsync(long nodeId, string newName)
|
||||
{
|
||||
var node = await _context.Nodes.FindAsync(nodeId);
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
throw new SecureException("Node not found");
|
||||
}
|
||||
|
||||
// Check if another node with the same name already exists in the same parent
|
||||
var existingNode = await _context.Nodes
|
||||
.FirstOrDefaultAsync(n => n.TreeId == node.TreeId && n.Name == newName && n.ParentId == node.ParentId && n.Id != nodeId);
|
||||
|
||||
if (existingNode != null)
|
||||
{
|
||||
throw new SecureException("A node with the same name already exists in this location");
|
||||
}
|
||||
|
||||
node.Name = newName;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<MNode?> GetTreeStructureAsync(string treeName)
|
||||
{
|
||||
var tree = await _context.Trees
|
||||
.Include(t => t.Nodes)
|
||||
.FirstOrDefaultAsync(t => t.Name == treeName);
|
||||
|
||||
if (tree == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Build the tree structure recursively
|
||||
var rootNodes = tree.Nodes
|
||||
.Where(n => n.ParentId == null)
|
||||
.Select(n => BuildMNode(n, tree.Nodes))
|
||||
.ToList();
|
||||
|
||||
// If no root node exists, create one
|
||||
if (!rootNodes.Any())
|
||||
{
|
||||
return new MNode
|
||||
{
|
||||
Id = 0,
|
||||
Name = tree.Name,
|
||||
Children = []
|
||||
};
|
||||
}
|
||||
|
||||
return rootNodes.First();
|
||||
}
|
||||
|
||||
private MNode BuildMNode(Node node, ICollection<Node> allNodes)
|
||||
{
|
||||
var mNode = new MNode
|
||||
{
|
||||
Id = node.Id,
|
||||
Name = node.Name,
|
||||
Children = []
|
||||
};
|
||||
|
||||
// Add children recursively
|
||||
var children = allNodes.Where(n => n.ParentId == node.Id).ToList();
|
||||
foreach (var child in children)
|
||||
{
|
||||
mNode.Children.Add(BuildMNode(child, allNodes));
|
||||
}
|
||||
|
||||
return mNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user