Beispiel #1
0
        public async Task <IActionResult> Create(TaskVariantViewModel taskVariant)
        {
            string userId = _userManager.GetUserId(User);

            if (ModelState.IsValid)
            {
                var testerFile = new FileInfo(Path.Combine(_env.WebRootPath, "App_Data", "Testers", taskVariant.Id + ".zip"));

                if (testerFile.Exists)
                {
                    testerFile.Delete();
                }

                if (taskVariant.TesterArchive != null && taskVariant.TesterArchive.Length != 0)
                {
                    using (var fileStream = new FileStream(testerFile.FullName, FileMode.Create))
                        await taskVariant.TesterArchive.CopyToAsync(fileStream);
                }

                TaskVariant editingTaskVariant;
                if ((editingTaskVariant = _context.TaskVariants.Find(taskVariant.Id)) != null)
                {
                    try
                    {
                        taskVariant = _taskVariantLogic.EditTaskVariant(userId, taskVariant.Id, taskVariant);
                    }
                    catch (NotFoundException)
                    {
                        return(NotFound());
                    }
                    catch (IllegalAccessException)
                    {
                        Challenge();
                    }
                }
                else
                {
                    try
                    {
                        taskVariant = _taskVariantLogic.CreateTaskVariant(userId, taskVariant);
                    }
                    catch (NotFoundException)
                    {
                        return(NotFound());
                    }
                    catch (IllegalAccessException)
                    {
                        Challenge();
                    }
                }
                return(RedirectToAction(nameof(View), new { id = taskVariant.Id }));
            }

            return(View(taskVariant));
        }
Beispiel #2
0
 public IActionResult View(int id)
 {
     try
     {
         string userId = _userManager.GetUserId(User);
         TaskVariantViewModel viewModel = _taskVariantLogic.GetTaskVariant(userId, id);
         return(View(viewModel));
     }
     catch (NotFoundException)
     {
         return(NotFound());
     }
 }
Beispiel #3
0
        public IActionResult EditTaskVariant(int taskVariantId, [FromBody] TaskVariantViewModel taskVariant)
        {
            string userId = _userManager.GetUserId(HttpContext.User);

            try
            {
                return(new ObjectResult(_taskVariantLogic.EditTaskVariant(userId, taskVariantId, taskVariant)));
            }
            catch (NotFoundException)
            {
                return(NotFound());
            }
            catch (IllegalAccessException)
            {
                return(Challenge());
            }
        }
Beispiel #4
0
        public TaskVariantViewModel CreateTaskVariant(string userId, TaskVariantViewModel taskVariant)
        {
            if (!KaCakeUtils.IsCourseTeacher(_context, _context.TaskGroups.Find(taskVariant.TaskGroupId).CourseId, userId))
            {
                throw new IllegalAccessException();
            }

            var entity = _context.TaskVariants.Add(new TaskVariant()
            {
                TaskGroupId = taskVariant.TaskGroupId,
                Name        = taskVariant.Name,
                Description = taskVariant.Description
            });

            _context.SaveChanges();

            return(GetTaskVariant(userId, entity.Entity.Id));
        }
Beispiel #5
0
        public TaskVariantViewModel EditTaskVariant(string userId, int taskVariantId, TaskVariantViewModel taskVariant)
        {
            var editingTaskVariant = _context.TaskVariants
                                     .Include(tv => tv.TaskGroup)
                                     .FirstOrDefault(tv => tv.Id == taskVariantId);

            if (editingTaskVariant == null)
            {
                throw new NotFoundException();
            }
            if (!KaCakeUtils.IsCourseTeacher(_context, editingTaskVariant.TaskGroup.CourseId, userId))
            {
                throw new IllegalAccessException();
            }

            editingTaskVariant.Name        = taskVariant.Name;
            editingTaskVariant.Description = taskVariant.Description;
            _context.SaveChanges();

            return(GetTaskVariant(userId, editingTaskVariant.Id));
        }