public async Task <IActionResult> CancelRentAsync(int id, string message, IFormFile document)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction("Index", "Profile", new { area = string.Empty })
                       .WithWarning(string.Empty, InvalidEntryData));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            var homeFromDb = await this.rentalService.GetDetailsAsync(id);

            var fileContents = await document.ToByteArrayAsync();

            var modelForDb = new RequestCreateServiceModel
            {
                Date     = DateTime.UtcNow,
                Type     = RequestType.CancelRent,
                UserId   = user.Id,
                Message  = message,
                HomeId   = homeFromDb.HomeId,
                Document = fileContents,
            };

            bool isCreated = await this.requestService.CreateRequestAsync(modelForDb);

            if (isCreated)
            {
                return(this.RedirectToAction("Index", "Profile", new { area = string.Empty })
                       .WithSuccess(string.Empty, SuccessfullySubmittedRequest));
            }

            return(this.RedirectToAction("Index", "Profile", new { area = string.Empty })
                   .WithDanger(string.Empty, FailedToSubmitRequest));
        }
Пример #2
0
        public async Task <bool> CreateRequestAsync(RequestCreateServiceModel model)
        {
            var request = new Request
            {
                Date     = model.Date,
                Type     = model.Type,
                UserId   = model.UserId,
                HomeId   = model.HomeId,
                Message  = model.Message,
                Document = model.Document,
                Status   = (RequestStatus)Enum.Parse(typeof(RequestStatus), NA),
            };

            if (request == null)
            {
                return(false);
            }

            await this.context.Requests.AddAsync(request);

            int result = await this.context.SaveChangesAsync();

            if (result > 0)
            {
                return(true);
            }

            return(false);
        }
Пример #3
0
        [Fact] // 1. async Task<bool> CreateRequestAsync(RequestCreateServiceModel model)
        public async void CreateRequestAsync_WithGivenRequestModel_ShouldReturnTrueIfCreated()
        {
            // Arrange
            var model = new RequestCreateServiceModel
            {
                Date     = DateTime.UtcNow,
                Type     = RequestType.ToRent,
                UserId   = Guid.NewGuid().ToString(),
                HomeId   = Guid.NewGuid().ToString(),
                Message  = Guid.NewGuid().ToString(),
                Document = new byte[1024],
            };

            var service = new RequestService(this.Context);

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

            // Assert
            result.Should().BeTrue();
        }
        public async Task <IActionResult> RequestFormAsync(string id, string about, string phoneNumber, string message, IFormFile document)
        {
            if (!this.User.Identity.IsAuthenticated)
            {
                return(this.RedirectToAction("Login", "Account", new { area = "Identity" })
                       .WithDanger(string.Empty, YouMustBeLoggedIn));
            }

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

            var homeFromDb = await this.listingService.GetDetailsAsync(id);

            if (!this.ModelState.IsValid)
            {
                var viewModel = new RequestFormInputModel
                {
                    PropertyDetails = homeFromDb,
                    User            = user,
                };

                return(this.View(viewModel));
            }

            if (document.Length > RequestDocumentMaxSize)
            {
                this.TempData.AddErrorMessage(FileTooLarge);
                var viewModel = new RequestFormInputModel
                {
                    PropertyDetails = homeFromDb,
                    User            = user,
                };

                return(this.View(viewModel));
            }

            if (
                (string.IsNullOrWhiteSpace(about) && string.IsNullOrWhiteSpace(user.About)) ||
                (string.IsNullOrWhiteSpace(phoneNumber) && string.IsNullOrWhiteSpace(user.PhoneNumber)) ||
                string.IsNullOrWhiteSpace(message))
            {
                this.TempData.AddErrorMessage(EmptyFields);
                var viewModel = new RequestFormInputModel
                {
                    PropertyDetails = homeFromDb,
                    User            = user,
                };

                return(this.View(viewModel));
            }

            if (!string.IsNullOrWhiteSpace(about))
            {
                user.About = about;
                await this.userManager.UpdateAsync(user);
            }

            if (!string.IsNullOrWhiteSpace(phoneNumber))
            {
                user.PhoneNumber = phoneNumber;
                await this.userManager.UpdateAsync(user);
            }

            var fileContents = await document.ToByteArrayAsync();

            var modelForDb = new RequestCreateServiceModel
            {
                Date     = DateTime.UtcNow,
                Type     = (RequestType)homeFromDb.Status,
                UserId   = userId,
                Message  = message,
                HomeId   = homeFromDb.Id,
                Document = fileContents,
            };

            bool isCreated = await this.requestService.CreateRequestAsync(modelForDb);

            if (isCreated)
            {
                return(this.RedirectToAction(nameof(HomeController.Index), "Home", new { area = string.Empty })
                       .WithSuccess(string.Empty, SuccessfullySubmittedRequest));
            }

            return(this.View());
        }