Exemplo n.º 1
0
        public bool CreateRegion(RegionCreate model)
        {
            Region entity = new Region
            {
                LocationInRegion = model.LocationInRegion,
                RegionName       = model.RegionName
            };

            _context.Region.Add(entity);
            return(_context.SaveChanges() == 1);
        }
Exemplo n.º 2
0
        private static string GenerateRegionId(RegionCreate create)
        {
            var id = string.Empty;

            if (!string.IsNullOrEmpty(create.ParentId))
            {
                id += create.ParentId + "/";
            }
            id += create.Name.ToLower().Replace(' ', '-');
            return(id);
        }
Exemplo n.º 3
0
        public static async Task <Region> CreateRegionAndAssertCreatedAsync(
            this HttpClient client,
            RegionCreate create)
        {
            var content = new StringContent(JsonConvert.SerializeObject(create), Encoding.UTF8, "application/json");

            var response = await client.PostAsync("/regions", content);

            response.AssertStatusCode(HttpStatusCode.Created);

            return(await response.DeserializeRegionAsync());
        }
Exemplo n.º 4
0
        public IHttpActionResult Post(RegionCreate region)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var service = CreateRegionService();

            if (!service.CreateRegion(region))
            {
                return(InternalServerError());
            }
            return(Ok());
        }
        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)));
        }
Exemplo n.º 6
0
        // [Authorize(Roles = "Admin")]
        public bool CreateRegion(RegionCreate model)
        {
            var entity =
                new Region()
            {
                OwnerId     = _regionId,
                Name        = model.Name,
                Description = model.Description,
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Regions.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
Exemplo n.º 7
0
        public ActionResult Create(RegionCreate model)
        {
            if (ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateRegionService();

            if (service.CreateRegion(model))
            {
                TempData["SaveResult"] = "Your Region was added.";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "The Region could not be added.");

            return(View(model));
        }
Exemplo n.º 8
0
 public bool CreateRegion(RegionCreate model)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 9
0
 public RegionEntity MapToEntity(RegionCreate create) =>
 _mapper.Map <RegionEntity>(create);