public async Task <IActionResult> CreateDepartment(Department department)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    department.CreationDate = DateTime.Now;
                    department.IsActive     = true;
                    await _departmentRepository.CreateAsync(department);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, $"There is allready a Department registered with the name {department.DepartmentName}, please insert another");
                        return(View(department));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.InnerException.Message);
                        return(View(department));
                    }
                }
            }

            return(View(department));
        }
Пример #2
0
        public async Task <IActionResult> Create(Department department)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _departmentRepository.CreateAsync(department);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "Already there is a department with that name");
                    }

                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.InnerException.Message);
                    }
                }
            }
            return(View(department));
        }
Пример #3
0
        public async Task CreateDepartmentAsync(DepartmentModel departmentModel)
        {
            var departmentEntity = new DepartmentEntity
            {
                Id   = Guid.NewGuid(),
                Name = departmentModel.Name
            };

            await _departmentRepository.CreateAsync(departmentEntity);

            await _departmentRepository.SaveAsync();
        }
Пример #4
0
        [HttpPost] // POST: api/departments
        public async Task <IActionResult> CreateDepartment([FromBody] Object obj)
        {
            var jsonString = JsonConvert.SerializeObject(obj);

            var department = JsonConvert.DeserializeObject <Department>(jsonString);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            await _repo.CreateAsync(department);

            return(Ok());
        }
        public async Task <Department> InsertAsync(DepartmentInsertRequestViewModel request)
        {
            Department aDepartment = new Department();

            aDepartment.Code = request.Code;
            aDepartment.Name = request.Name;

            await _departmentRepository.CreateAsync(aDepartment);

            if (await _departmentRepository.SaveCompletedAsync())
            {
                return(aDepartment);
            }
            throw new AplicationValidationException("department insert has some problem");
        }
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <bool> CreateDept(CreateDeptViewModel model)
        {
            //暂时不允许同名的部门
            if (await _departmentRepository.GetAll().AnyAsync(m => m.DeptName == model.DeptName))
            {
                return(false);
            }
            else
            {
                await _departmentRepository.CreateAsync(new Department
                {
                    DeptName = model.DeptName,
                });

                return(true);
            }
        }
        public async Task <ActionResult <Department> > Post([FromBody] Department department)
        {
            try
            {
                department.DepartmentID = Guid.NewGuid();
                await departmentRepo.CreateAsync(department);

                return(CreatedAtRoute("GetDepartmentByID",
                                      new
                {
                    id = department.DepartmentID
                },
                                      department));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Пример #8
0
        [HttpPost] // POST: /departments
        public async Task <IActionResult> CreateDepartment([FromBody] Department obj)
        {
            var jsonString = JsonConvert.SerializeObject(obj);

            var department = JsonConvert.DeserializeObject <Department>(jsonString);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                await _repo.CreateAsync(department);

                return(Ok());
            }
            catch (AppException ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Пример #9
0
 ///<inheritdoc/>
 public async Task <DepartmentDto> CreateAsync(DepartmentDto department) =>
 (await departmentRepository.CreateAsync(department.ToEntity())).ToDto();