Наработки по ChangeLog
This commit is contained in:
parent
5a44dfb109
commit
3b4af1fd8d
@ -5,6 +5,7 @@ using DD.Persistence.Models.Requests;
|
||||
using DD.Persistence.Repositories;
|
||||
using System.Net;
|
||||
using DD.Persistence.Models.Common;
|
||||
using DD.Persistence.API.Services;
|
||||
|
||||
namespace DD.Persistence.API.Controllers;
|
||||
|
||||
@ -13,11 +14,15 @@ namespace DD.Persistence.API.Controllers;
|
||||
[Route("api/[controller]")]
|
||||
public class ChangeLogController : ControllerBase, IChangeLogApi
|
||||
{
|
||||
private readonly IChangeLogRepository repository;
|
||||
public ChangeLogService service { get; }
|
||||
|
||||
public ChangeLogController(IChangeLogRepository repository)
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="service"></param>
|
||||
public ChangeLogController(ChangeLogService service)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
[HttpPost("{idDiscriminator}")]
|
||||
@ -31,7 +36,7 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
|
||||
//var userId = User.GetUserId<Guid>();
|
||||
var userId = Guid.NewGuid();
|
||||
var changeLogCommit = new ChangeLogCommitDto(userId, comment, [dto]);
|
||||
var result = await repository.AddRange(idDiscriminator, changeLogCommit, token);
|
||||
var result = await service.(idDiscriminator, changeLogCommit, token);
|
||||
|
||||
return CreatedAtAction(nameof(Add), result);
|
||||
}
|
||||
|
42
DD.Persistence.API/Services/ChangeLogService.cs
Normal file
42
DD.Persistence.API/Services/ChangeLogService.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using DD.Persistence.Repositories;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace DD.Persistence.API.Services;
|
||||
public class ChangeLogService
|
||||
{
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IChangeLogCommitRepository commitRepository;
|
||||
private readonly IChangeLogRepository repository;
|
||||
private readonly TimeSpan? AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60);
|
||||
|
||||
public ChangeLogService(
|
||||
IMemoryCache memoryCache,
|
||||
IChangeLogCommitRepository commitRepository,
|
||||
IChangeLogRepository repository)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
this.commitRepository = commitRepository;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private async Task<Guid> GetOrCreateAsync(Guid idUser, string comment, CancellationToken token)
|
||||
{
|
||||
var key = (idUser, comment);
|
||||
var commitId = await memoryCache.GetOrCreateAsync(key, async (cacheEntry) =>
|
||||
{
|
||||
cacheEntry.AbsoluteExpirationRelativeToNow = AbsoluteExpirationRelativeToNow;
|
||||
|
||||
var commitId = await commitRepository.Add(idUser, comment, token);
|
||||
|
||||
return commitId;
|
||||
});
|
||||
|
||||
return commitId;
|
||||
}
|
||||
|
||||
public async Task<int> AddRange( Guid idUser, string comment, CancellationToken token)
|
||||
{
|
||||
var cimmitId = await GetOrCreateAsync(idUser, comment, token);
|
||||
repository.AddRange(new);
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"DbConnection": {
|
||||
"Host": "postgres",
|
||||
"Port": 5432,
|
||||
"Host": "localhost",
|
||||
"Port": 5462,
|
||||
"Database": "persistence",
|
||||
"Username": "postgres",
|
||||
"Password": "postgres"
|
||||
|
@ -0,0 +1,44 @@
|
||||
using DD.Persistence.Database.Entity;
|
||||
using DD.Persistence.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UuidExtensions;
|
||||
|
||||
namespace DD.Persistence.Database.Repositories;
|
||||
public class ChangeLogCommitRepository : IChangeLogCommitRepository
|
||||
{
|
||||
private DbContext db;
|
||||
|
||||
public ChangeLogCommitRepository()
|
||||
{
|
||||
|
||||
}
|
||||
public ChangeLogCommitRepository(DbContext db)
|
||||
{
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
private virtual async Task<Guid> Add(Guid idUser, string comment, CancellationToken token) {
|
||||
|
||||
var commit = new ChangeLogCommit()
|
||||
{
|
||||
Id = Uuid7.Guid(),
|
||||
IdCommitAuthor = idUser,
|
||||
Comment = comment,
|
||||
Creation = DateTimeOffset.UtcNow
|
||||
};
|
||||
|
||||
db.Add(commit);
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
return commit.Id;
|
||||
|
||||
}
|
||||
|
||||
public abstract Task<Guid> GetOrCreate(Guid idUser, string comment, CancellationToken token);
|
||||
}
|
@ -12,15 +12,17 @@ namespace DD.Persistence.Database.Repositories;
|
||||
public class ChangeLogRepository : IChangeLogRepository
|
||||
{
|
||||
private readonly DbContext db;
|
||||
private readonly IChangeLogCommitRepository changeLogCommitRepo;
|
||||
|
||||
public ChangeLogRepository(DbContext db)
|
||||
public ChangeLogRepository(DbContext db, IChangeLogCommitRepository changeLogCommitRepo)
|
||||
{
|
||||
this.db = db;
|
||||
this.changeLogCommitRepo = changeLogCommitRepo;
|
||||
}
|
||||
|
||||
public async Task<int> AddRange(Guid idDiscriminator, ChangeLogCommitDto commitDto, CancellationToken token)
|
||||
{
|
||||
var commit = CreateCommit(commitDto);
|
||||
var commit = changeLogCommitRepo.Get(commitDto);
|
||||
db.Set<ChangeLogCommit>().Add(commit);
|
||||
|
||||
var entities = new List<ChangeLog>();
|
||||
@ -115,7 +117,7 @@ public class ChangeLogRepository : IChangeLogRepository
|
||||
|
||||
using var transaction = await db.Database.BeginTransactionAsync(token);
|
||||
|
||||
var commit = CreateCommit(commitDto);
|
||||
var commit = GetOrCreate(commitDto);
|
||||
db.Set<ChangeLogCommit>().Add(commit);
|
||||
|
||||
foreach (var dto in commitDto.ChangeLogItems)
|
||||
@ -221,7 +223,7 @@ public class ChangeLogRepository : IChangeLogRepository
|
||||
return entity;
|
||||
}
|
||||
|
||||
private static ChangeLogCommit CreateCommit(ChangeLogCommitDto commitDto)
|
||||
private static ChangeLogCommit GetOrCreate(ChangeLogCommitDto commitDto)
|
||||
{
|
||||
return new ChangeLogCommit()
|
||||
{
|
||||
|
22
DD.Persistence/Repositories/IChangeLogCommitRepository.cs
Normal file
22
DD.Persistence/Repositories/IChangeLogCommitRepository.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DD.Persistence.Repositories;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IChangeLogCommitRepository
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="idUser"></param>
|
||||
/// <param name="comment"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<Guid> Add(Guid idUser, string comment, CancellationToken token);
|
||||
}
|
Loading…
Reference in New Issue
Block a user