Добавить таблицу для учета комментариев и действий пользователя для вывода статистики по ChangeLog #30

Open
on.nemtina wants to merge 24 commits from feature/#956-change-log-table-comment into master
5 changed files with 34 additions and 28 deletions
Showing only changes of commit ebd2cff40d - Show all commits

View File

@ -54,20 +54,20 @@ public class ChangeLogController : ControllerBase, IChangeLogApi
[HttpDelete]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Delete(Guid id, CancellationToken token)
public async Task<IActionResult> Delete(Guid id, string comment, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, [id], token);
var result = await repository.MarkAsDeleted(userId, [id], comment, token);
return Ok(result);
}
[HttpDelete("range")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public async Task<IActionResult> DeleteRange(IEnumerable<Guid> ids, CancellationToken token)
public async Task<IActionResult> DeleteRange(IEnumerable<Guid> ids, string comment, CancellationToken token)
{
var userId = User.GetUserId<Guid>();
var result = await repository.MarkAsDeleted(userId, ids, token);
var result = await repository.MarkAsDeleted(userId, ids, comment, token);
return Ok(result);
}

View File

@ -6,7 +6,7 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=postgres;Persist Security Info=True"
"DefaultConnection": "Host=localhost:5462;Database=persistence;Username=postgres;Password=postgres;Persist Security Info=True"
},
"AllowedHosts": "*",
"NeedUseKeyCloak": false,

View File

