DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/ManualFolderRepository.cs
Степанов Дмитрий Александрович e56530a10f Добавление логики работы с инструкциями
1. Добавил сервисы и репозитории для инфструкций
2. Добавил контроллеры
3. Обновил конфиг
2023-08-10 11:45:05 +05:00

62 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.Manuals;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using AsbCloudDb.Model.Manuals;
using Mapster;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Repository;
public class ManualFolderRepository : CrudRepositoryBase<ManualFolderDto, ManualFolder>, IManualFolderRepository
{
public ManualFolderRepository(IAsbCloudDbContext context) : base(context)
{
}
public async Task<IEnumerable<ManualFolderDto>> GetTreeAsync(int idCategory,
CancellationToken cancellationToken)
{
var folders = await dbContext.ManualFolders
.Where(m => m.IdCategory == idCategory)
.AsNoTracking()
.Include(m => m.Manuals)
.Include(m => m.Parent)
.ToArrayAsync(cancellationToken);
return BuildTree(folders).Select(x => x.Adapt<ManualFolderDto>());
}
public async Task<ManualFolderDto?> GetOrDefaultAsync(string name, int? idParent, int idCategory,
CancellationToken cancellationToken)
{
var entity = await dbContext.ManualFolders
.FirstOrDefaultAsync(m => m.Name == name &&
m.IdCategory == idCategory &&
m.IdParent == idParent, cancellationToken);
if (entity is null)
return null;
return Convert(entity);
}
private IEnumerable<ManualFolder> BuildTree(IEnumerable<ManualFolder> folders)
{
var folderDict = folders.ToDictionary(f => f.Id);
foreach (var folder in folders)
{
if (folder.IdParent.HasValue && folderDict.TryGetValue(folder.IdParent.Value, out var parent))
{
parent.Children ??= new List<ManualFolder>();
parent.Children.Add(folder);
}
}
return folders.Where(f => f.IdParent == null);
}
}