public static GeoTaskListQuery ToGeoTaskListQuery
            (this ApiGeoTaskListQuery from, ClaimsPrincipal currentPrincipal)
        {
            if (from is null)
            {
                return(null);
            }

            var result = new GeoTaskListQuery
            {
                ActorId          = from.ActorId,
                ActorRoleMask    = from.ActorRoleMask,
                Archived         = from.Archived,
                CurrentPrincipal = currentPrincipal,
                Limit            = from.Limit,
                Offset           = from.Offset,
                ProjectId        = from.ProjectId,
                TaskStatusMask   = from.TaskStatusMask,
                СontainsKeyWords = from.ContainsKeyWords
            };

            if (TimeSpan.TryParse(from.MaxTimeToDeadLine,
                                  out var deadlineTimeSpan))
            {
                result.MaxTimeToDeadLine = deadlineTimeSpan;
            }

            return(result);
        }
        public static Dictionary <string, object> ToDictionary
            (this GeoTaskListQuery from)
        {
            if (from is null)
            {
                return(null);
            }

            return(new Dictionary <string, object>
            {
                { nameof(from.ActorId), from.ActorId },
                { nameof(from.ActorRoleMask), from.ActorRoleMask },
                { nameof(from.Archived), from.Archived },
                {
                    nameof(from.CurrentPrincipal),
                    from.CurrentPrincipal
                    .ToDictionary()
                    .ToList()
                    .Select(x => $"\"{x.Key}\"={x.Value}")
                },
                { nameof(from.Limit), from.Limit },
                { nameof(from.MaxTimeToDeadLine),
                  from.MaxTimeToDeadLine?.ToString() },
                { nameof(from.Offset), from.Offset },
                { nameof(from.ProjectId), from.ProjectId },
                { nameof(from.TaskStatusMask), from.TaskStatusMask },
                { nameof(from.СontainsKeyWords), from.СontainsKeyWords },
                { nameof(from.GeoIds), String.Join(',', from.GeoIds) },
            });
        }
Exemple #3
0
        public async Task <ListResponse <GeoTask> > Handle
            (GeoTaskListQuery request, CancellationToken cancellationToken)
        {
            Logger.LogInformation(AppLogEvent.HandleRequest,
                                  "Handle get task collection {request}",
                                  request.ToDictionary());

            if (request is null)
            {
                Logger.LogWarning(AppLogEvent.HandleArgumentError,
                                  "Handle get task collection request with empty request");
                return(ErrorResponse("Request empty argument"));
            }

            try
            {
                // Validate request
                var validator        = new GeoTaskListQueryValidator();
                var validationResult = await validator.ValidateAsync(request)
                                       .ConfigureAwait(false);

                if (!validationResult.IsValid)
                {
                    Logger.LogError("Query validation error. " +
                                    "Request={Request}. Error={Error}.",
                                    request.ToDictionary(),
                                    validationResult.Errors.Select(x => x.ErrorMessage));
                    return(ErrorResponse
                               (validationResult.Errors.Select(x => x.ErrorMessage)));
                }

                // Build filter
                var defaultLimit = Configuration.GetValue
                                       (Defaults.ConfigurationApiDefaultLimitParameterName,
                                       Defaults.ConfigurationApiDefaultLimitDefaultValue);
                var maxLimit = Configuration.GetValue
                                   (Defaults.ConfigurationApiMaxLimitParameterName,
                                   Defaults.ConfigurationApiMaxLimitDefaultValue);
                var filter = new DbGetGeoTaskListRequest
                {
                    Offset = request.Offset,
                    Limit  = request.Limit == 0
                        ? defaultLimit
                        : Math.Min(request.Limit, maxLimit),
                    Archived          = request.Archived,
                    Contains          = request.СontainsKeyWords,
                    MaxTimeToDeadLine = request.MaxTimeToDeadLine,
                    TaskStatusMask    = request.TaskStatusMask,
                };
                if (!String.IsNullOrWhiteSpace(request.ActorId))
                {
                    filter.Actors[request.ActorId] = request.ActorRoleMask;
                }
                if (!string.IsNullOrWhiteSpace(request.ProjectId))
                {
                    filter.ProjectIds.Add(request.ProjectId);
                }
                filter.GeoIds.AddRange(request.GeoIds);

                // Get Actor for current user by user name
                var currentActorResponse = await Mediator
                                           .Send(new DbGetActorByNameRequest
                                                 (request.CurrentPrincipal?.Identity?.Name))
                                           .ConfigureAwait(false);

                Actor currentActor = null;
                if (currentActorResponse.Success)
                {
                    currentActor = currentActorResponse.Entity;
                }
                else
                {
                    Logger.LogWarning(AppLogEvent.HandleArgumentError,
                                      "Not found current actor");
                    return(ErrorResponse("Not found current actor"));
                }

                ActorRole projectActorRole = null;
                if (!string.IsNullOrWhiteSpace(request.ProjectId))
                {
                    projectActorRole = await GetProjectRoleAsync
                                           (request.ProjectId, currentActor?.Id)
                                       .ConfigureAwait(false);
                }

                // Apply security filter
                var securizator = new DbGetGeoTaskListRequestSecurityBuilder
                                      (filter, currentActor, projectActorRole);
                var securedFilter = securizator.Build();

                return(await Mediator.Send(securedFilter)
                       .ConfigureAwait(false));
            }
            catch (Exception e)
            {
                Logger.LogError(AppLogEvent.HandleErrorResponse, e,
                                "Call repository exception");
                return(ErrorResponse("Not found"));
            }
        }