Ejemplo n.º 1
0
        public void AddDepartment(AddDepartmentDto data)
        {
            var newDepartment = AutoMapper.Mapper.Map <AddDepartmentDto, Department>(data);

            _unitOfWork.DepartmentRepository.Add(newDepartment);
            _unitOfWork.Commit();
        }
Ejemplo n.º 2
0
        public async Task Add(AddDepartmentDto departmentDto)
        {
            var clientRepository = DataContextManager.CreateRepository <IClientRepository>();

            var client = await clientRepository.GetById(departmentDto.ClientId);

            if (client == null)
            {
                throw new NotFoundDependencyObjectException("Client", departmentDto.ClientId);
            }

            if (client.Departments.Where(x => x.IsActive).Select(s => s.Name).Contains(departmentDto.Name))
            {
                throw new FoundSameObjectException("Department", departmentDto.Name);
            }

            var department = new Department
            {
                Name       = departmentDto.Name,
                CreateDate = DateTime.UtcNow,
                IsActive   = true
            };

            client.Departments.Add(department);

            await clientRepository.Update(client);
        }
        public async Task <ServiceResponse <GetDepartmentDto> > AddDepartment(AddDepartmentDto newDepartment)
        {
            try
            {
                var department = await _dbContext.Departments.FirstOrDefaultAsync(x => x.Description == newDepartment.DepartmentDescription);

                //check department duplicate
                if (!(department is null))
                {
                    return(ResponseResult.Failure <GetDepartmentDto>("Department duplicate."));
                }

                //asign value
                var department_new = new Department
                {
                    Description = newDepartment.DepartmentDescription
                };

                //insert database
                _dbContext.Departments.Add(department_new);
                await _dbContext.SaveChangesAsync();

                var department_return = await _dbContext.Departments.FirstOrDefaultAsync(x => x.Description == newDepartment.DepartmentDescription);

                //mapper Dto and return
                var dto = _mapper.Map <GetDepartmentDto>(department_return);
                _log.LogInformation("Add Department Success.");
                return(ResponseResult.Success(dto, "Success"));
            }
            catch (Exception ex)
            {
                _log.LogError(ex.Message);
                return(ResponseResult.Failure <GetDepartmentDto>(ex.Message));
            }
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> AddDepartment([FromBody] AddDepartmentDto departmentDto)
        {
            if (!IsAvailableOperation())
            {
                return(BadRequest());
            }

            await _departmentService.Add(departmentDto);

            AddLog(Enums.LogType.Create, LogMessage.CreateSuccessByNameMessage(LogMessage.DepartmentEntityName, departmentDto.Name, LogMessage.CreateAction, UserId));
            return(Ok());
        }
Ejemplo n.º 5
0
        public async Task UpdateDepartment(AddDepartmentDto department)
        {
            var depart = await _departmentRepository.GetByName(department.Name);

            if (depart == null)
            {
                throw new Exception("Wrong department");
            }
            depart.Name         = department.Name;
            depart.PhoneNumber  = department.PhoneNumber;
            depart.Descriptcion = department.Description;

            await _departmentRepository.UpdateDepartment(depart);
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <ResponseDto <int> > > AddDepartment([FromBody] AddDepartmentDto departmentDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            ResponseDto <int> result = await _departmentsService.AddDepartment(departmentDto);

            if (result.HasErrors)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Ejemplo n.º 7
0
        public static ResponseDto <int> ValidateAddDepartment(Department department, AddDepartmentDto departmentDto)
        {
            var response = new ResponseDto <int>();

            if (department != null)
            {
                response.AddError(DepartmentErrors.NameExists);
            }

            if (departmentDto.Name.Length < 2)
            {
                response.AddError(DepartmentErrors.TooShortName);
            }

            return(response);
        }
Ejemplo n.º 8
0
        public async Task <ResponseDto <int> > AddDepartment(AddDepartmentDto departmentDto)
        {
            var departmentFromDb = await _departmentsRepository.GetDepartmentByName(departmentDto.Name);

            ResponseDto <int> response = DepartmentsValidator.ValidateAddDepartment(departmentFromDb, departmentDto);

            if (response.HasErrors)
            {
                return(response);
            }

            var mappedDepartment = Mapper.Map <Department>(departmentDto);

            response.Value = await _departmentsRepository.AddDepartment(mappedDepartment);

            return(response);
        }
Ejemplo n.º 9
0
        public async Task AddDepartment(AddDepartmentDto department)
        {
            var depart = await _departmentRepository.GetByName(department.Name);

            if (depart != null)
            {
                throw new Exception("This department is used");
            }

            var newDepartment = new Department();

            newDepartment.Id           = Guid.NewGuid();
            newDepartment.Name         = department.Name;
            newDepartment.PhoneNumber  = department.PhoneNumber;
            newDepartment.Descriptcion = department.Description;

            await _departmentRepository.AddDepartment(newDepartment);
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> CreateDepartment(AddDepartmentDto model)
        {
            try
            {
                var department = _mapper.Map <Departments>(model);
                if (!await _manager.CreateDepartment(department))
                {
                    return(Ok(new JsonMessageResult("创建失败!", 0, null)));
                }

                return(CreatedAtAction(nameof(GetDepartment), new { department.Id }, department));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Ok(new JsonMessageResult(e.Message, 0, null)));
            }
        }
Ejemplo n.º 11
0
        public async Task <ReturnData <bool> > CreateAsync(AddDepartmentDto model)
        {
            var result    = new ReturnData <bool>();
            var deparment = await _context.Departments.FirstOrDefaultAsync(i => i.Name == model.Name);

            if (deparment != null)
            {
                result.Message = "已存在相同名称部门,添加失败";
                result.Result  = false;
                result.Success = true;
                return(result);
            }
            _context.Departments.Add(new Department()
            {
                Name = model.Name
            });
            result.Result = await _context.SaveChangesAsync() > 0;

            return(result);
        }
 public async Task <IActionResult> AddDepartment(AddDepartmentDto newDepartment)
 {
     return(Ok(await _comService.AddDepartment(newDepartment)));
 }
        public async Task <IActionResult> Create(AddDepartmentDto model)
        {
            var result = await _service.CreateAsync(model);

            return(Ok(result));
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> EditDepartment([FromBody] AddDepartmentDto department)
        {
            await _departmentService.UpdateDepartment(department);

            return(NoContent());
        }
Ejemplo n.º 15
0
        public async Task <IActionResult> Post([FromBody] AddDepartmentDto department)
        {
            await _departmentService.AddDepartment(department);

            return(Created("/department", null));
        }
Ejemplo n.º 16
0
 public IActionResult AddDepartment([FromBody] AddDepartmentDto departmentDto)
 {
     _departmentService.AddDepartment(departmentDto);
     return(Json(JsonResultData.Success()));
 }