public Guid Get(string externalId, bool refreshCache)
        {
            var cacheKey   = $"{CacheKeyPrefix}/{externalId}";
            var cacheValue = _cache.Get(cacheKey);

            if (refreshCache || cacheValue == null)
            {
                var userId = _query
                             .AsQueryable()
                             .Where(x => x.ExternalId == externalId)
                             .Select(x => x.Id)
                             .FirstOrDefault();

                var cacheEntryOptions = new DistributedCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(10));

                _cache.Set(cacheKey, Encode(userId), cacheEntryOptions);

                return(userId);
            }
            else
            {
                return(Decode(cacheValue));
            }
        }
        public async Task <GetCustomerAuditlogQueryResult> Handle(GetCustomerAuditlogQuery request, CancellationToken cancellationToken)
        {
            var query = _query.AsQueryable()
                        .Include(x => x.AuditlogItems)
                        .ThenInclude(y => y.AuditlogItems)
                        .Where(x => x.EntityName == nameof(Customer))
                        .Where(x => x.EntityId == request.CustomerId);

            var totalItems = await query.CountAsync(cancellationToken);

            var auditLogs = await query
                            .OrderByDescending(c => c.ModifiedOn)
                            .Skip(request.PageSize * request.PageIndex)
                            .Take(request.PageSize)
                            .ToListAsync(cancellationToken);

            return(new GetCustomerAuditlogQueryResult
            {
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                TotalItems = totalItems,
                CustomerId = request.CustomerId,
                Auditlogs = _mapper.Map <IEnumerable <AuditlogDto> >(auditLogs)
            });
        }
Exemplo n.º 3
0
        public async Task <GetRoleByIdQueryResult> Handle(GetRoleByIdQuery request, CancellationToken cancellationToken)
        {
            var role = await _query.AsQueryable()
                       .ProjectTo <RoleDto>(_mapper.ConfigurationProvider)
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            return(new GetRoleByIdQueryResult {
                Role = role
            });
        }
        public async Task <GetUserByIdQueryResult> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
        {
            var user = await _query.AsQueryable()
                       .Include(user => user.UserRoles)
                       .ProjectTo <UserDto>(_mapper.ConfigurationProvider)
                       .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            return(new GetUserByIdQueryResult {
                User = user
            });
        }
        public async Task <GetCustomerByIdQueryResult> Handle(GetCustomerByIdQuery request, CancellationToken cancellationToken)
        {
            var customer = await _query.AsQueryable()
                           .ProjectTo <CustomerDto>(_mapper.ConfigurationProvider)
                           .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            return(new GetCustomerByIdQueryResult
            {
                Customer = customer
            });
        }
Exemplo n.º 6
0
        public async Task <GetInvoiceByIdQueryResult> Handle(GetInvoiceByIdQuery request, CancellationToken cancellationToken)
        {
            var invoice = await _query.AsQueryable()
                          .Include(invoice => invoice.InvoiceLines)
                          .ProjectTo <InvoiceDto>(_mapper.ConfigurationProvider)
                          .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (invoice != null)
            {
                invoice.InvoiceLines = invoice.InvoiceLines.OrderBy(x => x.LineNumber).ToList();
            }

            return(new GetInvoiceByIdQueryResult {
                Invoice = invoice
            });
        }
