public ActionResult Post([FromBody] SubTaskDto request)
 {
     try
     {
         _subTaskService.AddSubTask(request);
         Debug.WriteLine($"Successfully added subtask: {request.Text}");
         return(Ok($"Successfully added subtask: {request.Text}"));
     }
     catch (SubTaskException ex)
     {
         Debug.WriteLine($"SUBTASK: {ex.Message}");
         return(BadRequest(ex.Message));
     }
     catch (Exception ex)
     {
         Debug.WriteLine($"SUBTASK: {ex.Message}");
         return(BadRequest(ex.Message));
     }
 }
Ejemplo n.º 2
0
        public void AddSubTask(SubTaskDto request)
        {
            var user = _userRepository.GetAll()
                       .FirstOrDefault(x => x.Id == request.UserId);

            if (user == null)
            {
                throw new SubTaskException("User does not exist");
            }

            var todo = _todoRepository.GetAll()
                       .FirstOrDefault(x => x.Id == request.ToDoId);

            if (todo == null)
            {
                throw new SubTaskException("ToDo does not exist");
            }

            if (string.IsNullOrWhiteSpace(request.Text))
            {
                throw new SubTaskException("Text field is required");
            }

            if ((int)request.Status < 1 || (int)request.Status > 3)
            {
                throw new SubTaskException("Status is required and should be between 1 and 3");
            }

            var subTask = new SubTask
            {
                Text   = request.Text,
                Status = (int)request.Status,
                ToDoId = request.ToDoId
            };

            _subtaskRepository.Insert(subTask);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Creates or Updates subtask </summary>
        /// <param name="subtaskDto">subtask DTO</param>
        /// <param name="userDto">user who updates the subtask</param>
        public void SaveSubTask(SubTaskDto subtaskDto, UserDto userDto)
        {
            var rep   = UnitOfWork.GetRepository <SubTask>();
            var model = rep.GetById(subtaskDto.EntityId);

            if (model == null)
            {
                subtaskDto.CompanyId = userDto.CompanyId;
                subtaskDto.Order     = (rep.SearchFor(e => e.CompanyId == userDto.CompanyId && e.TaskId == subtaskDto.TaskId).Max(e => (int?)e.Order) ?? 0) + 1;
                model = this.Mapper.Map <SubTask>(subtaskDto);
                rep.Insert(model, userDto.Id);
            }
            else
            {
                subtaskDto.CompanyId = userDto.CompanyId;
                this.Mapper.Map(subtaskDto, model);
                rep.Update(model, userDto.Id);
            }
            this.UnitOfWork.SaveChanges();

            this.SetSubTaskFavorite(subtaskDto.Favorite, subtaskDto.TaskId, subtaskDto.EntityId, userDto);

            this.CalculateTaskValues(model.TaskId, userDto);
        }