DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/WellOperationCategoryRepository.cs
2024-02-08 11:38:25 +05:00

45 lines
1.2 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
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)
{
var categories = memoryCache
.GetOrCreateBasic(db.Set<WellOperationCategory>());
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;
}
}