public IActionResult AddTask(long sectionId, [FromBody] long taskId)
        {
            TestSection section = _db.TestSections.Where(s => s.Id == sectionId)
                                  .Include(s => s.TestSectionsAndTasks).FirstOrDefault();

            if (section == null)
            {
                return(NotFound($"TestSection with id={sectionId} doesn't exist"));
            }

            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            if (section.TestSectionsAndTasks.FirstOrDefault(t => t.TaskId == task.Id) != null)
            {
                return(BadRequest($"Task with id={taskId} is already present in TestSection with id={sectionId}"));
            }

            _db.TestSectionAndTasks.Add(new TestSectionAndTask()
            {
                TaskId        = taskId,
                TaskPosition  = section.TestSectionsAndTasks.Count(),
                TestSectionId = sectionId
            });

            _db.SaveChanges();

            return(Ok());
        }
Example #2
0
        public ActionResult <MultichoiceTaskAnswerDto> AddTaskAnswer([FromBody] AddTaskAnswerCommand cmd)
        {
            StpTask task = _db.Tasks.Find(cmd.TaskId);

            if (task == null)
            {
                return(NotFound($"Answer with id={cmd.TaskId} doesn't exist"));
            }

            MultichoiceTaskAnswer answer = new MultichoiceTaskAnswer()
            {
                Name      = cmd.Name,
                IsCorrect = cmd.IsCorrect,
                TaskId    = cmd.TaskId
            };

            _db.MultichoiceTaskAnswers.Add(answer);
            _db.SaveChanges();

            var res = new MultichoiceTaskAnswerDto()
            {
                Id        = answer.Id,
                Name      = answer.Name,
                IsCorrect = answer.IsCorrect
            };

            return(CreatedAtAction(nameof(AddTaskAnswer), res));
        }
Example #3
0
        public IActionResult UpdateTaskPosition(long taskId, [FromBody] int position)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            var tasks = _db.Tasks.Where(x => x.CategoryId == task.CategoryId).ToList();

            var maxPosition = Math.Max(task.Position, position);
            var minPosition = Math.Min(task.Position, position);

            task.Position = position;

            int shift = position == minPosition ? 1 : -1;

            tasks
            .Where(x => (x.Id != task.Id && x.Position >= minPosition && x.Position <= maxPosition))
            .ToList()
            .ForEach(c => c.Position += shift);

            _db.SaveChanges();

            return(Ok());
        }
Example #4
0
        public IActionResult DeleteTask(long taskId)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.IsDeleted = true;
            _db.SaveChanges();

            return(NoContent());
        }
Example #5
0
        public IActionResult UpdateTaskDescription(long taskId, [FromBody] string description)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.Description = description;
            _db.SaveChanges();

            return(Ok());
        }
Example #6
0
        public IActionResult UpdateTaskComplexity(long taskId, [FromBody] TaskComplexity complexity)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.Complexity = complexity;
            _db.SaveChanges();

            return(Ok());
        }
Example #7
0
        public IActionResult UpdateTaskPoints(long taskId, [FromBody] int points)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.Points = points;
            _db.SaveChanges();

            return(Ok());
        }
Example #8
0
        public IActionResult UpdateTaskDuration(long taskId, [FromBody] int duration)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.DurationMinutes = duration;
            _db.SaveChanges();

            return(Ok());
        }
Example #9
0
        public IActionResult UpdateTaskName(long taskId, [FromBody] string name)
        {
            StpTask task = _db.Tasks.Find(taskId);

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            task.Name = name;
            _db.SaveChanges();

            return(Ok());
        }
