public Result <ApplicationResult <DepartmentDto> > Create(DepartmentCreateDto departmentDto)
        {
            var applicationResult = new ApplicationResult <DepartmentDto>();

            _codeBuilder.Build("DE1", "DE")
            .OnSuccess(resultCode => DepartmentEdit.Create(resultCode, departmentDto.Name, departmentDto.Description))
            .OnSuccess(resultDepartment => {
                if (resultDepartment.IsValid)
                {
                    _departmentRepository.Create(resultDepartment)
                    .OnSuccess(() =>
                    {
                        var dto = Mapper.Map <DepartmentDto>(resultDepartment);

                        applicationResult.IsValid = true;
                        applicationResult.Entity  = dto;
                    });
                }
                else
                {
                    applicationResult.IsValid = false;
                    foreach (var error in resultDepartment.BrokenRules.Errors)
                    {
                        applicationResult.ErrorMessages.Add(new ErrorMessage {
                            Code = error.ErrorCode, Message = error.ErrorMessage
                        });
                    }
                }
            });

            return(applicationResult.ToResult());
        }
        private Result <ApplicationResult <DepartmentDto> > UpdateModel(DepartmentEdit departmentEdit, DepartmentUpdateDto departmentUpdateDto)
        {
            var applicationResult = new ApplicationResult <DepartmentDto>();

            DepartmentEdit.Update(departmentEdit, departmentUpdateDto.Name, departmentUpdateDto.Description)
            .OnSuccess(resultDepartment => {
                if (resultDepartment.IsValid)
                {
                    _departmentRepository.Update(resultDepartment)
                    .OnSuccess(() => {
                        var dto = Mapper.Map <DepartmentDto>(resultDepartment);

                        applicationResult.IsValid = true;
                        applicationResult.Entity  = dto;
                    });
                }
                else
                {
                    applicationResult.IsValid = false;
                    foreach (var error in resultDepartment.BrokenRules.Errors)
                    {
                        applicationResult.ErrorMessages.Add(new ErrorMessage {
                            Code = error.ErrorCode, Message = error.ErrorMessage
                        });
                    }
                }
            });

            return(applicationResult.ToResult());
        }
        public Result <ApplicationResult <DepartmentDto> > Update(Guid departmentId, DepartmentUpdateDto departmentUpdateDto)
        {
            var applicationResult = new ApplicationResult <DepartmentDto>();

            Fetch(new FetchDepartmentByIdSpec(departmentId))
            .OnSuccess(resultDepartmentDto => {
                var editToUpdate = Mapper.Map <DepartmentEdit>(resultDepartmentDto);

                return(UpdateModel(editToUpdate, departmentUpdateDto));
            });

            return(applicationResult.ToResult());
        }
        public Result <ApplicationResult <DepartmentDto> > Delete(DeleteDepartmentSpec spec)
        {
            var applicationResult = new ApplicationResult <DepartmentDto>();

            _departmentRepository.Delete(spec)
            .OnSuccess(resultDepartment => {
                var dto = Mapper.Map <DepartmentDto>(resultDepartment);
                applicationResult.IsValid = true;
                applicationResult.Entity  = dto;
            });

            return(applicationResult.ToResult());
        }