public async Task <IActionResult> Create(OfficeCreateBindingModel model, int companyId) { if (ModelState.IsValid) { var serviceModel = new OfficeCreateServiceModel() { CompanyId = companyId, Country = model.Country, City = model.City, Street = model.Street, StreetNumber = model.StreetNumber, IsHQ = model.IsHQ, }; bool result = await this._officeServices.CreateOfficeAsync(serviceModel); if (!result) { return(RedirectToAction("Error", "Home")); } return(RedirectToAction("Details", "Company", new { id = companyId })); } return(View(model)); }
public async Task EditAsync(OfficeCreateServiceModel officeDto) { Office office = await dbContext .Offices .FindAsync(officeDto.Id); if (office == null) { return; } if (officeDto.IsHeadQuarters == true) { var currentHeadquarters = await GetHeadquarters(officeDto.CompanyId); if (currentHeadquarters != null) { // Probably there should be only one headquarters. Exception could be thrown too. currentHeadquarters.IsHeadQuarters = false; } } office.Address = officeDto.Address; office.CompanyId = officeDto.CompanyId; office.CityId = officeDto.CityId; office.IsHeadQuarters = officeDto.IsHeadQuarters; await dbContext.SaveChangesAsync(); }
public async Task <ActionResult> EditAsync(int id, [FromBody] OfficeCreateModel office) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!await officeService.ExistsAsync(id)) { return(NotFound()); } var officeToEdit = new OfficeCreateServiceModel { Id = id, Address = new Address { Street = office.Street, StreetNumber = office.StreetNumber }, CityId = office.CityId, CompanyId = office.CompanyId, IsHeadQuarters = office.IsHeadQuarters }; await officeService .EditAsync(officeToEdit); return(NoContent()); }
public async Task <bool> CreateOfficeAsync(OfficeCreateServiceModel model) { Office office = new Office() { City = model.City, Country = model.Country, Street = model.Street, StreetNumber = model.StreetNumber, IsHQ = model.IsHQ, CompanyId = model.CompanyId }; this._context.Offices.Add(office); int result = await this._context.SaveChangesAsync(); if (result > 0) { return(true); } return(false); }