public async Task <IActionResult> AddTask(ProblemViewModel model) { if (User.Identity.IsAuthenticated) { var user = await _userManager.GetUserAsync(User); ViewBag.Name = user.Name; } if (ModelState.IsValid) { var subject = _subjectService.GetSubject(model.Subject); Problem problem = new Problem { SubjectId = subject.Id, Number = model.Number, Answer = model.Answer }; await _problemService.AddProblemAsync(problem); foreach (var imageData in model.Condition) { using var binaryReader = new BinaryReader(imageData.OpenReadStream()); var content = binaryReader.ReadBytes((int)imageData.Length); var picture = new Picture { Content = content, Alt = $"{model.Subject} - {model.Number}", IsSolve = false, ProblemId = problem.Id }; await _pictureService.AddPictureAsync(picture); } foreach (var imageData in model.Solve) { using var binaryReader = new BinaryReader(imageData.OpenReadStream()); var content = binaryReader.ReadBytes((int)imageData.Length); var picture = new Picture { Content = content, Alt = $"{model.Subject} - {model.Number}", IsSolve = true, ProblemId = problem.Id }; await _pictureService.AddPictureAsync(picture); } return(RedirectToAction("Index", "Subject", new { subjectId = subject.Id })); } ModelState.AddModelError("Condition", ModelState.ErrorCount.ToString()); return(View(model)); }
public async Task <IActionResult> AddProblemAsync(long contestId, [FromForm] AddProblemViewModel addProblem) { var userId = _authService.GetUserIdFromRequest(Request.HttpContext.User.Claims); var result = await _problemService.AddProblemAsync(contestId, userId, addProblem); switch (result.Error) { case AddProblemResultType.Unauthorized: return(new ObjectResult(result) { StatusCode = StatusCodes.Status401Unauthorized }); case AddProblemResultType.Forbiddance: return(new ObjectResult(result) { StatusCode = StatusCodes.Status403Forbidden }); case AddProblemResultType.ContestNotFound: return(NotFound(result)); case AddProblemResultType.FileTooBig: return(BadRequest(result)); case AddProblemResultType.Wrong: return(new ObjectResult(result) { StatusCode = StatusCodes.Status422UnprocessableEntity }); case AddProblemResultType.Error: return(BadRequest(result)); case AddProblemResultType.Ok: return(Ok(result)); default: throw new ArgumentOutOfRangeException(); } }