Example #1
0
        public async Task <ActionResult> OnPostAsync(string id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            _logger.LogTrace("Processing submission form POST");
            TaskModel retrievedTask = await _taskService.GetTaskAsync(id);

            if (retrievedTask == null)
            {
                _logger.LogWarning("Submission with invalid task {id}", id);
                return(RedirectToPage("/Tasks/Index"));
            }
            var isAuthroized = await _authService.AuthorizeAsync(User, retrievedTask, AuthorizationConstants.Submit);

            if (!isAuthroized.Succeeded)
            {
                _logger.LogWarning("{user} tried submitting task {taskid} without authorization", User.GetEmail(), id);
                return(RedirectToPage("/Tasks/Index"));
            }
            TaskDetail = retrievedTask; // Now it's ok to show user this task
            if (TaskDetail.IsEnded() && !User.IsAdmin())
            {
                _logger.LogTrace("Submission is after deadline passed {deadline}", TaskDetail.Ends);
                ModelState.AddModelError(string.Empty, "Task deadline has passed");
                return(Page());
            }
            Submission submission = Submission.GenerateSubmission(
                NewSubmission, TaskDetail, User.GetEmail());

            try
            {
                _logger.LogTrace("Submission passed tests, gonna send to service");
                Submission _ =
                    await _submisssionService.CreateSubmissionAsync(submission);

                _logger.LogTrace("Submission was created successfuly");
                return(RedirectToPage("/Submissions/Success"));
            }
            catch (Exception e)
            {
                _logger.LogWarning("Submission failed with error {e}", e);
                ModelState.AddModelError(string.Empty, "Operation failed");
            }
            return(Page());
        }