Example #1
0
        public async Task <ActionResult <RegionAdd> > AddRegionInfo([FromBody] RegionAdd regionToAdd)
        {
            var regModel = _mapper.Map <Region>(regionToAdd);
            var temp     = await _service.QueryRegionName(regionToAdd.Name);

            List <Region> duplicate = null;
            var           rName     = regionToAdd.Name.ToUpper();

            if (temp.Any())
            {
                foreach (var item in temp)
                {
                    if (item.Name.ToUpper() == rName)
                    {
                        duplicate.Add(item);
                    }
                }
            }
            if (duplicate == null)
            {
                try
                {
                    _service.AddNewRegion(regModel);
                    _service.SaveChanges();
                }
                catch (Exception)
                {
                    return(BadRequest("Duplicate RegionCode detected."));
                }

                var regReadDto = _mapper.Map <RegionView>(regModel);

                return(CreatedAtRoute(
                           "SearchByRegCode",
                           new { regionCode = regReadDto.RegionCode },
                           regReadDto
                           ));
            }
            else
            {
                return(BadRequest($"{regionToAdd.Name} already exists."));
            }
        }
        public ActionResult RegionAdd(int id, RegionAdd newItem)
        {
            // Validate the input
            // Two conditions must be checked
            if (!ModelState.IsValid && id == newItem.CountryId)
            {
                return(View(newItem));
            }

            // Process the input
            var addedItem = AdminManager.AddRegion(newItem);

            if (addedItem == null)
            {
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("CountryDetails", new { id = id }));
            }
        }
Example #3
0
        public RegionBase AddRegion(RegionAdd newItem)
        {
            // get associated object
            var o = CurrentDbContext.CountryDB.Include("Regions").SingleOrDefault(a => a.Id == newItem.CountryId);

            if (o == null)
            {
                return(Mapper.Map <RegionBase>(newItem));
            }

            // check for duplicate region
            var dup = o.Regions.SingleOrDefault(a => a.Name == newItem.Name);

            if (!o.Regions.Any(a => a.Name == newItem.Name))
            {
                var addedItem = CurrentDbContext.RegionDB.Add(Mapper.Map <Region>(newItem));
                addedItem.Country = o;
                addedItem.SeoName = SeoManager.GetSeoLocation(addedItem.Country.Name, addedItem.Name);
                o.RegionCount++;
                CurrentDbContext.SaveChanges();
                return((addedItem == null) ? null : Mapper.Map <RegionBase>(addedItem));
            }
            return(Mapper.Map <RegionBase>(newItem));
        }