public async Task <bool> CreateListingAsync(OwnerCreateListingServiceModel model)
        {
            var city = await this.context.Cities.FirstOrDefaultAsync(x => x.Id == model.CityId);

            Home home = new Home
            {
                Name        = model.Name,
                Description = model.Description,
                Address     = model.Address,
                Price       = model.Price,
                City        = city,
                Category    = model.Category,
                Status      = model.Status,
                Owner       = model.Owner,
            };

            home.Images.Add(model.Image);

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

            city.Homes.Add(home);
            await this.context.SaveChangesAsync();

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

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

            // Upload the Image
            var imgResult = await this.imageService
                            .UploadImageAsync(model.Image);

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

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

            var homeCreateServiceModel = new OwnerCreateListingServiceModel
            {
                Name        = model.Name,
                Description = model.Description,
                Address     = model.Address,
                Price       = model.Price,
                CityId      = model.CityId,
                Category    = model.Category,
                Status      = model.Status,
                Owner       = user,
                Image       = imageToWrite,
            };

            // Create Listing
            bool isCreated = await this.ownerListingService.CreateListingAsync(homeCreateServiceModel);

            if (!isCreated)
            {
                return(this.RedirectToAction("Index", "Dashboard", new { area = string.Empty })
                       .WithWarning(string.Empty, CouldNotCreateRecord));
            }

            // Add To Role
            await this.userManager.AddToRoleAsync(user, OwnerRoleName);

            // Referesh authorization since redirect requires the new role auth
            await this.signInManager.RefreshSignInAsync(user);

            // Write Image to DB
            await this.imageDbService.WriteToDatabasebAsync(imgUrl, imgPubId);

            return(this.RedirectToAction("Index", "Dashboard", new { area = ManagementArea })
                   .WithSuccess(string.Empty, RecordCreatedSuccessfully));
        }
Example #3
0
        [Fact] // async Task<bool> CreateListingAsync(OwnerCreateListingServiceModel model)
        public async Task CreateListingAsync_WithGivenInputModel_ShouldCreateOwnerListingAndReturnTrue()
        {
            // Arrange
            const string modelName        = "New house close to the beach.";
            const string modelDescription = "That we can tuck in our children at night and know that they are fed and clothed and safe from harm. I wanted to be part of something larger. And yet, it has only been in the last couple of weeks that the discussion of race in this campaign has taken a particularly divisive turn.";
            const string modelAddress     = "10, G.M. Dimitrov, 1700";

            var owner   = UserCreator.Create("Prasemir", "Butonoskov", "prasio", "*****@*****.**");
            var country = CountryCreator.Create();
            var city    = CityCreator.Create(country.Id);
            var image   = ImageCreator.CreateForModel();

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

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

            await this.Context.Users.AddAsync(owner);

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

            await this.Context.SaveChangesAsync();

            var model = new OwnerCreateListingServiceModel
            {
                Name        = modelName,
                Description = modelDescription,
                Address     = modelAddress,
                CityId      = city.Id,
                Price       = 1000,
                Status      = HomeStatus.ToRent,
                Category    = HomeCategory.House,
                Owner       = owner,
                Image       = image,
            };

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

            // Act
            var result = await service.CreateListingAsync(model);

            var savedEntry = await this.Context.Homes.Where(h => h.OwnerId == owner.Id).FirstOrDefaultAsync();

            var expected = true;

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

            savedEntry.Should().NotBeNull();
            savedEntry.Name.Should().Match(modelName);
            savedEntry.Description.Should().Match(modelDescription);
            savedEntry.OwnerId.Should().Match(owner.Id);
            savedEntry.Images.Any(i => i.PictureUrl == image.PictureUrl);
        }