Example #10
0
        public ActionResult <TaskDto> CreateTask([FromBody] CreateTaskCommand cmd)
        {
            var category = _db.TaskCategories.Find(cmd.TaskCategoryId);

            if (category == null)
            {
                return(BadRequest($"Task category with Id={cmd.TaskCategoryId} not found"));
            }

            var skillIds       = cmd.Skills.Select(y => y.Id).ToList();
            var existingSkills = _db.Skills.Where(x => skillIds.Contains(x.Id)).ToList();
            var maxPos         = _db.Tasks.Where(x => x.CategoryId == category.Id).Max(x => (int?)x.Position);

            if (maxPos == null)
            {
                maxPos = 0;
            }
            else
            {
                ++maxPos;
            }
            using var trn = _db.Database.BeginTransaction();

            StpTask newTask = new StpTask()
            {
                CategoryId      = category.Id,
                Name            = cmd.Name,
                Points          = cmd.Points,
                DurationMinutes = cmd.DurationMinutes,
                Type            = cmd.Type,
                Position        = maxPos.Value,
                Complexity      = cmd.Complexity
            };

            _db.Tasks.Add(newTask);

            newTask.TaskAndSkills = new List <TaskAndSkill>();

            foreach (var skill in cmd.Skills)
            {
                if (skill.State != SkillState.Added && skill.State != SkillState.New)
                {
                    return(BadRequest($"Unexpected SkillState: {skill.State}"));
                }

                Skill skillToAdd = null;
                if (skill.State == SkillState.New)
                {
                    skillToAdd = new Skill()
                    {
                        Name = skill.Name
                    };
                    _db.Skills.Add(skillToAdd);
                    _db.SaveChanges();
                }
                else
                {
                    var dbSkill = existingSkills.Find(x => x.Id == skill.Id);
                    if (dbSkill == null)
                    {
                        return(BadRequest($"Skill with Id={skill.Id} not found"));
                    }
                    skillToAdd = dbSkill;
                }


                TaskAndSkill link = new TaskAndSkill()
                {
                    SkillId = skillToAdd.Id,
                    //TaskId = newTask.Id
                };
                newTask.TaskAndSkills.Add(link);

                //_db.TaskAndSkills.Add(link);
            }

            _db.SaveChanges();
            trn.Commit();

            var result = new TaskDto()
            {
                TaskSummary = new TaskSummaryDto()
                {
                    Id              = newTask.Id,
                    Complexity      = newTask.Complexity,
                    DurationMinutes = newTask.DurationMinutes,
                    Name            = newTask.Name,
                    Points          = newTask.Points,
                    Position        = newTask.Position,
                    Type            = newTask.Type,
                    Skills          = newTask.TaskAndSkills.Select(x => new SkillDto()
                    {
                        Id = x.Skill.Id, Name = x.Skill.Name
                    }).ToList(),
                },
                MultichoiceTaskInfo = new MultichoiceTaskInfoDto()
                {
                    Question = "",
                    Answers  = new List <MultichoiceTaskAnswerDto>()
                }
            };

            return(Created(nameof(CreateTask), result));
        }
Example #11
0
        public ActionResult <List <SkillDto> > UpdateSkills(long taskId, [FromBody] List <SkillStateDto> skills)
        {
            //return BadRequest($"Failed to add the skill. The task already contains it.");

            //StpTask task = _db.Tasks.Find(taskId);
            StpTask task = _db.Tasks
                           .Include(x => x.TaskAndSkills)
                           .Where(x => x.Id == taskId)
                           .FirstOrDefault();

            if (task == null)
            {
                return(NotFound($"Task with id={taskId} doesn't exist"));
            }

            //var dbSkills = task.TaskAndSkills;
            ////.Include(x => x.Skill)
            //.Where(x => x.TaskId == taskId)
            //.ToList();
            using var trn = _db.Database.BeginTransaction();

            foreach (var skill in skills)
            {
                var st = skill.State;

                TaskAndSkill newLink;
                switch (skill.State)
                {
                case SkillState.Added:

                    var duplicate = task.TaskAndSkills.FirstOrDefault(x => x.SkillId == skill.Id);
                    if (duplicate != null)
                    {
                        return(BadRequest($"Failed to add the skill '{skill.Name}'. The task already contains it."));
                    }
                    newLink = new TaskAndSkill()
                    {
                        TaskId  = task.Id,
                        SkillId = skill.Id.Value
                    };
                    task.TaskAndSkills.Add(newLink);

                    break;

                case SkillState.Removed:
                    var skillToRemove = task.TaskAndSkills.FirstOrDefault(x => x.SkillId == skill.Id);
                    task.TaskAndSkills.Remove(skillToRemove);
                    break;

                case SkillState.New:
                    // TODO: add unique name check
                    var skillToAdd = new Skill()
                    {
                        Name = skill.Name
                    };
                    _db.Skills.Add(skillToAdd);
                    _db.SaveChanges();

                    newLink = new TaskAndSkill()
                    {
                        TaskId  = task.Id,
                        SkillId = skillToAdd.Id
                    };
                    task.TaskAndSkills.Add(newLink);
                    break;

                default:
                    return(BadRequest($"Unexpected SkillState: {skill.State}"));
                }
            }
            _db.SaveChanges();
            trn.Commit();

            var res = _db.TaskAndSkills
                      .Where(x => x.TaskId == task.Id)
                      .Select(ts => new SkillDto()
            {
                Id   = ts.Skill.Id,
                Name = ts.Skill.Name
            }).ToList();

            return(res);
        }