예제 #1
0
        /// <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);
        }
예제 #2
0
        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()));
        }
예제 #3
0
 public ActionResult Add(WebDepartment input)
 {
     _context.AddOneEntity <WebDepartment>(input);
     return(RedirectToAction <WebDepartmentController>(d => d.Index(input.LanguageId)));
 }