Example #1
0
        [Fact] // async Task<bool> EditListingAsync(OwnerEditListingServiceModel model)
        public async Task EditHomeStatusAsync_WithGivenRequestObject_ShouldChangeStatusAndReturnString()
        {
            // Arrange
            string       newName        = "New house on the block";
            string       newDescription = "Well maintained house close to the beach";
            HomeStatus   newStatus      = HomeStatus.Rented;
            HomeCategory newCategory    = HomeCategory.Room;

            var country = CountryCreator.Create();
            var city    = CityCreator.Create(country.Id);
            var image   = ImageCreator.CreateForModel();
            var home    = HomeCreator.CreateAny(city.Id);

            await this.Context.Countries.AddAsync(country);

            await this.Context.Cities.AddAsync(city);

            await this.Context.Homes.AddAsync(home);

            await this.Context.CloudImages.AddAsync(image);

            await this.Context.SaveChangesAsync();

            var model = new OwnerEditListingServiceModel
            {
                Id          = home.Id,
                Name        = newName,
                Description = newDescription,
                Price       = 1000m,
                Status      = newStatus,
                Category    = newCategory,
                Image       = image,
            };

            var service = new OwnerListingService(this.Context, null, null, null);

            // Act
            var savedEntry = await this.Context.Homes.Where(h => h.Id == home.Id).FirstOrDefaultAsync();

            var result = await service.EditListingAsync(model);

            var expected = true;

            // Assert
            result.Should().Be(true);
            result.Should().Equals(expected);

            savedEntry.Should().NotBeNull();
            savedEntry.Id.Should().Be(model.Id);
            savedEntry.Name.Should().Match(model.Name);
            savedEntry.Description.Should().Match(model.Description);
            savedEntry.Price.Should().Be(model.Price);
            savedEntry.Status.Should().Be(model.Status);
            savedEntry.Category.Should().Be(model.Category);
            savedEntry.Images.Select(i => i.PictureUrl).FirstOrDefault()
            .Should()
            .Match(model.Image.PictureUrl);
        }
        public async Task <bool> EditListingAsync(OwnerEditListingServiceModel model)
        {
            var home = await this.context.Homes.FirstOrDefaultAsync(h => h.Id == model.Id);

            home.Name        = model.Name;
            home.Description = model.Description;
            home.Price       = model.Price;
            home.Category    = model.Category;
            home.Status      = model.Status;

            if (model.Image != null)
            {
                home.Images.Add(model.Image);
            }

            this.context.Update(home);
            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <IActionResult> EditPostAsync(OwnerListingEditInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var userId = this.userManager.GetUserId(this.User);
            var user   = await this.userManager.FindByIdAsync(userId);

            var    homeEditModel = new OwnerEditListingServiceModel();
            string imgUrl        = string.Empty;
            string imgPubId      = string.Empty;

            var files = this.HttpContext.Request.Form.Files;

            if (model.Image != null)
            {
                var imgResult = await this.imageService
                                .UploadImageAsync(model.Image);

                imgUrl   = imgResult.SecureUri.AbsoluteUri;
                imgPubId = imgResult.PublicId;

                var imageToWrite = new CloudImage
                {
                    PictureUrl      = imgUrl,
                    PicturePublicId = imgPubId,
                };

                homeEditModel = new OwnerEditListingServiceModel
                {
                    Id          = model.Id,
                    Name        = model.Name,
                    Description = model.Description,
                    Price       = model.Price,
                    Category    = model.Category,
                    Status      = model.Status,
                    Image       = imageToWrite,
                };
            }
            else
            {
                homeEditModel = new OwnerEditListingServiceModel
                {
                    Id          = model.Id,
                    Name        = model.Name,
                    Description = model.Description,
                    Price       = model.Price,
                    Category    = model.Category,
                    Status      = model.Status,
                };
            }

            bool isEdited = await this.listingService.EditListingAsync(homeEditModel);

            if (!isEdited)
            {
                return(this.RedirectToAction("Details", "Listings", new { id = model.Id, Area = ManagementArea })
                       .WithWarning(string.Empty, CouldNotUpdateRecord));
            }

            await this.imageDbService.WriteToDatabasebAsync(imgUrl, imgPubId);

            return(this.RedirectToAction("Details", "Listings", new { id = model.Id, area = ManagementArea })
                   .WithSuccess(string.Empty, RecordUpdatedSuccessfully));
        }