public async Task <ActionResult <CompanyModel> > CreateCompanyAsync([FromBody] CompanyModel companyModel) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var url = HttpContext.Request.Host; var newCompany = await _companyService.CreateCompanyAsync(companyModel); return(CreatedAtRoute("GetCompany", new { companyId = newCompany.Id }, newCompany)); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, $"Something happend: {ex.Message}")); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? this.Url.Content("~/Identity/Account/Login"); if (this.Input.LicenseUrl == null) { this.ModelState.AddModelError("FileURL", "Please upload file"); } if (this.ModelState.IsValid) { var user = new SteuUser { UserName = this.Input.Username, Email = this.Input.Email, FirstName = this.Input.FirstName, LastName = this.Input.LastName }; var result = await _userManager.CreateAsync(user, this.Input.Password); if (result.Succeeded) { //_logger.LogInformation("User created a new account with password."); //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Page( // "/Account/ConfirmEmail", // pageHandler: null, // values: new { userId = user.Id, code = code }, // protocol: Request.Scheme); //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", // $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (await _userManager.Users.CountAsync() == 1) { await _userManager.AddToRoleAsync(user, GlobalConstants.AdministratorRoleName); await _userManager.AddToRoleAsync(user, GlobalConstants.ModeratorRoleName); await this.usersService.ChangeUserIsAuthorizeSatusByUserIdAsync(user.Id, "true"); } else { await _userManager.AddToRoleAsync(user, GlobalConstants.OwnerRoleName); await this.usersService.ChangeUserIsAuthorizeSatusByUserIdAsync(user.Id, "false"); } var licenseUrl = await this.cloudinaryService.UploadPictureAsync(this.Input.LicenseUrl, this.Input.LicenseUrl.FileName); CreateCompanyBindingModel model = new CreateCompanyBindingModel() { Name = this.Input.Company, UIC = this.Input.UIC, UserId = user.Id, LicenseUrl = licenseUrl, }; await this.usersService.SetMenagerByIdAsync(user.Id); await _companiesService.CreateCompanyAsync(model, user); 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()); }