public async Task <ApiResult> AddContestAsync(TeacherContestViewModel addContest, long userId) { if (addContest.StartTime >= addContest.EndTime) { return(new ApiResult { Error = AddContestResultType.Wrong }); } var time = DateTime.Now; if (addContest.StartTime <= time || addContest.EndTime <= time) { return(new ApiResult { Error = AddContestResultType.Wrong }); } var owner = await _context.Teachers.SingleOrDefaultAsync(o => o.UserId == userId); var contest = _mapper.Map <TeacherContestViewModel, ContestModel>(addContest); contest.CreateTime = DateTime.Now; contest.OwnerId = owner.Id; contest.State = ContestState.UnPublished; await _context.Contests.AddAsync(contest); await _context.SaveChangesAsync(); return(new ApiResult { Error = AddContestResultType.Ok }); }
public async Task <IActionResult> PutContestAsync(int id, [FromBody] TeacherContestViewModel putContest) { if (!ModelState.IsValid) { return(new ObjectResult(new ApiDataResult { Error = PutContestResultType.Wrong, Data = new { ModelState } }) { StatusCode = StatusCodes.Status422UnprocessableEntity }); } if (id != putContest.Id) { return(BadRequest(new ApiResult { Error = PutContestResultType.Wrong })); } var userId = _authService.GetUserIdFromRequest(Request.HttpContext.User.Claims); var result = await _contestService.PutContestAsync(putContest, userId); switch (result.Error) { case PutContestResultType.Unauthorized: return(new ObjectResult(result) { StatusCode = StatusCodes.Status401Unauthorized }); case PutContestResultType.ConcurrencyException: return(BadRequest(result)); case PutContestResultType.ContestNotFound: return(NotFound(result)); case PutContestResultType.Wrong: return(new ObjectResult(result) { StatusCode = StatusCodes.Status422UnprocessableEntity }); case PutContestResultType.Error: return(BadRequest(result)); case PutContestResultType.Ok: return(Ok(result)); default: throw new ArgumentOutOfRangeException(); } }
public async Task <IActionResult> PostContestAsync([FromBody] TeacherContestViewModel addContest) { var userId = _authService.GetUserIdFromRequest(Request.HttpContext.User.Claims); var result = await _contestService.AddContestAsync(addContest, userId); switch (result.Error) { case AddContestResultType.Wrong: return(BadRequest(result)); case AddContestResultType.Error: return(BadRequest(result)); case AddContestResultType.Ok: return(Ok(result)); default: throw new ArgumentOutOfRangeException(); } }
public async Task <ApiResult> PutContestAsync(TeacherContestViewModel putContest, long userId) { var contest = await _context.Contests.Include(c => c.Owner).SingleOrDefaultAsync(c => c.Id == putContest.Id); if (contest == null) { return new ApiResult { Error = PutContestResultType.ContestNotFound } } ; if (contest.Owner.UserId != userId) { return new ApiResult { Error = PutContestResultType.Unauthorized } } ; _context.Entry(contest).State = EntityState.Modified; if (putContest.StartTime >= putContest.EndTime) { return(new ApiResult { Error = PutContestResultType.Wrong }); } if (contest.State == ContestState.UnPublished) { if (putContest.State == 1) { var time = DateTime.Now; if (putContest.StartTime <= time || putContest.EndTime <= time) { return(new ApiResult { Error = PutContestResultType.Wrong }); } } contest.Name = putContest.Name; contest.Notice = putContest.Notice; contest.StartTime = putContest.StartTime; contest.EndTime = putContest.EndTime; contest.State = Enum.Parse <ContestState>(putContest.State.ToString()); } else if (contest.State == ContestState.NotDownloaded) { if (contest.ProgressState == ContestProgressState.NoStarted) { var time = DateTime.Now; if (putContest.StartTime <= time || putContest.EndTime <= time) { return(new ApiResult { Error = PutContestResultType.Wrong }); } contest.Name = putContest.Name; contest.Notice = putContest.Notice; contest.StartTime = putContest.StartTime; contest.EndTime = putContest.EndTime; contest.State = Enum.Parse <ContestState>(putContest.State.ToString()); } else if (contest.ProgressState == ContestProgressState.InProgress) { var time = DateTime.Now; if (time >= putContest.EndTime || contest.StartTime >= putContest.EndTime) { return(new ApiResult { Error = PutContestResultType.Wrong }); } contest.EndTime = putContest.EndTime; contest.Notice = putContest.Notice; } else { return(new ApiResult { Error = PutContestResultType.Wrong }); } } else { return(new ApiResult { Error = PutContestResultType.Wrong }); } try { await _context.SaveChangesAsync(); return(new ApiResult { Error = PutContestResultType.Ok }); } catch (DbUpdateConcurrencyException) { return(new ApiResult { Error = PutContestResultType.ConcurrencyException }); } }