示例#1
0
        public async Task AddCompany_WithProperCompany_ShouldAddCorrectly()
        {
            var companyModel = new CompanyCreationBindingModel()
            {
                Name     = companyName,
                Bulstat  = bulstat,
                Address  = address,
                TownName = town
            };

            var controller = new CompaniesController(companiesService, townsService);

            // Act

            await controller.Add(companyModel);

            var company = this.dbContext.Companies.First();

            // Assert
            Assert.AreEqual(1, this.dbContext.Companies.Count());
            Assert.AreEqual(companyName, company.Name);
            Assert.AreEqual(bulstat, company.Bulstat);
            Assert.AreEqual(address, company.Address);
            Assert.AreEqual(town, company.Town.Name);
        }
        public async Task <Company> AddCompanyAsync(CompanyCreationBindingModel model)
        {
            var company = this.Mapper.Map <Company>(model);

            await this.DbContext.Companies.AddAsync(company);

            await this.DbContext.SaveChangesAsync();

            return(company);
        }
        public async Task <IActionResult> Add(CompanyCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var town = await this.townsService.GetTownByName(model.TownName);

            if (town == null)
            {
                var townToAdd = new TownCreationBindingModel {
                    Name = model.TownName
                };
                town = await this.townsService.AddTownAsync(townToAdd);
            }

            model.TownId = town.TownId;

            var company = await this.companyService.AddCompanyAsync(model);

            return(this.RedirectToAction("Details", new { id = company.CompanyId }));
        }