public async Task <IActionResult> InvokeAsync(ActionContext context, string parentRegionId)
        {
            var region = await _regionCache.GetRegionAsync(parentRegionId);

            if (region == null)
            {
                context.ModelState.AddModelError(nameof(parentRegionId), $"Could not find region with {nameof(parentRegionId)} \"{parentRegionId}\"");
                return(new BadRequestObjectResult(context.ModelState));
            }

            var childRegions = await _regionCache.GetRegionsByParentAsync(parentRegionId);

            return(new OkObjectResult(childRegions.Select(r => _regionMapper.MapToResult(r))));
        }
        public async Task <IActionResult> InvokeAsync(ActionContext context, RegionCreate create)
        {
            RegionEntity parentEntity = null;

            if (string.IsNullOrEmpty(create.ParentId))
            {
                parentEntity = await _coreDbContext.GetRegionByIdAsync(Constants.RootRegionId);
            }
            else
            {
                parentEntity = await _coreDbContext.GetRegionByIdAsync(create.ParentId);

                if (parentEntity == null)
                {
                    return(BadRequest(context, nameof(RegionCreate.ParentId), "Parent region not found"));
                }

                var parentLevel = parentEntity.GetLevel();
                if (parentLevel >= 5)
                {
                    return(BadRequest(context, nameof(RegionCreate.ParentId), "Max level region reached"));
                }
            }

            var entity = _regionMapper.MapToEntity(create);

            entity.ParentId = parentEntity.Id;

            var existingEntity = _coreDbContext.GetRegionByIdAsync(entity.Id);

            if (existingEntity != null)
            {
                return(BadRequest(
                           context,
                           $"{nameof(RegionCreate.ParentId)}+{nameof(RegionCreate.Name)}",
                           $"Region with id {entity.Id} already exists"));
            }

            await _coreDbContext.Regions.AddAsync(entity);

            await _coreDbContext.SaveChangesAsync();

            return(new CreatedAtActionResult(
                       "Get",
                       "Regions",
                       entity.Id,
                       _regionMapper.MapToResult(entity)));
        }
Esempio n. 3
0
        public async Task <IActionResult> InvokeAsync(ActionContext context, string regionId)
        {
            if (string.IsNullOrEmpty(regionId))
            {
                context.ModelState.AddModelError(nameof(regionId), "Region id was invalid");
                return(new BadRequestObjectResult(context.ModelState));
            }

            var entity = await _regionCache.GetRegionAsync(regionId);

            if (entity == null)
            {
                return(new NotFoundResult());
            }

            return(new OkObjectResult(_regionMapper.MapToResult(entity)));
        }