public async Task <KeyValuePair <bool, string> > AddLocation(LocationToAddDto location) { var city = await _unitOfWork.Cities.GetById(location.CityId); if (city == null) { return(new KeyValuePair <bool, string>(false, "City does not exist")); } var country = await _unitOfWork.Countries.GetById(location.CountryId); if (country == null) { return(new KeyValuePair <bool, string>(false, "Country does not exist")); } var locationToAdd = _mapper.Map <Location>(location); locationToAdd.City = city; locationToAdd.Country = country; _unitOfWork.Locations.Add(locationToAdd); if (await _unitOfWork.Complete()) { // TODO: Comment all Repository and Controller methods // Move messages to constants! return(new KeyValuePair <bool, string>(true, "Location created successfully!")); } return(new KeyValuePair <bool, string>(false, "Problem creating location")); }
public async Task AddLocation_ShouldBeDoneSuccessfully() { // Arrange var responseMessage = "Location created successfully!"; var locationToAddDto = new LocationToAddDto() { Name = "Jelovac", CityId = 1, CountryId = 1 }; var location = new Location() { Id = 1, City = new City(), CityId = locationToAddDto.CityId, Country = new Country(), CountryId = locationToAddDto.CountryId, Groups = new List <Group>(), Name = locationToAddDto.Name }; _unitOfWorkMock.Setup(x => x.Cities.GetById(It.IsAny <int>())) .ReturnsAsync(new City()); _unitOfWorkMock.Setup(x => x.Countries.GetById(It.IsAny <int>())) .ReturnsAsync(new Country()); _mapperMock.Setup(x => x.Map <Location>(locationToAddDto)) .Returns(location); _unitOfWorkMock.Setup(x => x.Locations.Add(location)) .Verifiable(); _unitOfWorkMock.Setup(x => x.Complete()) .ReturnsAsync(true); // Act var result = await _sut.AddLocation(locationToAddDto); // Assert Assert.Equal(responseMessage, result.Value); }
public async Task AddLocation_ShouldNotAdd_WhenCityDoesNotExist() { // Arrange var responseMessage = "City does not exist"; var locationToAddDto = new LocationToAddDto() { Name = "Jelovac", CityId = 1, CountryId = 1 }; _unitOfWorkMock.Setup(x => x.Cities.GetById(It.IsAny <int>())) .ReturnsAsync(() => null); // Act var result = await _sut.AddLocation(locationToAddDto); // Assert Assert.Equal(responseMessage, result.Value); }
public async Task <IActionResult> AddLocation(LocationToAddDto location) { return(Ok(await _locationsService.AddLocation(location))); }