DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/WellOperationCategoryRepository.cs

47 lines
1.3 KiB
C#
Raw Permalink Normal View History

using AsbCloudApp.Repositories;
2024-02-08 11:38:25 +05:00
using AsbCloudDb.Model;
using Mapster;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Linq;
using AsbCloudApp.Data.WellOperation;
2024-02-08 11:38:25 +05:00
namespace AsbCloudInfrastructure.Repository;
public class WellOperationCategoryRepository : IWellOperationCategoryRepository
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
public WellOperationCategoryRepository(IAsbCloudDbContext db, IMemoryCache memoryCache)
{
this.db = db;
this.memoryCache = memoryCache;
}
public IEnumerable<WellOperationCategoryDto> Get(bool includeParents, bool includeHidden = true)
2024-02-08 11:38:25 +05:00
{
var categories = memoryCache
.GetOrCreateBasic(db.Set<WellOperationCategory>());
if (!includeHidden)
categories = categories.Where(o => o.IsHidden == false);
2024-02-08 11:38:25 +05:00
if (!includeParents)
{
var parentIds = categories
.Select(o => o.IdParent)
.Distinct();
categories = categories
.Where(o => !parentIds.Contains(o.Id));
}
var result = categories
.OrderBy(o => o.Name)
.Adapt<IEnumerable<WellOperationCategoryDto>>();
return result;
}
}