예제 #1
0
        public async Task CanAdd()
        {
            var department = new Department {
                DepartmentId = 999, Name = "TEST DEPARTMENT"
            };
            var newDepartment = await _departmentRepository.AddAsync(department).ConfigureAwait(false);

            Assert.Equal("TEST DEPARTMENT", newDepartment.Name);
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var emptyDepartment = new Department();

            if (await TryUpdateModelAsync <Department>(
                    emptyDepartment,
                    "department",
                    d => d.Id,
                    d => d.DepartmentName,
                    d => d.Budget,
                    d => d.FoundedDate,
                    d => d.DepartmentChairId
                    ))
            {
                await _departmentRepository.AddAsync(emptyDepartment);

                return(RedirectToPage("./Index"));
            }

            return(await OnGet());
        }
예제 #3
0
        public async Task AddDepartmentAsync(Department department)
        {
            DepartmentValidator validator = new DepartmentValidator();
            await validator.ValidateAndThrowAsync(department);

            await _repository.AddAsync(department);
        }
예제 #4
0
        public async Task <Department> CreateDepartmentAsync(Department department)
        {
            var dep = await _departmentRepository.FindFirstAsync(x => x.Name == department.Name);

            if (dep != null)
            {
                throw new OperationCanceledException();
            }
            var addedDepartment = await _departmentRepository.AddAsync(department);

            return(addedDepartment);
        }
예제 #5
0
        public async Task <ResultEntity> AddOrUpdateAsync(Department department)
        {
            var result = new ResultEntity();

            #region 检查机构代码

            if (!department.CheckCode())
            {
                result.Message = "机构代码输入内容错误";
                return(result);
            }
            if (await ExistCodeAsync(department))
            {
                result.Message = "机构代码重复";
                return(result);
            }

            #endregion

            //设置机构全称
            if (department.Id == 0 || String.IsNullOrEmpty(department.FullName))
            {
                if (department.ParentId != null)
                {
                    var parentDepartment = await _departmentRepository.GetByIdAsync(department.ParentId.Value);

                    department.FullName = department.SetFullName(parentDepartment);
                    department.FullCode = department.SetFullCode(parentDepartment);
                }
                else
                {
                    department.FullName = department.Name;
                    department.FullCode = department.Code;
                }
            }


            if (department.Id > 0)
            {
                _departmentRepository.Update(department);
            }
            else
            {
                await _departmentRepository.AddAsync(department);
            }

            result.Success = true;
            return(result);
        }
예제 #6
0
        public async Task <ResponseMessageResult <string> > CreateDepartmentAsync(DepartmentViewModel department)
        {
            var response = new ResponseMessageResult <string>();

            var newDepartment = _mapper.Map <Department>(department);

            await _departmentRepository.AddAsync(newDepartment);

            await _unitOfWork.SaveChangesAsync();

            response.Content = department.Id.ToString();
            response.Message = $"Department [{department.Name}] Id: [{department.Id}] was successfully added!";

            return(response);
        }
예제 #7
0
        public async Task <IActionResult> CreateAsync([FromBody] Department department)
        {
            Logger.LogDebug("CreateAsync");
            if (department == null)
            {
                return(BadRequest(new BadRequestError("The department is null")));
            }
            await DepartmentRepository.AddAsync(department);

            return(CreatedAtRoute("GetDepartmentById", new
            {
                Controller = "Departments",
                id = department.DepartmentId
            }, department));
        }
예제 #8
0
        public async Task <SaveDepartmentResponse> SaveAsync(Department department)
        {
            try
            {
                await _departmentRepository.AddAsync(department);

                await _unitOfWork.CompleteAsync();

                return(new SaveDepartmentResponse(department));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new SaveDepartmentResponse($"An error occurred when saving the Department: {ex.Message}"));
            }
        }
예제 #9
0
        public async Task <IActionResult> Create(DepartmentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("create"));
                //return BadRequest(ModelState);
            }

            await _departmentRepository.AddAsync(new Department
            {
                Name       = model.Name,
                Createdate = DateTime.Now
            });

            return(RedirectToAction("Index"));
        }
        public async Task <IResultModel> Add(DepartmentAddModel model)
        {
            var entity = _mapper.Map <DepartmentEntity>(model);

            if (await _repository.Exists(entity))
            {
                return(ResultModel.HasExists);
            }

            var result = await _repository.AddAsync(entity);

            if (result)
            {
                return(ResultModel.Success(entity.Id));
            }

            return(ResultModel.Failed());
        }
예제 #11
0
        public async Task <IResultModel> Add(DepartmentAddModel model)
        {
            if (await _repository.ExistsName(model.Name, model.ParentId))
            {
                return(ResultModel.Failed("所属部门中已存在名称相同的部门"));
            }
            if (model.Code.NotNull() && await _repository.ExistsCode(model.Code))
            {
                return(ResultModel.Failed("编码已存在"));
            }

            var entity = _mapper.Map <DepartmentEntity>(model);

            entity.FullPath = $"/{entity.Name}";
            //查询父级
            if (model.ParentId.NotEmpty())
            {
                var parent = await _repository.GetAsync(model.ParentId);

                if (parent != null)
                {
                    //设置等级
                    entity.Level = parent.Level++;
                    //设置完整路径
                    entity.FullPath = $"{parent.FullPath}/{entity.Name}";
                }
            }

            var result = await _repository.AddAsync(entity);

            if (result)
            {
                await ClearCache();

                return(ResultModel.Success(entity));
            }

            return(ResultModel.Failed());
        }
예제 #12
0
        public async Task <DepartmentModel> AddAsync(DepartmentModel model, int clientId, int userId)
        {
            if (model == null)
            {
                throw new ArgumentNullException();
            }

            if (!await _permissionManager.HasPermission(clientId, userId, Permission.CanAddDepartment))
            {
                throw new Exception("User has not permission to perform this operation");
            }

            var department = _departmentMapper.ConvertToDataModel(model);

            department.IsActive  = true;
            department.CreatedOn = DateTime.UtcNow;
            department.UpdatedOn = DateTime.UtcNow;
            department.ClientId  = clientId;

            department = await _departmentRepository.AddAsync(department);

            return(_departmentMapper.ConvertToModel(department));
        }
예제 #13
0
        public async Task CanAddRange()
        {
            var departments = new List <Department>
            {
                new()
                {
                    CompanyId = 1, DepartmentId = 1111, Name = "TEST1"
                },
                new()
                {
                    CompanyId = 1, DepartmentId = 2222, Name = "TEST2"
                },
                new()
                {
                    CompanyId = 1, DepartmentId = 3333, Name = "TEST3"
                }
            };
            await _departmentRepository.AddAsync(departments).ConfigureAwait(false);

            await _departmentRepository.SaveAsync().ConfigureAwait(false);

            Assert.True(departments.Count > 0);
        }