Beispiel #1
0
        public IActionResult CreateProblem([FromBody] CreateProblemRequest request)
        {
            var user = UserService.Get(long.Parse(User.Identity.Name));

            if (user == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Пользователь не найден"
                }));
            }

            var problem = new Problem
            {
                User        = user,
                ProblemText = request.ProblemText
            };

            ProblemService.Create(problem);

            return(Ok(new DataResponse <ProblemViewModel>
            {
                Data = new ProblemViewModel(problem)
            }));
        }
        private void AddHolds(ExistingProblem problem, CreateProblemRequest newProblem,
                              IEnumerable <Hold> existingHolds, IEnumerable <HoldRule> existingHoldRules)
        {
            if (problem.Holds is null)
            {
                throw new ArgumentException("The problem must have holds.", nameof(problem));
            }

            if (newProblem.Holds is null)
            {
                throw new ArgumentException("The problem must have holds.", nameof(newProblem));
            }

            foreach (var hold in problem.Holds)
            {
                var newHold = new CreateHoldOnProblemRequest()
                {
                    HoldId = existingHolds.First(existingHold => existingHold.Name == hold.Hold).Id,
                    IsStandingStartHold = hold.IsStandingStartHold,
                    ExistingHoldRuleIds = new List <int>(),
                    NewHoldRules        = new List <CreateHoldRuleRequest>()
                };

                AddHoldRules(hold, newHold, existingHoldRules);
                newProblem.Holds.Add(newHold);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Create a Problem entity from a CreateProblemRequest DTO
        /// </summary>
        /// <param name="problem">The DTO to map from</param>
        /// <returns>The corresponding entity</returns>
        /// <exception cref="ArgumentNullException"></exception>
        internal static Problem Map(CreateProblemRequest problem)
        {
            if (problem is null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            var problemDbo = new Problem()
            {
                Name           = problem.Name,
                Description    = problem.Description,
                SetBy          = problem.SetBy,
                DateSet        = problem.DateSet,
                FirstAscent    = problem.FirstAscent,
                TechGradeId    = problem.TechGradeId,
                BGradeId       = problem.BGradeId,
                PoveyGradeId   = problem.PoveyGradeId,
                FurlongGradeId = problem.FurlongGradeId
            };

            if (problem.StyleSymbolIds != null)
            {
                AddStyleSymbolsToProblem(problem.StyleSymbolIds, problemDbo);
            }

            problemDbo.ProblemRules = CreateProblemRuleDbos(problem, problemDbo).ToList();
            problemDbo.ProblemHolds = problem.Holds.Select((hold, index) => CreateProblemHoldDbo(hold, index, problemDbo)).ToList();

            return(problemDbo);
        }
        private Problem AddUnverifiedProblemToDatabase(CreateProblemRequest problem, string userEmail)
        {
            var problemDbo = Mapper.Map(problem);

            problemDbo.Verified = false;
            problemDbo.LoggedBy = userEmail;
            repository.Problem.Add(problemDbo);
            return(problemDbo);
        }
Beispiel #5
0
        public async Task <ActionResult <CustomResponse <Guid> > > CreateProblem(
            [FromServices] IObjectService objectService,
            [FromServices] ProblemProvider problemProvider,
            [FromServices] ApplicationDbContext dbContext,
            [FromBody] CreateProblemRequest request
            )
        {
            var problem = await problemProvider.CreateObject(dbContext, request.Statement, request.TestData);

            return(new CustomResponse <Guid>(problem.Id));
        }
Beispiel #6
0
        /// <summary>
        /// Validates a CreateProblemRequest
        /// </summary>
        /// <param name="problem">The request to validate</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="EntityNotFoundException"></exception>
        /// <exception cref="EntityWithNameAlreadyExistsException"></exception>
        /// <exception cref="InternalEntityNotFoundException"></exception>
        public void Validate(CreateProblemRequest problem)
        {
            if (problem is null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            ValidateCreateProblemRequest(problem);
            ValidateCreateHoldsOnProblemRequest(problem.Holds);
            ValidateAddProblemRulesRequest(problem.NewRules, problem.ExistingRuleIds);
            ValidateAddProblemStyleSymbolsRequest(problem.StyleSymbolIds);
        }
Beispiel #7
0
        private void ValidateCreateProblemRequest(CreateProblemRequest problem)
        {
            if (problem is null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            repository.Problem.VerifyEntityWithNameDoesNotExists(problem.Name);

            VerifyGradeExists(problem.TechGradeId, repository.TechGrade);
            VerifyGradeExists(problem.BGradeId, repository.BGrade);
            VerifyGradeExists(problem.PoveyGradeId, repository.PoveyGrade);
            VerifyGradeExists(problem.FurlongGradeId, repository.FurlongGrade);
        }
        private void AddRules(ExistingProblem problem, CreateProblemRequest newProblem)
        {
            if (repository.GeneralRule is null)
            {
                throw new DatabaseException();
            }

            foreach (string rule in problem.Rules ?? new List <string>())
            {
                var existingRule = repository.GeneralRule.Local.First(generalRule => generalRule.Name == rule);
                newProblem.ExistingRuleIds = newProblem.ExistingRuleIds.Concat(new List <int>()
                {
                    existingRule.Id
                });
            }
        }
        private static void AddGrades(ExistingProblem problem, CreateProblemRequest newProblem, IEnumerable <TechGrade> techGrades,
                                      IEnumerable <BGrade> bGrades, IEnumerable <FurlongGrade> furlongGrades)
        {
            if (problem.Grades is null)
            {
                throw new ArgumentException("The problem must have grades.", nameof(problem));
            }

            foreach (string grade in problem.Grades)
            {
                if (Regex.IsMatch(grade, "^[4-6][a-c][+-]?$"))
                {
                    if (newProblem.TechGradeId != null)
                    {
                        throw new Exception();
                    }

                    newProblem.TechGradeId = techGrades.First(techGrade => techGrade.Name == grade).Id;
                }
                else if (Regex.IsMatch(grade, "^B[0-8]$"))
                {
                    if (newProblem.BGradeId != null)
                    {
                        throw new Exception();
                    }

                    newProblem.BGradeId = bGrades.First(bGrade => bGrade.Name == grade).Id;
                }
                else if (Regex.IsMatch(grade, "^(AAA)|(XXX)$"))
                {
                    if (newProblem.FurlongGradeId != null)
                    {
                        throw new Exception();
                    }

                    newProblem.FurlongGradeId = furlongGrades.First(furlongGrade => furlongGrade.Name == grade).Id;
                }
                else
                {
                    throw new Exception();
                }
            }
        }
        private void StoreProblems(IEnumerable <ExistingProblem> problems, bool validate)
        {
            if (repository.Problem is null || repository.HoldRule is null)
            {
                throw new DatabaseException();
            }

            var holds         = repository.Hold.ToList();
            var holdRules     = repository.HoldRule.Local.ToList();
            var techGrades    = repository.TechGrade.ToList();
            var bGrades       = repository.BGrade.ToList();
            var furlongGrades = repository.FurlongGrade.ToList();

            var newProblems = new List <CreateProblemRequest>();

            foreach (var problem in problems)
            {
                var newProblem = new CreateProblemRequest()
                {
                    Name            = problem.Name,
                    NewRules        = new List <CreateProblemRuleRequest>(),
                    ExistingRuleIds = new List <int>(),
                    Holds           = new List <CreateHoldOnProblemRequest>()
                };

                AddRules(problem, newProblem);
                AddHolds(problem, newProblem, holds, holdRules);
                AddGrades(problem, newProblem, techGrades, bGrades, furlongGrades);

                if (validate)
                {
                    validator.Validate(newProblem);
                }

                newProblems.Add(newProblem);
            }

            var problemDbos = newProblems.Select(Mapper.Map);

            repository.Problem.AddRange(problemDbos);
        }
        /// <summary>
        /// Creates a problem
        /// </summary>
        /// <param name="problem">Problem to create</param>
        /// <returns>The created problem</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="EntityNotFoundException"></exception>
        /// <exception cref="EntityWithNameAlreadyExistsException"></exception>
        /// <exception cref="InternalEntityNotFoundException"></exception>
        public ProblemResponse CreateUnverifiedProblem(CreateProblemRequest problem, string userEmail)
        {
            if (problem is null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            validator.Validate(problem);

            var problemDbo = AddUnverifiedProblemToDatabase(problem, userEmail);

            repository.SaveChanges();

            try
            {
                return(problemReader.GetProblem(problemDbo.Id));
            }
            catch (EntityNotFoundException exception)
            {
                throw new EntityCreationException(string.Empty, exception);
            }
        }
Beispiel #12
0
        public ActionResult <ProblemResponse> CreateUnverifiedProblem(CreateProblemRequest problem)
        {
            var createdProblem = problemCreator.CreateUnverifiedProblem(problem, UserEmail);

            return(CreatedAtRoute(new { problemId = createdProblem.ProblemId }, createdProblem));
        }