Ejemplo n.º 1
0
        public bool ValidateGetCollection(
            OfmForGetCollectionQueryResult <TOfmForGet> ofmForGetCollectionQueryResult,
            out ObjectResult objectResult)
        {
            objectResult = null;
            if (ofmForGetCollectionQueryResult.ErrorMessages.Count() > 0)
            {
                foreach (var errorMessage in ofmForGetCollectionQueryResult.ErrorMessages)
                {
                    _controller.ModelState.AddModelError(_shortCamelCasedControllerName, errorMessage);
                }
                objectResult = new UnprocessableEntityObjectResult(_controller.ModelState);
                return(false);
            }

            var allOfmForGet = ofmForGetCollectionQueryResult.ReturnedTOfmForGetCollection;

            if (allOfmForGet.OfmForGets.Count() == 0)
            {
                _controller.ModelState.AddModelError(_shortCamelCasedControllerName, $"No {_shortCamelCasedControllerName.ToPlural()} found");
                objectResult = new EntityNotFoundObjectResult(_controller.ModelState);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static void AddPaginationMetadata <TId, TOfmForGet>(this Controller controller,
                                                                   OfmForGetCollectionQueryResult <TOfmForGet> tOfmForGetCollectionQueryResult,
                                                                   IncomingHeaders incomingHeaders,
                                                                   IDictionary <string, object> resourceParametersAsDictionary,
                                                                   IUrlHelper urlHelper,
                                                                   string controllerName)
            where TOfmForGet : class, IOfmForGet
            where TId : struct
        {
            if (incomingHeaders.IncludeHateoas)
            {
                var paginationMetadata = new
                {
                    totalCount  = tOfmForGetCollectionQueryResult.TotalCount,
                    pageSize    = tOfmForGetCollectionQueryResult.PageSize,
                    currentPage = tOfmForGetCollectionQueryResult.CurrentPage,
                    totalPages  = tOfmForGetCollectionQueryResult.TotalPages
                };
                controller.Response.Headers.Add("X-Pagination",
                                                Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));
            }
            else
            {
                var hateoasLinks = new HateoasLinkFactory <TId>(urlHelper, controllerName).CreateLinksForOfmGetGeneric(
                    resourceParametersAsDictionary,
                    tOfmForGetCollectionQueryResult.HasPrevious,
                    tOfmForGetCollectionQueryResult.HasNext);

                var paginationMetadata = new
                {
                    totalCount   = tOfmForGetCollectionQueryResult.TotalCount,
                    pageSize     = tOfmForGetCollectionQueryResult.PageSize,
                    currentPage  = tOfmForGetCollectionQueryResult.CurrentPage,
                    totalPages   = tOfmForGetCollectionQueryResult.TotalPages,
                    previousPage = hateoasLinks.FirstOrDefault(w => w.Rel == "previousPage")?.Href,
                    nextPage     = hateoasLinks.FirstOrDefault(w => w.Rel == "nextPage")?.Href
                };
                controller.Response.Headers.Add("X-Pagination",
                                                Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));
            }
        }
Ejemplo n.º 3
0
        //public virtual async Task<OfmForGetCollectionQueryResult<TOfmForGet>> GetCollection<TOfmResourceParameters1>(TOfmResourceParameters1 ofmResourceParameters, Guid ownerGuid)
        //    where TOfmResourceParameters1 : class, IResourceParameters
        //{
        //    throw new NotImplementedException();
        //}

        public virtual async Task <OfmForGetCollectionQueryResult <TOfmForGet> > GetCollection <TOfmResourceParameters>(TOfmResourceParameters ofmResourceParameters, Guid ownerGuid)
            where TOfmResourceParameters : OfmResourceParametersBase
        {
            var ofmForGetCollectionQueryResult = new OfmForGetCollectionQueryResult <TOfmForGet>();

            ofmForGetCollectionQueryResult = await AsyncGetOfmGuardClause.ValidateResourceParameters(ofmForGetCollectionQueryResult, ofmResourceParameters);

            if (ofmForGetCollectionQueryResult.ErrorMessages.Count > 0)
            {
                return(ofmForGetCollectionQueryResult);
            }

            var entityResourceParameters = Mapper.Map <TEntityCollectionResourceParameters>(ofmResourceParameters);

            entityResourceParameters.OwnerGuid = ownerGuid;
            entityResourceParameters.OrderBy   = ofmResourceParameters.OrderBy.ToEntityOrderBy(PropertyMappingService.GetPropertyMapping <TOfmForGet, TEntity>());

            var result = await Repo.GetPagedCollection(entityResourceParameters);

            var pagedListEntityCollection = result.CopyPropertyValuesTo(ofmForGetCollectionQueryResult);

            ofmForGetCollectionQueryResult.ReturnedTOfmForGetCollection.OfmForGets = Mapper.Map <List <TEntity>, List <TOfmForGet> >(pagedListEntityCollection);
            return(ofmForGetCollectionQueryResult);
        }
Ejemplo n.º 4
0
        public async Task <OfmForGetCollectionQueryResult <TOfmForGet> > ValidateResourceParameters(OfmForGetCollectionQueryResult <TOfmForGet> ofmForGetCollectionQueryResult, OfmResourceParametersBase resourceParameters)
        {
            await Task.Run(() =>
            {
                var errorMessages = new List <string>();

                var idsAreCorrectlySyntaxed = new ValidRegExRangeOfIntIdsAttribute(FittifyRegularExpressions.RangeOfIntIds);
                if (!idsAreCorrectlySyntaxed.IsValid(resourceParameters.Ids))
                {
                    ofmForGetCollectionQueryResult.ErrorMessages.Add(idsAreCorrectlySyntaxed.FormatErrorMessage(null));
                }

                var idsInAscendingOrderValidation = new ValidAscendingOrderRangeOfIntIdsAttribute();
                if (!idsInAscendingOrderValidation.IsValid(resourceParameters.Ids))
                {
                    ofmForGetCollectionQueryResult.ErrorMessages.Add(idsInAscendingOrderValidation.FormatErrorMessage(null));
                }

                errorMessages = new List <string>();
                if (!_typeHelperService.TypeHasProperties <TOfmForGet>(resourceParameters.OrderBy, ref errorMessages))
                {
                    ofmForGetCollectionQueryResult.ErrorMessages.AddRange(errorMessages);
                }

                errorMessages = new List <string>();
                if (!_typeHelperService.TypeHasProperties <TOfmForGet>(resourceParameters.Fields, ref errorMessages))
                {
                    ofmForGetCollectionQueryResult.ErrorMessages.AddRange(errorMessages);
                }

                ofmForGetCollectionQueryResult.ErrorMessages =
                    ofmForGetCollectionQueryResult.ErrorMessages.Distinct().ToList();
            });

            return(ofmForGetCollectionQueryResult);
        }