| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Org.BouncyCastle.Crypto.Digests; |
| | | 9 | | using WinSolutions.Sveta.Common; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 12 | | using WinSolutions.Sveta.Server.Domain; |
| | | 13 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 14 | | |
| | | 15 | | namespace 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, |
| | 0 | 24 | | ILogger<MovementNotesService> logger) :base(authenticationService) |
| | 0 | 25 | | { |
| | 0 | 26 | | _logger = logger; |
| | 0 | 27 | | _db = db; |
| | 0 | 28 | | } |
| | | 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) |
| | 0 | 32 | | { |
| | 0 | 33 | | var notes = PrepareQuery(); |
| | 0 | 34 | | int total = userId != 0 |
| | 0 | 35 | | ? await FilterNote(notes, 0, 0, userId, DateTime.MinValue, DateTime.MinValue).CountAsync() |
| | 0 | 36 | | : await notes.CountAsync(); |
| | 0 | 37 | | var filterNotes = FilterNote(notes, movementId, movementKindId, userId, dateFrom, dateTo); |
| | 0 | 38 | | int filteredCount = await filterNotes.CountAsync(); |
| | 0 | 39 | | filterNotes = SortQuery(filterNotes, sort); |
| | 0 | 40 | | return new PaginatedData<List<MovementNote>> |
| | 0 | 41 | | { |
| | 0 | 42 | | TotalCount = total, |
| | 0 | 43 | | TotalFilteredCount = filteredCount, |
| | 0 | 44 | | Result = await filterNotes.Skip((page < 2 ? 0 : page - 1) * limit).Take(limit).ToListAsync() |
| | 0 | 45 | | }; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task<int> GetCountNotes(long movementId, long movementKindId, long userId, |
| | | 49 | | DateTime dateFrom, DateTime dateTo) => |
| | 0 | 50 | | await FilterNote(PrepareQuery(), movementId, movementKindId, userId, dateFrom, dateTo).CountAsync(); |
| | | 51 | | |
| | 0 | 52 | | private IQueryable<Movement> PrepareQuery() => _db.Movements |
| | 0 | 53 | | .Include(d => d.Notes) |
| | 0 | 54 | | .ThenInclude(Notes => Notes.CreatedByUser) |
| | 0 | 55 | | .Include(d => d.MovementType) |
| | 0 | 56 | | .Where(d => !d.IsDeleted) |
| | 0 | 57 | | .AsQueryable(); |
| | | 58 | | |
| | | 59 | | private IQueryable<MovementNote> SortQuery(IQueryable<MovementNote> notes, string sort = default) => |
| | 0 | 60 | | (sort ?? "").ToLower() switch |
| | 0 | 61 | | { |
| | 0 | 62 | | "id|desc" => notes.OrderByDescending(d => d.Id), |
| | 0 | 63 | | "created_on" => notes.OrderBy(d => d.CreationDateTime), |
| | 0 | 64 | | "created_on|desc" => notes.OrderByDescending(d => d.CreationDateTime), |
| | 0 | 65 | | "user" => notes.OrderBy(d => d.CreatedByUser.Id), |
| | 0 | 66 | | "user|desc" => notes.OrderByDescending(d => d.CreatedByUser.Id), |
| | 0 | 67 | | _ => notes |
| | 0 | 68 | | }; |
| | | 69 | | private IQueryable<MovementNote> FilterNote(IQueryable<Movement> movements, long movementId, long movementKindId |
| | 0 | 70 | | DateTime dateFrom, DateTime dateTo) => movements |
| | 0 | 71 | | .Where(d => movementId == 0 || d.Id == movementId) |
| | 0 | 72 | | .Where(d => movementKindId == 0 || d.MovementType.Id == movementKindId) |
| | 0 | 73 | | .Where(d => userId == 0 || d.Notes.Any(s => s.CreatedByUser.Id == userId)) |
| | 0 | 74 | | .Where(d => dateFrom == DateTime.MinValue || d.Notes.Any(s => s.CreationDateTime >= dateFrom)) |
| | 0 | 75 | | .Where(d => dateTo == DateTime.MinValue || d.Notes.Any(s => s.CreationDateTime < dateTo)) |
| | 0 | 76 | | .SelectMany(d => d.Notes) |
| | 0 | 77 | | .AsQueryable(); |
| | | 78 | | } |
| | | 79 | | } |