Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDb, User, IAuditable and Tests implementation added. #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/_ReSharper.Caches/
.idea/
.vs/
.cr/
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
13 changes: 0 additions & 13 deletions .idea/.idea.AuditSharp/.idea/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/.idea.AuditSharp/.idea/indexLayout.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/.idea.AuditSharp/.idea/vcs.xml

This file was deleted.

22 changes: 22 additions & 0 deletions AuditSharp.Sample/AuditSharp.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
<PackageReference Include="xunit" Version="2.9.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\AuditSharp.MongoDb\AuditSharp.MongoDb.csproj"/>
<ProjectReference Include="..\src\AuditSharp.PostgreSql\AuditSharp.PostgreSql.csproj"/>
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions AuditSharp.Sample/Controllers/AuditLogController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using AuditSharp.Core.Entities;
using AuditSharp.EntityFrameworkCore.Context;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AuditSharp.Sample.Controllers
{
/// <summary>
/// Controller for handling audit log operations.
/// </summary>
public class AuditLogController : Controller
{
private readonly IAuditSharpContext _auditSharpContext;

/// <summary>
/// Initializes a new instance of the <see cref="AuditLogController"/> class.
/// </summary>
/// <param name="auditSharpContext">The context for accessing audit logs.</param>
public AuditLogController(IAuditSharpContext auditSharpContext)
{
_auditSharpContext = auditSharpContext;
}

/// <summary>
/// Gets all audit logs.
/// </summary>
/// <returns>A list of audit logs.</returns>
[HttpGet("GetAuditLogs")]
public async Task<List<AuditLog>> GetAsync()
{
return await _auditSharpContext.GetAuditLogsQueryable<AuditLog>().ToListAsync();
}

/// <summary>
/// Gets audit logs by entity ID and entity name.
/// </summary>
/// <param name="entityId">The ID of the entity.</param>
/// <param name="entityName">The name of the entity.</param>
/// <returns>A list of audit logs for the specified entity.</returns>
[HttpGet("GetAuditLogsByEntityId/{entityId}/{entityName}")]
public async Task<List<AuditLog>> GetByEntityIdAsync(string entityId, string entityName)
{
return await _auditSharpContext.GetAuditLogsByEntityId<AuditLog>(entityId, entityName).ToListAsync();
}
}
}
103 changes: 103 additions & 0 deletions AuditSharp.Sample/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using AuditSharp.Core.Entities;
using AuditSharp.EntityFrameworkCore.Context;
using AuditSharp.Sample.DataAccess;
using AuditSharp.Sample.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AuditSharp.Sample.Controllers;

[ApiController]
[Route("[controller]")]
public class PersonController : ControllerBase
{
private readonly ExampleDbContext _dbContext;
private readonly IAuditSharpContext _auditSharpContext;
private readonly ILogger<PersonController> _logger;

/// <summary>
/// Initializes a new instance of the <see cref="PersonController"/> class.
/// </summary>
/// <param name="logger">The logger instance.</param>
/// <param name="dbContext">The database context instance.</param>
/// <param name="auditSharpContext">The audit context instance.</param>
public PersonController(ILogger<PersonController> logger, ExampleDbContext dbContext,
IAuditSharpContext auditSharpContext)
{
_logger = logger;
_dbContext = dbContext;
_auditSharpContext = auditSharpContext;
}

/// <summary>
/// HTTP GET endpoint to retrieve all Person entities.
/// </summary>
/// <returns>A list of Person entities.</returns>
[HttpGet(Name = "GetPersons")]
public async Task<List<Person>> Get()
{
var test = _auditSharpContext.GetAuditLogsQueryable<AuditLog>().ToList();
var test2 = _auditSharpContext
.GetAuditLogsByEntityId<AuditLog>("190c101b-9da6-4243-82f2-1c60a2e44119", "Person",
w => w.OperationType == "Added")
.ToList();
return await _dbContext.Persons.ToListAsync();
}

/// <summary>
/// HTTP POST endpoint to create a new Person entity.
/// </summary>
/// <param name="person">The Person entity to be created, provided in the request body.</param>
/// <returns>The created Person entity.</returns>
[HttpPost(Name = "CreatePerson")]
public async Task<Person> Create([FromBody] Person person)
{
// Adds the new Person entity to the DbContext.
var entity = await _dbContext.Persons.AddAsync(person);

// Saves the changes to the database.
await _dbContext.SaveChangesAsync();

// Returns the created Person entity.
return entity.Entity;
}

/// <summary>
/// HTTP PUT endpoint to update an existing Person entity.
/// </summary>
/// <param name="id">The ID of the Person entity to be updated.</param>
/// <param name="person">The updated Person entity, provided in the request body.</param>
/// <returns>The updated Person entity.</returns>
[HttpPut("{id}", Name = "UpdatePerson")]
public async Task<IActionResult> Update(Guid id, [FromBody] Person person)
{
var existingPerson = await _dbContext.Persons.FindAsync(id);
if (existingPerson == null) return NotFound();

existingPerson.Name = person.Name;
existingPerson.Surname = person.Surname;
existingPerson.BirthDate = person.BirthDate;
existingPerson.Description = person.Description;

await _dbContext.SaveChangesAsync();

return Ok(existingPerson);
}

/// <summary>
/// HTTP DELETE endpoint to delete an existing Person entity.
/// </summary>
/// <param name="id">The ID of the Person entity to be deleted.</param>
/// <returns>An IActionResult indicating the result of the operation.</returns>
[HttpDelete("{id}", Name = "DeletePerson")]
public async Task<IActionResult> Delete(Guid id)
{
var existingPerson = await _dbContext.Persons.FindAsync(id);
if (existingPerson == null) return NotFound();

_dbContext.Persons.Remove(existingPerson);
await _dbContext.SaveChangesAsync();

return NoContent();
}
}
19 changes: 19 additions & 0 deletions AuditSharp.Sample/DataAccess/ExampleDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using AuditSharp.Sample.Entities;
using Microsoft.EntityFrameworkCore;

namespace AuditSharp.Sample.DataAccess;

public class ExampleDbContext : DbContext
{
public ExampleDbContext(DbContextOptions<ExampleDbContext> options)
: base(options)
{
}

public DbSet<Person> Persons { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase("ExampleDb");
}
}
12 changes: 12 additions & 0 deletions AuditSharp.Sample/Entities/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using AuditSharp.Core.Abstracts;

namespace AuditSharp.Sample.Entities;

public class Person : IAuditable
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public DateTime BirthDate { get; set; }
public string Description { get; set; }
}
32 changes: 32 additions & 0 deletions AuditSharp.Sample/Middlewares/DefaultUserMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Security.Claims;

namespace AuditSharp.Sample.Middlewares;

public class DefaultUserMiddleware
{
private readonly RequestDelegate _next;

public DefaultUserMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
if (context?.User == null || !context.User.Identity?.IsAuthenticated == true)
{
// Varsayılan kullanıcıyı oluştur
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, "1"),
new Claim(ClaimTypes.Name, "DefaultUser"),
new Claim(ClaimTypes.Role, "DefaultRole")
};

var identity = new ClaimsIdentity(claims, "Default");
context.User = new ClaimsPrincipal(identity);
}

await _next(context);
}
}
12 changes: 12 additions & 0 deletions AuditSharp.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using AuditSharp.Sample;

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app, builder.Environment);

app.Run();
41 changes: 41 additions & 0 deletions AuditSharp.Sample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:8318",
"sslPort": 44379
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5247",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7073;http://localhost:5247",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy