Exemplo n.º 1
0
        public async Task <IActionResult> UpdateMetaPage(long id,
                                                         [FromBody] MetaPageForUpdateDto metaPageForUpdate)
        {
            if (metaPageForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
            }

            if (Regex.Matches(metaPageForUpdate.PageName, @"[a-zA-Z0-9 ]").Count < metaPageForUpdate.PageName.Length)
            {
                ModelState.AddModelError("Message", "Page name contains invalid characters (Enter A-Z, a-z, 0-9, space)");
                return(BadRequest(ModelState));
            }

            if (!string.IsNullOrWhiteSpace(metaPageForUpdate.PageDefinition))
            {
                if (Regex.Matches(metaPageForUpdate.PageDefinition, @"[-a-zA-Z0-9 .,]").Count < metaPageForUpdate.PageDefinition.Length)
                {
                    ModelState.AddModelError("Message", "Page definition contains invalid characters (Enter A-Z, a-z, 0-9, space, hyphen, period, comma)");
                    return(BadRequest(ModelState));
                }
            }

            if (!string.IsNullOrWhiteSpace(metaPageForUpdate.Breadcrumb))
            {
                if (Regex.Matches(metaPageForUpdate.Breadcrumb, @"[-a-zA-Z0-9 .,]").Count < metaPageForUpdate.Breadcrumb.Length)
                {
                    ModelState.AddModelError("Message", "Bread crumb contains invalid characters (Enter A-Z, a-z, 0-9, space, hyphen, period, comma)");
                    return(BadRequest(ModelState));
                }
            }

            if (_unitOfWork.Repository <MetaPage>().Queryable().
                Where(l => l.PageName == metaPageForUpdate.PageName && l.Id != id)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
            }

            var metaPageFromRepo = await _metaPageRepository.GetAsync(f => f.Id == id);

            if (metaPageFromRepo == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                metaPageFromRepo.PageName       = metaPageForUpdate.PageName;
                metaPageFromRepo.PageDefinition = metaPageForUpdate.PageDefinition;
                metaPageFromRepo.Breadcrumb     = metaPageForUpdate.Breadcrumb;
                metaPageFromRepo.IsVisible      = (metaPageForUpdate.Visible == Models.ValueTypes.YesNoValueType.Yes);

                _metaPageRepository.Update(metaPageFromRepo);
                await _unitOfWork.CompleteAsync();

                return(Ok());
            }

            return(BadRequest(ModelState));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateMetaPage(
            [FromBody] MetaPageForUpdateDto metaPageForUpdate)
        {
            if (metaPageForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
            }

            if (Regex.Matches(metaPageForUpdate.PageName, @"[a-zA-Z0-9 ]").Count < metaPageForUpdate.PageName.Length)
            {
                ModelState.AddModelError("Message", "Page name contains invalid characters (Enter A-Z, a-z, 0-9, space)");
                return(BadRequest(ModelState));
            }

            if (!string.IsNullOrWhiteSpace(metaPageForUpdate.PageDefinition))
            {
                if (Regex.Matches(metaPageForUpdate.PageDefinition, @"[-a-zA-Z0-9 .,]").Count < metaPageForUpdate.PageDefinition.Length)
                {
                    ModelState.AddModelError("Message", "Page definition contains invalid characters (Enter A-Z, a-z, 0-9, space, hyphen, period, comma)");
                    return(BadRequest(ModelState));
                }
            }

            if (!string.IsNullOrWhiteSpace(metaPageForUpdate.Breadcrumb))
            {
                if (Regex.Matches(metaPageForUpdate.Breadcrumb, @"[-a-zA-Z0-9 .,]").Count < metaPageForUpdate.Breadcrumb.Length)
                {
                    ModelState.AddModelError("Message", "Bread crumb contains invalid characters (Enter A-Z, a-z, 0-9, space, hyphen, period, comma)");
                    return(BadRequest(ModelState));
                }
            }

            if (_unitOfWork.Repository <MetaPage>().Queryable().
                Where(l => l.PageName == metaPageForUpdate.PageName)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
            }

            if (ModelState.IsValid)
            {
                var newMetaPage = new MetaPage()
                {
                    PageName       = metaPageForUpdate.PageName,
                    PageDefinition = metaPageForUpdate.PageDefinition,
                    Breadcrumb     = metaPageForUpdate.Breadcrumb,
                    IsSystem       = false,
                    MetaDefinition = string.Empty,
                    MetaPageGuid   = Guid.NewGuid(),
                    IsVisible      = (metaPageForUpdate.Visible == Models.ValueTypes.YesNoValueType.Yes)
                };

                _metaPageRepository.Save(newMetaPage);

                var mappedMetaPage = await GetMetaPageAsync <MetaPageIdentifierDto>(newMetaPage.Id);

                if (mappedMetaPage == null)
                {
                    return(StatusCode(500, "Unable to locate newly added item"));
                }

                return(CreatedAtAction("GetMetaPageByIdentifier",
                                       new
                {
                    id = mappedMetaPage.Id
                }, CreateLinksForMetaPage <MetaPageIdentifierDto>(mappedMetaPage)));
            }

            return(BadRequest(ModelState));
        }