135 lines
4.5 KiB
C#
135 lines
4.5 KiB
C#
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<AppDbContext> 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<PagedResultDto<DaySummaryDto>> 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<List<DaySummaryDto>>(goals);
|
|
|
|
return new PagedResultDto<DaySummaryDto>
|
|
{
|
|
Items = items,
|
|
TotalCount = totalCount,
|
|
Page = filter.Page,
|
|
PageSize = filter.PageSize
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wendet Filter auf die Abfrage an.
|
|
/// </summary>
|
|
private static IQueryable<Day> ApplyFilters(IQueryable<Day> 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;
|
|
}
|
|
}
|
|
}
|