public async Task <bool> CreateDealershipAsync(CreateDealershipInputModel input, ApplicationUser user, string picturePath) { var dealership = new Dealership { Name = input.DealershipName, DealerSince = DateTime.UtcNow, Location = input.Location + " " + input.FullAddress, PhoneNumber = input.PhoneNumber, Description = input.Description, Stars = 0, User = user, UserId = user.Id, }; // /wwwroot/images/dealerships/jhdsi-343g3h453-=g34g.jpg Directory.CreateDirectory($"{picturePath}/dealerships/"); var extension = Path.GetExtension(input.LogoPicture.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new Exception($"Invalid picture extension {extension}"); } var dbPicture = new Picture { Extension = extension, }; dealership.LogoPicture = dbPicture; var physicalPath = $"{picturePath}/dealerships/{dbPicture.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await input.LogoPicture.CopyToAsync(fileStream); if (dealership != null) { await this.dealershipRepository.AddAsync(dealership); await this.dealershipRepository.SaveChangesAsync(); return(true); } throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionWhileCreatingDealership); }
public async Task CreateDealershipShouldWorkWithCorrectData() { var dealerships = new List <Dealership>(); this.mockDealershipRepository.Setup(r => r.AllAsNoTracking()).Returns(() => dealerships.AsQueryable()); this.mockDealershipRepository.Setup(r => r.AddAsync(It.IsAny <Dealership>())).Callback((Dealership dealership) => dealerships.Add(dealership)); var fileMock = new Mock <IFormFile>(); var content = "Hello World from a Fake File"; var fileName = "test.jpg"; var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(content); writer.Flush(); ms.Position = 0; fileMock.Setup(_ => _.OpenReadStream()).Returns(ms); fileMock.Setup(_ => _.FileName).Returns(fileName); fileMock.Setup(_ => _.Length).Returns(ms.Length); var file = fileMock.Object; var model = new CreateDealershipInputModel { DealershipName = "autoa", Location = "sofiq", PhoneNumber = "088", Description = "desc", LogoPicture = file, }; var appUser = new ApplicationUser { Email = "*****@*****.**", }; var result = await this.mockService.CreateDealershipAsync(model, appUser, "wwwroot/images/dealerships/"); Assert.Single(dealerships); Assert.True(result); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= this.Url.Content("~/"); this.ExternalLogins = (await this.signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (this.ModelState.IsValid) { var user = new ApplicationUser { UserName = this.Input.Email, Email = this.Input.Email, PhoneNumber = this.Input.PhoneNumber }; var result = await this.userManager.CreateAsync(user, this.Input.Password); if (this.Input.Type == "dealership") { var dealershipInputModel = new CreateDealershipInputModel { DealershipName = this.Input.DealershipName, Email = this.Input.Email, Location = this.Input.Location, FullAddress = this.Input.FullAddress, PhoneNumber = this.Input.PhoneNumber, LogoPicture = this.Input.LogoPicture, }; var dealershipUser = this.userManager.FindByEmailAsync(dealershipInputModel.Email).Result; await this.dealershipsService.CreateDealershipAsync(dealershipInputModel, dealershipUser, $"{this.environment.WebRootPath}/images"); await this.userManager.AddToRoleAsync(dealershipUser, "Dealership"); } if (result.Succeeded) { this.logger.LogInformation("User created a new account with password."); var code = await this.userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = this.Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: this.Request.Scheme); await this.emailSender.SendEmailAsync(this.Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (this.userManager.Options.SignIn.RequireConfirmedAccount) { return(this.RedirectToPage("RegisterConfirmation", new { email = this.Input.Email, returnUrl = returnUrl })); } else { await this.signInManager.SignInAsync(user, isPersistent : false); return(this.LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { this.ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(this.Page()); }