Exemplo n.º 7
0
        public async Task <CustomerLookupQueryResult> Handle(CustomerLookupQuery request, CancellationToken cancellationToken)
        {
            var query = _query.AsQueryable();

            if (!string.IsNullOrWhiteSpace(request.SearchTerm))
            {
                var isValidInteger = int.TryParse(request.SearchTerm, NumberStyles.Integer, CultureInfo.GetCultureInfo("nl-NL"), out var integerValue);

                query = query.Where(x =>
                                    EF.Functions.Like(x.Name, $"%{request.SearchTerm}%") ||
                                    (isValidInteger && EF.Functions.Like(x.Code.ToString(), $"%{request.SearchTerm}%"))
                                    );
            }

            if (request.Ids != null && request.Ids.Length > 0)
            {
                query = query.Where(x => request.Ids.Contains(x.Id));
            }

            var totalItems = await query.CountAsync(cancellationToken);

            var sortOrder = request.OrderByDescending ? SortDirection.Descending : SortDirection.Ascending;

            query = request.OrderBy switch
            {
                CustomerLookupOrderByEnum.Code => query.OrderBy(x => x.Code, sortOrder),
                CustomerLookupOrderByEnum.Name => query.OrderBy(x => x.Name, sortOrder),
                _ => throw new Exception($"OrderBy '{request.OrderBy}' not implemented.")
            };

            var customers = await query
                            .Skip(request.PageSize *request.PageIndex)
                            .Take(request.PageSize)
                            .ProjectTo <CustomerLookupDto>(_mapper.ConfigurationProvider)
                            //.WriteQueryStringToOutputWindowIfInDebugMode()
                            .ToListAsync(cancellationToken);

            return(new CustomerLookupQueryResult
            {
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                TotalItems = totalItems,
                Customers = customers
            });
        }
    }
        public async Task <SearchInvoicesQueryResult> Handle(SearchInvoicesQuery request, CancellationToken cancellationToken)
        {
            var query = _query.AsQueryable()
                        .Include(x => x.Customer)
                        .AsQueryable();

            if (!string.IsNullOrWhiteSpace(request.SearchTerm))
            {
                var isValidDate   = DateTime.TryParse(request.SearchTerm, CultureInfo.GetCultureInfo("nl-NL"), DateTimeStyles.None, out var dateValue);
                var isValidAmount = decimal.TryParse(request.SearchTerm, NumberStyles.Number, CultureInfo.GetCultureInfo("nl-NL"), out var amountValue);

                query = query.Where(x =>
                                    EF.Functions.Like(x.InvoiceNumber, $"%{request.SearchTerm}%") ||
                                    EF.Functions.Like(x.Customer.Name, $"%{request.SearchTerm}%") ||
                                    (isValidDate && x.InvoiceDate == dateValue)
                                    // || (isValidAmount && x.Amount == amountValue)
                                    );
            }

            var totalItems = await query.CountAsync(cancellationToken);

            var sortOrder = request.OrderByDescending ? SortDirection.Descending : SortDirection.Ascending;

            query = request.OrderBy switch
            {
                SearchInvoicesOrderByEnum.InvoiceNumber => query.OrderBy(x => x.InvoiceNumber, sortOrder),
                SearchInvoicesOrderByEnum.InvoiceDate => query.OrderBy(x => x.InvoiceDate, sortOrder),
                _ => throw new Exception($"OrderBy '{request.OrderBy}' not implemented.")
            };

            var invoices = await query
                           .Skip(request.PageSize *request.PageIndex)
                           .Take(request.PageSize)
                           .ProjectTo <SearchInvoiceDto>(_mapper.ConfigurationProvider)
                           //.WriteQueryStringToOutputWindowIfInDebugMode()
                           .ToListAsync(cancellationToken);

            return(new SearchInvoicesQueryResult
            {
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                TotalItems = totalItems,
                Invoices = invoices
            });
        }
    }
