Ejemplo n.º 1
0
        public async Task <ApiResult> AddProblemAsync(long contestId, long userId, AddProblemViewModel addProblem)
        {
            var contest = await _context.Contests.Include(c => c.Owner).SingleOrDefaultAsync(c => c.Id == contestId && c.Id == addProblem.ContestId);

            if (contest == null)
            {
                return new ApiResult {
                           Error = AddProblemResultType.ContestNotFound
                }
            }
            ;
            if (contest.Owner.UserId != userId)
            {
                return new ApiResult {
                           Error = AddProblemResultType.Unauthorized
                }
            }
            ;
            if (contest.ProgressState != ContestProgressState.NoStarted &&
                contest.ProgressState != ContestProgressState.UnPublished)
            {
                return(new ApiResult {
                    Error = AddProblemResultType.Forbiddance
                });
            }

            var problem = _mapper.Map <AddProblemViewModel, ProblemModel>(addProblem);

            var fileResult = await _fileService.AddFileAsync(addProblem.File, userId);

            switch (fileResult.Error)
            {
            case AddFileResultType.Error:
                return(new ApiResult {
                    Error = AddProblemResultType.Error
                });

            case AddFileResultType.FileTooBig:
                return(new ApiResult {
                    Error = AddProblemResultType.FileTooBig
                });

            case AddFileResultType.Ok:
                problem.Content = fileResult.Data;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            await _context.Problems.AddAsync(problem);

            await _context.SaveChangesAsync();

            return(new ApiResult {
                Error = AddProblemResultType.Ok
            });
        }
Ejemplo n.º 2
0
        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();
            }
        }