예제 #1
0
        public void ValidateNameIsNotNull()
        {
            var tempTask = new TaskDto()
            {
                Name = null
            };

            _sut.Validate(tempTask).Errors.Should()
            .Contain(x => x.PropertyName == nameof(TaskDto.Name));
        }
예제 #2
0
        public bool CreateTask(TaskCreateApiModel apiModel, int createdUser)
        {
            var validator = _taskValidator.Validate(apiModel);

            if (validator.IsValid)
            {
                return(_taskTemplateRepository.CreateTask(apiModel, createdUser));
            }
            return(false);
        }
예제 #3
0
    protected void ButtonAddTask_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            var task = new Task
            {
                Name            = TextBoxName.Text,
                CategoryId      = Convert.ToInt32(DropDownListCategories.SelectedValue),
                StartDate       = CalendarStartTime.SelectedDate,
                DueDate         = CalendarDueDate.SelectedDate,
                Completed       = CheckBoxIsCompleted.Checked,
                Priority        = Convert.ToInt32(DropDownListPriority.SelectedValue),
                PercentComplete = Convert.ToInt16(TextBoxPercentComplete.Text),
                UserName        = User.Identity.Name
            };

            var validator = new TaskValidator();

            if (validator.Validate(task))
            {
                var repository = TaskRepository.Instance;
                repository.AddTask(task);
                LabelResults.Text = "Successfully added task";
                BulletedListErrorMessages.Items.Clear();
            }
            else
            {
                BulletedListErrorMessages.DataSource = validator.ErrorMessages;
                BulletedListErrorMessages.DataBind();
            }
        }
    }
예제 #4
0
        public void Validate()
        {
            var validator = new TaskValidator();

            var validationResult = validator.Validate(this);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(string.Join(";", validationResult.Errors.Select(i => i.ErrorCode)));
            }
        }
예제 #5
0
    protected void ButtonEditTask_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            try
            {
                // Need to retrieve the task again in case there are multiple tabs open on the browser.
                // This ensures that the task actually still exists and was not already edited to 'completed' status.
                int  taskId     = Convert.ToInt32(Request["taskId"]);
                var  repository = TaskRepository.Instance;
                Task task       = repository.GetTask(taskId);

                if (task == null || task.Completed)
                {
                    throw new Exception(string.Format("Could not edit task with id '{0}'. It does not exist or is already in the completed status", taskId));
                }

                if (task.UserName != User.Identity.Name)
                {
                    throw new Exception("You cannot edit a task that does not belong to you!");
                }

                task.CategoryId      = Convert.ToInt32(DropDownListCategories.SelectedValue);
                task.StartDate       = CalendarStartTime.SelectedDate;
                task.DueDate         = CalendarDueDate.SelectedDate;
                task.Completed       = CheckBoxIsCompleted.Checked;
                task.Priority        = Convert.ToInt32(DropDownListPriority.Text);
                task.PercentComplete = Convert.ToInt16(TextBoxPercentComplete.Text);

                var validator = new TaskValidator();

                if (validator.Validate(task))
                {
                    repository.UpdateTask(task);
                    ButtonEditTask.Enabled = !task.Completed;
                    LabelResults.Text      = "Successfully edited task";
                    BulletedListErrorMessages.Items.Clear();
                }
                else
                {
                    BulletedListErrorMessages.DataSource = validator.ErrorMessages;
                    BulletedListErrorMessages.DataBind();
                }
            }
            catch (Exception ex)
            {
                LabelResults.Text = ex.Message;
            }
        }
    }
예제 #6
0
        public MessageFormat <TaskDTO> Add(TaskDTO taskDTO)
        {
            try
            {
                TaskValidator    taskValidator = new TaskValidator();
                ValidationResult result        = taskValidator.Validate(taskDTO);

                return(this.TaskRepository.Add(taskDTO));
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
예제 #7
0
        public OperationResult <Task> AddTask(Task task)
        {
            TaskValidator    taskValidator    = new TaskValidator();
            ValidationResult validationResult = taskValidator.Validate(task);

            if (!validationResult.IsValid)
            {
                return(CreateFailureResult <Task>("Invalid Data", validationResult.Errors, "400"));
            }
            try
            {
                this.TaskRepository.Add(task);
                return(CreateSuccessResult <Task>("Task Added Successully", task));
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception.Message, exception);
                return(CreateFailureResult <Task>(exception.Message, null, "500"));
            }
        }
예제 #8
0
        public void CreateTask(Task task)
        {
            var taskValidator = new TaskValidator();

            errors.Clear();

            ValidationResult result = taskValidator.Validate(task);

            if (result.IsValid)
            {
                _dbContext.Tasks.Add(task);
                _dbContext.SaveChanges();
            }
            else
            {
                foreach (ValidationFailure failure in result.Errors)
                {
                    errors.Add($"{failure.PropertyName}: {failure.ErrorMessage}");
                }
            }
        }