예제 #1
0
        public async Task <bool> EditRealEstateAsync(RealEstateEditServiceModel model)
        {
            if (!this.context.RealEstates.Any(x => x.Id == model.Id))
            {
                throw new ArgumentNullException(UnexistingRealEstateMessage);
            }

            var realEstateToEdit = await this.context.RealEstates
                                   .Include(x => x.Address)
                                   .FirstOrDefaultAsync(x => x.Id == model.Id);

            //Edit Address nav properties
            var city = await this.citiesServices.GetByNameAsync(model.City);

            var neighbourhood = await this.neighbourhoodServices.GetNeighbourhoodByNameAsync(model.Neighbourhood);

            var village = await this.villageServices.CreateVillageAsync(model.Village);

            var addressId = realEstateToEdit.Address.Id;

            //Edit the address with already edited nav properties
            realEstateToEdit.Address = await this.addressServices.EditAddressAsync(addressId, city, model.Address, village, neighbourhood);

            realEstateToEdit.RealEstateType = await this.realEstateTypeServices.GetRealEstateTypeByNameAsync(model.RealEstateType);

            realEstateToEdit.BuildingType = await this.buildingTypeServices.GetBuildingTypeAsync(model.BuildingType);

            realEstateToEdit.HeatingSystem = await this.heatingSystemServices.GetHeatingSystemAsync(model.HeatingSystem);

            realEstateToEdit.PricePerSquareMeter = model.Price / (decimal)model.Area;
            realEstateToEdit.ModifiedOn          = DateTime.UtcNow;

            this.mapper.Map <RealEstateEditServiceModel, RealEstate>(model, realEstateToEdit);
            return(await UpdateRealEstateInTheDb(realEstateToEdit));
        }
        public void EditRealEstateShouldThrowAnExceptionIfNoSuchRealEstate()
        {
            var mapper = this.GetMapper();

            var nonExistingRealEstate = new RealEstateEditServiceModel
            {
                Id    = "Agent007",
                Price = 7500m,
                Area  = 7,
            };

            var serviceInstance = new RealEstateServices(context,
                                                         realEstateTypeServices.Object,
                                                         citiesServices.Object,
                                                         neighbourhoodServices.Object,
                                                         addressServices.Object,
                                                         villageServices.Object,
                                                         buildingTypeServices.Object,
                                                         heatingSystemServices.Object,
                                                         mapper);

            Assert.ThrowsAsync <ArgumentNullException>(async() => await serviceInstance.EditRealEstateAsync(nonExistingRealEstate), NonExistingRealEstateMessage);
        }