@echo using System.Collections.Generic;
@echo using System.Linq;
@echo using System.Threading.Tasks;
@echo using Microsoft.EntityFrameworkCore;
@echo using HRD.WebApi.Repositories;
@echo using DAL.Models.Entities;
@echo using DAL.Models.Filters;
@echo.
@echo namespace DAL.Repositories
@echo {
@echo     public class %1Repository: BaseRepository^<%1^>
@echo     {
@echo         public %1Repository() : base(new WebApiContext())
@echo         {
@echo         }
@echo.
@echo         public async Task^<List^<%1^>^> GetListByFilterAsync(%2Filter filter, bool asNoTracking = true)
@echo         {
@echo             var items = this.RepositoryContext.Set^<%1^>().AsQueryable();
@echo.            
@echo             if (filter.%2Id != null ^&^& filter.%2Id != 0)
@echo             {
@echo                 items = items.Where(x =^> x.%1Id == filter.%2Id);
@echo                 return asNoTracking ? await items.ToListAsync() : await items.ToListAsync();
@echo             }
@echo.            
@echo /*
@echo             // gets entity x if x.FirstName or x.LastName contains filter.Name, case independent
@echo             if (!string.IsNullOrEmpty(filter.Name))
@echo             {
@echo                 items = items.Where(x =^> EF.Functions.Like(x.FirstName.ToLower()+' '+ x.LastName.ToLower(), $"%%{filter.Name.ToLower()}%%"));
@echo             }
@echo.            
@echo             // N:N - filter: gets entity x where one of Id in x.IdList (comma separated list of ids) is infilter.ListIds (array of Ids from Hensel-Selection multiselect Component)
@echo             if (filter.ListIds != null ^&^& filter.ListIds.Length ^> 0)
@echo             {
@echo                 IQueryable^<%1^> itemsTmp = null;
@echo                 for (int i = 0; i^< filter.ListIds.Length; i++)
@echo                 {
@echo                     var inx = filter.ListIds[i];
@echo                     var items_ = items.Where(x =^> EF.Functions.Like("," + x.IdList.Replace(" ", "") + ",", $"%%,{inx},%%"));
@echo                     itemsTmp = (itemsTmp != null) ? itemsTmp.Concat(items_) : items_;
@echo                 }
@echo                 items = itemsTmp;
@echo             }
@echo */
@echo.            
@echo             return asNoTracking ? await items.AsNoTracking().ToListAsync() : await items.ToListAsync();
@echo         }
@echo     }
@echo }
