public async Task <IHttpActionResult> EditCompany(string id, EditCompanyBindingModel model) { var userId = this.User.Identity.GetUserId(); if (userId == null) { return(this.BadRequest("Invalid session token.")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (model == null) { return(this.BadRequest("Invalid input data")); } var company = await this.Data.Companies.All() .Where(c => c.Id == new Guid(id) && c.OwnerId == userId) .FirstOrDefaultAsync(); if (company == null) { return(this.BadRequest("Company id is not correct or you are not allowed to edit it.")); } company.Email = model.Email ?? company.Email; company.Name = model.Name ?? company.Name; await this.Data.SaveChangesAsync(); return(this.StatusCode(HttpStatusCode.NoContent)); }
public IActionResult Edit(EditCompanyBindingModel bm, int id) { var company = CompanyServices.GetCompanyById(id); if (bm.Name != null) { var isTaken = CompanyServices.NameIsTaken(bm.Name); if (isTaken) { ModelState.AddModelError("Name", "This name is already taken"); return(this.View(company)); } company.Name = bm.Name; } if (bm.FoundationDate != null) { company.FoundationDate = bm.FoundationDate.Value; } if (bm.PictureUrl != null) { company.PictureUrl = bm.PictureUrl; } this.CompanyServices.UpdateCompany(company); this.ViewData["edit"] = "You have successfully edited this company!"; return(this.View(company)); }
public async Task <IActionResult> EditCompany(EditCompanyBindingModel model) { if (ModelState.IsValid) { await service.EditCompany(model); return(RedirectToAction("DisplayAllCompanies")); } else { return(View()); } }
public async Task <IActionResult> EditCompany(int id) { var foundCompany = await service.FindCompany(id); if (foundCompany == null) { return(NotFound()); } var companyToEdit = new EditCompanyBindingModel(); companyToEdit.Id = foundCompany.Id; companyToEdit.Name = foundCompany.Name; companyToEdit.IsDeleted = foundCompany.IsDeleted; return(View(companyToEdit)); }
public async Task EditCompany(EditCompanyBindingModel model) { var editedCompany = await FindCompany(model.Id); if (editedCompany == null) { throw new NullReferenceException($"No company with id:{model.Id} exists."); } else { editedCompany.Name = model.Name; editedCompany.IsDeleted = model.IsDeleted; } dbContext.Update(editedCompany); await dbContext.SaveChangesAsync(); }