@ -36,7 +36,7 @@ public class ChangeLogRepository : IChangeLogRepository
return result;
}
public async Task<int> MarkAsDeleted(Guid idEditor, IEnumerable<Guid> ids, CancellationToken token)
public async Task<int> MarkAsDeleted(Guid idEditor, IEnumerable<Guid> ids, string comment, CancellationToken token)
{
var query = db.Set<ChangeLog>()
.Where(s => ids.Contains(s.Id))
@ -49,12 +49,12 @@ public class ChangeLogRepository : IChangeLogRepository
var entities = await query.ToArrayAsync(token);
var result = await MarkAsObsolete(idEditor, entities, token);
var result = await MarkAsObsolete(idEditor, entities, comment, token);
return result;
}
public async Task<int> MarkAsDeleted(Guid idEditor, Guid idDiscriminator, CancellationToken token)
public async Task<int> MarkAsDeleted(Guid idEditor, Guid idDiscriminator, string comment, CancellationToken token)
{
var query = db.Set<ChangeLog>()
.Where(s => s.IdDiscriminator == idDiscriminator)
@ -62,19 +62,26 @@ public class ChangeLogRepository : IChangeLogRepository
var entities = await query.ToArrayAsync(token);
var result = await MarkAsObsolete(idEditor, entities, token);
var result = await MarkAsObsolete(idEditor, entities, comment, token);
return result;
}
private async Task<int> MarkAsObsolete(Guid idEditor, IEnumerable<ChangeLog> entities, CancellationToken token)
private async Task<int> MarkAsObsolete(Guid idEditor, IEnumerable<ChangeLog> entities, string comment, CancellationToken token)
{
var updateTime = DateTimeOffset.UtcNow;
var commit = new ChangeLogCommit() {
Comment = comment,
Creation = updateTime,
Id = Uuid7.Guid(),
IdCommitAuthor = idEditor
};
db.Set<ChangeLogCommit>().Add(commit);
foreach (var entity in entities)
{
entity.Obsolete = updateTime;
//entity.IdEditor = idEditor;
entity.IdDiscriminator = commit.Id;
}
return await db.SaveChangesAsync(token);
@ -84,14 +91,16 @@ public class ChangeLogRepository : IChangeLogRepository
{
var result = 0;
var changeLogIds = commitDto.ChangeLogItems.Select(c => c.Id);
Review

Это не то.
Покрой этот метод интеграционным тестом плиз.

// arrange
add some data winth 2 discriminators
// act replace data for 1 discriminaqtor
// assert
check thar othe data with othe discr

Это не то. Покрой этот метод интеграционным тестом плиз. // arrange add some data winth 2 discriminators // act replace data for 1 discriminaqtor // assert check thar othe data with othe discr
var comment = commitDto.Comment;
using var transaction = await db.Database.BeginTransactionAsync(token);
result += await MarkAsDeleted(commitDto.IdAuthor, idDiscriminator, token);
result += await MarkAsDeleted(commitDto.IdAuthor, changeLogIds, comment, token);
result += await AddRange(idDiscriminator, commitDto, token);
await transaction.CommitAsync(token);
return result;
}
@ -104,15 +113,10 @@ public class ChangeLogRepository : IChangeLogRepository
.Where(s => updatedIds.Contains(s.Id))
.ToDictionary(s => s.Id);
var result = 0;
using var transaction = await db.Database.BeginTransactionAsync(token);
var commit = CreateCommit(commitDto);
db.Set<ChangeLogCommit>().Add(commit);
db.SaveChanges();
//using var transaction = await db.Database.BeginTransactionAsync(token);
foreach (var dto in commitDto.ChangeLogItems)
{
@ -122,19 +126,17 @@ public class ChangeLogRepository : IChangeLogRepository
throw new ArgumentException($"Entity with id = {dto.Id} doesn't exist in Db", nameof(dto));
}
var newEntity = CreateChangeLogFromDto(commitDto.IdAuthor, updatedEntity.IdDiscriminator, commit.Id, dto);
var newEntity = CreateChangeLogFromDto(updatedEntity.IdDiscriminator, commit.Id, commitDto.IdAuthor, dto);
dbSet.Add(newEntity);
updatedEntity.IdNext = newEntity.Id;
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
}
result = await db.SaveChangesAsync(token);
//await transaction.CommitAsync(token);
var result = await db.SaveChangesAsync(token);
await transaction.CommitAsync(token);
return result;
}
public async Task<PaginationContainer<ChangeLogValuesDto>> GetByDate(

View File

@ -90,17 +90,19 @@ public interface IChangeLogApi : ISyncWithDiscriminatorApi<ChangeLogValuesDto>
/// Удалить одну запись
/// </summary>
/// <param name="id"></param>
/// <param name="comment">комментарий к удалению</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IActionResult> Delete(Guid id, CancellationToken token);
Task<IActionResult> Delete(Guid id, string comment, CancellationToken token);
/// <summary>
/// Удалить несколько записей
/// </summary>
/// <param name="ids"></param>
/// <param name="comment">комментарий к удалению</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IActionResult> DeleteRange(IEnumerable<Guid> ids, CancellationToken token);
Task<IActionResult> DeleteRange(IEnumerable<Guid> ids, string comment, CancellationToken token);
/// <summary>
/// Получение списка дат, в которые происходили изменения (день, месяц, год, без времени)

View File

@ -24,18 +24,20 @@ public interface IChangeLogRepository : ISyncWithDiscriminatorRepository<ChangeL
/// </summary>
/// <param name="idEditor"></param>
/// <param name="ids">ключи записей</param>
/// <param name="comment">комментарий к удалению</param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkAsDeleted(Guid idEditor, IEnumerable<Guid> ids, CancellationToken token);
Task<int> MarkAsDeleted(Guid idEditor, IEnumerable<Guid> ids, string comment, CancellationToken token);
/// <summary>
/// Пометить записи как удаленные
/// </summary>
/// <param name="idEditor"></param>
/// <param name="idDiscriminator">дискриминатор таблицы</param>
/// <param name="comment">комментарий к удалению</param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkAsDeleted(Guid idEditor, Guid idDiscriminator, CancellationToken token);
Task<int> MarkAsDeleted(Guid idEditor, Guid idDiscriminator, string comment, CancellationToken token);
/// <summary>
/// Очистить и добавить новые