Exemplo n.º 1
0
        public async Task <IActionResult> AddLocation([FromBody] LocationForCreationDto locationForCreationDto)
        {
            // note: use regex for street address abbreviations
            var locationExists = await _context.Locations
                                 .FirstOrDefaultAsync(x => x.Name.ToLower().Replace(" ", String.Empty) == locationForCreationDto.Name.ToLower().Replace(" ", String.Empty) &&
                                                      x.Address.ToLower().Replace(" ", String.Empty) == locationForCreationDto.Address.ToLower().Replace(" ", String.Empty) &&
                                                      x.City.ToLower().Replace(" ", String.Empty) == locationForCreationDto.City.ToLower().Replace(" ", String.Empty) &&
                                                      x.State.ToLower().Replace(" ", String.Empty) == locationForCreationDto.State.ToLower().Replace(" ", String.Empty)
                                                      );

            if (locationExists == null)
            {
                var location = locationForCreationDto.Adapt <Location>();

                location.AdminApproved = false;

                _context.Locations.Add(location);

                if (await _context.SaveChangesAsync() > 0)
                {
                    return(Ok(location));
                }

                return(BadRequest("something went wrong"));
            }
            else
            {
                return(BadRequest("Location already exists"));
            }
        }
        public async void AddLocation_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var locationForCreationDto = new LocationForCreationDto
            {
                Name = "Warsaw"
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.AddLocationAsync(Arg.Any <Location>())
            .Returns(_ =>
            {
                var locationToAdd = _.Arg <Location>();
                locationToAdd.Id  = 55;
                var tcs           = new TaskCompletionSource <Location>();
                tcs.SetResult(locationToAdd);
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >());
            var result = await sit.AddLocation(locationForCreationDto);

            // Assert
            var addedLocation = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, LocationDto>(result);

            Assert.Equal(locationForCreationDto.Name, addedLocation.Name);
            Assert.Equal(55, addedLocation.Id);

            WebApiTestsHelper.Unlock();
        }
        public ActionResult <LocationDto> Createlocation(LocationForCreationDto location)
        {
            var locationEntity = _mapper.Map <Location>(location);

            _locationRepository.AddLocation(locationEntity);
            _locationRepository.Save();

            var locationToReturn = _mapper.Map <LocationDto>(locationEntity);

            return(CreatedAtRoute("GetLocation",
                                  new { locationId = locationToReturn.Id },
                                  locationToReturn));
        }
        public IActionResult PutLocations([FromRoute] int id, LocationForCreationDto locations)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError("ModelState is failed.");
                return(BadRequest(ModelState));
            }

            var LocationForUpdate = _repoWrapper.Location.FindByCondition(x => x.Id == id).FirstOrDefault();

            if (LocationForUpdate == null)
            {
                _logger.LogError("null id in database");
                return(NotFound());
            }

            Locations newlocation = new Locations()
            {
                Id       = id,
                AreaCode = locations.AreaCode,
                Adrress  = locations.Adrress,
                Status   = locations.Status,
                Revenue  = locations.Revenue,
                Leader   = locations.Leader
            };
            var result = _mapper.Map(LocationForUpdate, locations);

            _repoWrapper.Location.Update(newlocation);



            try
            {
                _repoWrapper.Save();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LocationsExists(id))
                {
                    _logger.LogInfo($"Location is null");

                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> CreateLocation([FromBody] LocationForCreationDto locationForCreationDto)
        {
            var location = _mapper.Map <Location>(locationForCreationDto);

            _repo.Add(location);

            if (await _repo.SaveAll())
            {
                var locationToReturn = _mapper.Map <LocationToReturnDto>(location);
                return(CreatedAtRoute("GetLocation", new { locationId = location.Id }, locationToReturn));
            }

            return(BadRequest("Creating the location failed."));
        }
        public async void AddLocation_ShouldReturnBadRequest()
        {
            // Arrange
            var locationForCreationDto = new LocationForCreationDto();

            // Act
            var sit = new LocationsController(null, Substitute.For <ILogger <LocationsController> >());

            sit.ModelState.AddModelError("PropName", "ModelError");
            var result = await sit.AddLocation(locationForCreationDto);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
Exemplo n.º 7
0
        public async Task <IActionResult> AddLocation([FromBody] LocationForCreationDto locationForCreation)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogWarning("Bad request. Model state invalid: {@ModelState}", ModelState);
                return(BadRequest(ModelState));
            }

            var locationToAdd = Mapper.Map <LocationForCreationDto, Location>(locationForCreation);
            var addedLocation = await _locationDataService.AddLocationAsync(locationToAdd);

            var result = Mapper.Map <Location, LocationDto>(addedLocation);

            return(Ok(result));
        }
        public async Task <IActionResult> UpdateLocation(int locationId, [FromBody] LocationForCreationDto locationForCreationDto)
        {
            var locationFromRepo = await _repo.GetLocation(locationId);

            if (locationFromRepo == null)
            {
                return(NoContent());
            }

            _mapper.Map(locationForCreationDto, locationFromRepo);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Updating the location failed."));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> EditLocation([FromBody] LocationForCreationDto locationForCreationDto, int locationId)
        {
            Location location = await _context.Locations.Include(x => x.Photo).FirstOrDefaultAsync(x => x.Id == locationForCreationDto.Id);

            // Location location = locationForCreationDto.Adapt<Location>();

            location.Address   = locationForCreationDto.Address;
            location.Name      = locationForCreationDto.Name;
            location.City      = locationForCreationDto.City;
            location.State     = locationForCreationDto.State;
            location.Zip       = locationForCreationDto.Zip;
            location.Country   = locationForCreationDto.Country;
            location.Photo.Url = locationForCreationDto.PhotoUrl;
            location.Market    = (Market)Enum.Parse(typeof(Market), locationForCreationDto.MarketType);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(Ok(location));
            }

            return(BadRequest("something went wrong"));
        }