< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.MovementNotesService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/MovementNotesService.cs
Covered lines:0
Uncovered lines:44
Coverable lines:44
Total lines:79
Line coverage:0% (0 of 44)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetNotes()0%0%
GetCountNotes()0%100%
PrepareQuery()0%100%
SortQuery(...)0%0%
FilterNote(...)0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/MovementNotesService.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Linq.Expressions;
 5using System.Threading.Tasks;
 6using Microsoft.EntityFrameworkCore;
 7using Microsoft.Extensions.Logging;
 8using Org.BouncyCastle.Crypto.Digests;
 9using WinSolutions.Sveta.Common;
 10using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 11using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 12using WinSolutions.Sveta.Server.Domain;
 13using WinSolutions.Sveta.Server.Services.Interfaces;
 14
 15namespace WinSolutions.Sveta.Server.Services.Implements
 16{
 17    public class MovementNotesService: SvetaServiceBase, IMovementNotesService
 18    {
 19        private readonly ILogger<MovementNotesService> _logger;
 20        private readonly SvetaDbContext _db;
 21
 22        public MovementNotesService(IAuthenticationService authenticationService,
 23            SvetaDbContext db,
 024            ILogger<MovementNotesService> logger) :base(authenticationService)
 025        {
 026            _logger = logger;
 027            _db = db;
 028        }
 29
 30        public async Task<PaginatedData<List<MovementNote>>> GetNotes(long movementId, long movementKindId, long userId,
 31            DateTime dateFrom, DateTime dateTo, int page, int limit, string sort = default)
 032        {
 033            var notes = PrepareQuery();
 034            int total = userId != 0
 035                ? await FilterNote(notes, 0, 0, userId, DateTime.MinValue, DateTime.MinValue).CountAsync()
 036                : await notes.CountAsync();
 037            var filterNotes = FilterNote(notes, movementId, movementKindId, userId, dateFrom, dateTo);
 038            int filteredCount = await filterNotes.CountAsync();
 039            filterNotes = SortQuery(filterNotes, sort);
 040            return new PaginatedData<List<MovementNote>>
 041            {
 042                TotalCount = total,
 043                TotalFilteredCount = filteredCount,
 044                Result = await filterNotes.Skip((page < 2 ? 0 : page - 1) * limit).Take(limit).ToListAsync()
 045            };
 046        }
 47
 48        public async Task<int> GetCountNotes(long movementId, long movementKindId, long userId,
 49            DateTime dateFrom, DateTime dateTo) =>
 050            await FilterNote(PrepareQuery(), movementId, movementKindId, userId, dateFrom, dateTo).CountAsync();
 51
 052        private IQueryable<Movement> PrepareQuery() => _db.Movements
 053            .Include(d => d.Notes)
 054            .ThenInclude(Notes => Notes.CreatedByUser)
 055            .Include(d => d.MovementType)
 056            .Where(d => !d.IsDeleted)
 057            .AsQueryable();
 58
 59        private IQueryable<MovementNote> SortQuery(IQueryable<MovementNote> notes, string sort = default) =>
 060            (sort ?? "").ToLower() switch
 061            {
 062                "id|desc" => notes.OrderByDescending(d => d.Id),
 063                "created_on" => notes.OrderBy(d => d.CreationDateTime),
 064                "created_on|desc" => notes.OrderByDescending(d => d.CreationDateTime),
 065                "user" => notes.OrderBy(d => d.CreatedByUser.Id),
 066                "user|desc" => notes.OrderByDescending(d => d.CreatedByUser.Id),
 067                _ => notes
 068            };
 69        private IQueryable<MovementNote> FilterNote(IQueryable<Movement> movements, long movementId, long movementKindId
 070            DateTime dateFrom, DateTime dateTo) => movements
 071            .Where(d => movementId == 0 || d.Id == movementId)
 072            .Where(d => movementKindId == 0 || d.MovementType.Id == movementKindId)
 073            .Where(d => userId == 0 || d.Notes.Any(s => s.CreatedByUser.Id == userId))
 074            .Where(d => dateFrom == DateTime.MinValue || d.Notes.Any(s => s.CreationDateTime >= dateFrom))
 075            .Where(d => dateTo == DateTime.MinValue || d.Notes.Any(s => s.CreationDateTime < dateTo))
 076            .SelectMany(d => d.Notes)
 077            .AsQueryable();
 78    }
 79}