Beispiel #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."));
            }
        }