Exemplo n.º 9
0
        public async Task <UserLookupQueryResult> Handle(UserLookupQuery request, CancellationToken cancellationToken)
        {
            var query = _query.AsQueryable();

            if (!string.IsNullOrWhiteSpace(request.SearchTerm))
            {
                query = query.Where(x => EF.Functions.Like(x.Fullname, $"%{request.SearchTerm}%"));
            }

            if (request.Ids != null && request.Ids.Length > 0)
            {
                query = query.Where(x => request.Ids.Contains(x.Id));
            }

            var totalItems = await query.CountAsync(cancellationToken);

            var sortOrder = request.OrderByDescending ? SortDirection.Descending : SortDirection.Ascending;

            query = request.OrderBy switch
            {
                UserLookupOrderByEnum.Fullname => query.OrderBy(x => x.Fullname, sortOrder),
                UserLookupOrderByEnum.FamilyName => query.OrderBy(x => x.FamilyName, sortOrder),
                _ => throw new Exception($"OrderBy '{request.OrderBy}' not implemented.")
            };

            var users = await query
                        .Skip(request.PageSize *request.PageIndex)
                        .Take(request.PageSize)
                        .ProjectTo <UserLookupDto>(_mapper.ConfigurationProvider)
                        //.WriteQueryStringToOutputWindowIfInDebugMode()
                        .ToListAsync(cancellationToken);

            return(new UserLookupQueryResult
            {
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,
                TotalItems = totalItems,
                Users = users
            });
        }
    }
        public async Task <InvoiceToPdfModel> MapAsync(Invoice invoice, CancellationToken cancellationToken)
        {
            var customer = await _customerQuery.AsQueryable()
                           .SingleAsync(x => x.Id == invoice.CustomerId, cancellationToken);

            //var contact = invoice.ContactId.HasValue ?
            //    await _contactQuery.AsQueryable()
            //        .SingleAsync(x => x.Id == invoice.ContactId, cancellationToken) :
            //    null;
            //var itemIds = invoice.InvoiceLines.Where(x => x.ItemId.HasValue).Select(x => x.ItemId);
            //var items = itemIds.Any() ?
            //    await _itemQuery.AsQueryable()
            //        .Where(x => itemIds.Contains(x.Id))
            //        .ToListAsync(cancellationToken) :
            //    new List<Item>();

            var invoiceToPdfModel = new InvoiceToPdfModel
            {
                InvoiceNumber = invoice.InvoiceNumber,
                InvoiceDate   = invoice.InvoiceDate,
                CustomerName  = customer.Name,
                //ContactName = contact?.LastName,
                InvoiceLines = new List <InvoiceToPdfInvoiceLineModel>()
            };

            foreach (var entityInvoiceLine in invoice.InvoiceLines.OrderBy(x => x.LineNumber))
            {
                //var item = entityInvoiceLine.ItemId.HasValue ?
                //    items.Single(x => x.Id == entityInvoiceLine.ItemId) :
                //    null;
                var modelInvoiceLine = new InvoiceToPdfInvoiceLineModel
                {
                    Quantity    = entityInvoiceLine.Quantity,
                    Description = entityInvoiceLine.Description,
                    //ItemDescription = item?.Description
                };
                invoiceToPdfModel.InvoiceLines.Add(modelInvoiceLine);
            }

            return(invoiceToPdfModel);
        }
Exemplo n.º 11
0
        public async Task <IEnumerable <ValidationMessage> > ValidateAsync(IDomainEntityContext <Customer> context, CancellationToken cancellationToken = default)
        {
            if (context.EditMode != EditMode.Delete)
            {
                return(ValidationResult.Ok());
            }

            var invoiceStatussesWhichAllowDeletionOfCustomer = new[] { InvoiceStatus.Cancelled };

            var hasInvoices = await _invoiceQuery.AsQueryable()
                              .Where(x => x.CustomerId == context.Entity.Id)
                              .Where(x => !invoiceStatussesWhichAllowDeletionOfCustomer.Contains(x.Status))
                              .AnyAsync(cancellationToken);

            if (hasInvoices)
            {
                return(ValidationResult.Invalid("Cannot delete customer, because one or more invoices are linked to this customer."));
            }

            return(ValidationResult.Ok());
        }
        public async Task <IEnumerable <ValidationMessage> > ValidateAsync(IDomainEntityContext <Role> context, CancellationToken cancellationToken = default)
        {
            if (context.EditMode == EditMode.Delete)
            {
                return(ValidationResult.Ok());
            }

            if (context.IsPropertyDirty(x => x.Name))
            {
                var alreadyExists = await _roleQuery.AsQueryable()
                                    .Where(x => x.Id != context.Entity.Id)
                                    .Where(x => x.Name == context.Entity.Name)
                                    .AnyAsync(cancellationToken);

                if (alreadyExists)
                {
                    return(ValidationResult.Invalid($"A role with name '{context.Entity.Name}' already exists."));
                }
            }

            return(ValidationResult.Ok());
        }