using AutoMapper; using Koogle.Application.DTOs; using Koogle.Application.Interfaces; using Koogle.Domain.Interfaces; using KoogleApp.Data; using KoogleApp.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Koogle.Application.Services { public class DayService( IDbContextFactory contextFactory, IDayRepository goalRepository, IMapper mapper, ICurrentUserService currentUserService, IAuthorizationService authorizationService) : IDayService { private readonly IDayRepository _goalRepository = goalRepository; private readonly ICurrentUserService _currentUserService = currentUserService; private readonly IAuthorizationService _authorizationService = authorizationService; public async Task> GetAllAsync(DayFilterDto filter, CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); var query = context.Days .Include(g => g.Members) //.Include(g => g.Status) //.Include(g => g.ActualValues.Where(av => !av.IsDeleted)) .AsQueryable(); // Filter anwenden query = ApplyFilters(query, filter); // Gesamtanzahl ermitteln var totalCount = await query.CountAsync(cancellationToken); // Sortierung anwenden //query = ApplySorting(query, filter); // Paginierung anwenden var goals = await query .Skip(filter.Skip) .Take(filter.PageSize) .ToListAsync(cancellationToken); var items = mapper.Map>(goals); return new PagedResultDto { Items = items, TotalCount = totalCount, Page = filter.Page, PageSize = filter.PageSize }; } /// /// Wendet Filter auf die Abfrage an. /// private static IQueryable ApplyFilters(IQueryable query, DayFilterDto filter) { //if (!string.IsNullOrWhiteSpace(filter.SearchTerm)) //{ // var searchTerm = filter.SearchTerm.ToLower(); // query = query.Where(g => // g.Name.ToLower().Contains(searchTerm) || // (g.Description != null && g.Description.ToLower().Contains(searchTerm))); //} //if (filter.CompanyId.HasValue) //{ // query = query.Where(g => g.CompanyId == filter.CompanyId.Value); //} //if (filter.CompanyIds?.Count > 0) //{ // query = query.Where(g => filter.CompanyIds.Contains(g.CompanyId)); //} //if (filter.YearId.HasValue) //{ // query = query.Where(g => g.YearId == filter.YearId.Value); //} //if (filter.Year.HasValue) //{ // query = query.Where(g => g.Year.YearNumber == filter.Year.Value); //} //if (filter.GoalType.HasValue) //{ // query = query.Where(g => g.GoalType == filter.GoalType.Value); //} //if (filter.TagId.HasValue) //{ // query = query.Where(g => g.Tags.Any(t => t.Id == filter.TagId.Value)); //} //if (filter.TagIds?.Count > 0) //{ // query = query.Where(g => g.Tags.Any(t => filter.TagIds.Contains(t.Id))); //} //if (filter.OwnerId.HasValue) //{ // query = query.Where(g => g.GoalOwners.Any(go => go.UserId == filter.OwnerId.Value)); //} //if (filter.IsInherited.HasValue) //{ // query = filter.IsInherited.Value // ? query.Where(g => g.ParentGoalId != null) // : query.Where(g => g.ParentGoalId == null); //} //if (filter.IsRootGoal == true) //{ // query = query.Where(g => g.ParentGoalId == null); //} return query; } } }