public async Task <IActionResult> Create(CreateContestViewModel createViewModel)
        {
            var newContestDTO = new NewContestDTO()
            {
                Name         = createViewModel.Name,
                CategoryName = createViewModel.CategoryName,
                IsOpen       = createViewModel.IsOpen,
                Phase1       = createViewModel.Phase1,
                Phase2       = createViewModel.Phase2,
                Finished     = createViewModel.Finished
            };

            if (ModelState.IsValid)
            {
                try
                {
                    await this.contestService.CreateAsync(newContestDTO);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception e)
                {
                    toastNotification.AddErrorToastMessage(e.Message, new NotyOptions());
                    return(Redirect(Request.Path.Value.ToString()));
                }
            }
            var categories = await this.categoryService.GetAllBaseAsync();

            ViewData["Categories"] = new SelectList(categories, "Name", "Name");
            return(View());
        }
        public async Task <IActionResult> CreateContest([FromQuery] NewContestDTO dto)
        {
            try
            {
                var contest = await this.contestService.CreateAsync(dto);

                return(Created("post", contest));
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a contest.
        /// </summary>
        /// <param name="dto">Details of the contest to be created.</param>
        /// <returns>Returns the created contest or an appropriate error message.</returns>
        public async Task <ContestDTO> CreateAsync(NewContestDTO dto)
        {
            var newContest = new Contest();

            newContest.Name = dto.Name ?? throw new ArgumentException(Exceptions.RequiredContestName);

            var category = await this.categoryService.FindCategoryByNameAsync(dto.CategoryName);

            newContest.CategoryId = category.Id;
            newContest.Category   = category;

            var status = await this.dbContext.Statuses.FirstOrDefaultAsync(s => s.Name == "Phase 1");

            newContest.StatusId = status.Id;
            newContest.Status   = status;

            newContest.IsOpen = dto.IsOpen;

            ValidatePhase1(DateTime.ParseExact(dto.Phase1, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture),
                           DateTime.ParseExact(dto.Phase2, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture));
            newContest.Phase1 = DateTime.ParseExact(dto.Phase1, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture);

            ValidatePhase2(DateTime.ParseExact(dto.Phase1, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture),
                           DateTime.ParseExact(dto.Phase2, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture),
                           DateTime.ParseExact(dto.Finished, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture));
            newContest.Phase2 = DateTime.ParseExact(dto.Phase2, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture);

            ValidateFinished(DateTime.ParseExact(dto.Finished, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture),
                             DateTime.ParseExact(dto.Phase2, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture));
            newContest.Finished = DateTime.ParseExact(dto.Finished, "dd.MM.yy HH:mm", CultureInfo.InvariantCulture);

            newContest.CreatedOn = DateTime.UtcNow;
            await this.dbContext.Contests.AddAsync(newContest);

            await this.dbContext.SaveChangesAsync();

            return(new ContestDTO(newContest));
        }