/// <summary> /// Coverts Department to WebDepartment /// </summary> public static WebDepartment DepartmentToWebDepartment(this Department department) { var webDepartment = new WebDepartment { Id = department.Id, Name = department.Name }; return(webDepartment); }
public IActionResult UpdateDepartment([FromBody] WebDepartment updateDepartmentRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbDepartment = _dbContext.Departments.FirstOrDefault(u => u.Id == updateDepartmentRequest.Id); if (dbDepartment == null) { return(BadRequest($"Department {updateDepartmentRequest.Id} does not exist")); } if (_dbContext.Departments.Any(u => u.Name == updateDepartmentRequest.Name)) { return(BadRequest($"Name {updateDepartmentRequest.Name} is not unique")); } if (!string.IsNullOrEmpty(updateDepartmentRequest.Name)) { dbDepartment.Name = updateDepartmentRequest.Name; } using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Departments.Update(dbDepartment); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbDepartment.DepartmentToWebDepartment())); }
public ActionResult Add(WebDepartment input) { _context.AddOneEntity <WebDepartment>(input); return(RedirectToAction <WebDepartmentController>(d => d.Index(input.LanguageId))); }