public async Task <IActionResult> Add(AddCompanyInputModel input) { if (!this.ModelState.IsValid) { this.ViewBag.InvestmentWalletId = input.InvestmentWalletId; this.TempData["InvalidAddModel"] = "Some of fields you entered are invalid!"; return(this.View(input)); } if (await this.companiesService.IsCompanyAlreadyExistAsync(input.Ticker)) { this.TempData["CompanyExist"] = $"Company with ticker {input.Ticker} already exists!"; this.ViewBag.InvestmentWalletId = input.InvestmentWalletId; return(this.View(input)); } await this.companiesService.AddAsync(input.Ticker, input.CompanyName); if (input.InvestmentWalletId == 0) { if (this.User.IsInRole(GlobalConstants.AdministratorRoleName)) { this.TempData["SuccessfullAddedCompany"] = $"Successfilly added company with name {input.CompanyName} and ticker {input.Ticker}!"; List <CompanyViewModel> companies = await this.GetAllCompaniesWithDeletedAsync(); return(this.View("Index", companies)); } return(this.Redirect($"/Investments/AllInvestments")); } return(this.Redirect($"/Trades/Add?investmentWalletId={input.InvestmentWalletId}")); }
public async Task <IActionResult> Add(AddCompanyInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); if (!this.User.IsInRole(GlobalConstants.AdministratorRoleName) && !this.User.IsInRole(GlobalConstants.CompanyRoleName)) { return(this.Unauthorized()); } string imagePath = await this.UploadImageToCloudinaryAsync(input.LogoImg); var companyId = await this.companiesService.AddAsync( input.Name, input.Description, imagePath, input.OfficialSite, user.Id, input.CategoryId); return(this.RedirectToAction(nameof(this.Details), new { id = companyId })); }
public async Task <IActionResult> AddAsync([FromForm] AddCompanyInputModel model) { var service = Ioc.Get <ICompanyService>(); var company = new Company { Id = Guid.NewGuid().ToString(), FullName = model.FullName, ShortName = model.ShortName, Nature = model.Nature, Website = model.Website, Email = model.Email, Creator = model.Creator, Contact = model.Contact, Mobile = model.Mobile, Phone = model.Phone, Address = model.Address, Description = model.Description, CreatedBy = CurrentUserId, CreatedTime = DateTime.Now, Enabled = model.Enabled }; await service.AddAsync(company); return(Ok(new StandardResult().Succeed("添加成功"))); }
public IActionResult Add() { var categories = this.categoriesService.GetAll <CategoryDropdownViewModel>(); var viewModel = new AddCompanyInputModel { Categories = categories, }; return(this.View(viewModel)); }
public async Task AddShouldCreateCompanyAndReturnViewResultWhenUserIsAdmin() { // Arrange this.FillDatabase(); this.inputModel = new AddCompanyInputModel { CompanyName = "Test Company", Ticker = "TCO", }; this.controller.TempData["InvalidAddModel"] = "Some of fields you entered are invalid!"; this.controller.TempData["CompanyExist"] = $"Company with ticker {this.inputModel.Ticker} already exists!"; this.controller.TempData["SuccessfullAddedCompany"] = $"Successfilly added company with name {this.inputModel.CompanyName} and ticker {this.inputModel.Ticker}!"; var user = new ClaimsPrincipal(new ClaimsIdentity( new Claim[] { new Claim(ClaimTypes.Name, "example name"), new Claim(ClaimTypes.NameIdentifier, "userId"), new Claim(ClaimTypes.Role, GlobalConstants.AdministratorRoleName), new Claim("custom-claim", "example claim value"), }, "mock")); this.controller.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user }, }; // Act var result = await this.controller.Add(this.inputModel); // Assert var viewResult = Assert.IsType <ViewResult>(result); var createdCompany = this.db.Companies.FirstOrDefault(x => x.Name == "Test Company"); Assert.Equal("TCO", createdCompany.Ticker); Assert.Equal("Test Company", createdCompany.Name); Assert.Equal(4, this.db.Companies.Count()); }