public async Task <ApiResult> PutUserAsync(PutUserViewModel putUser) { var user = await _context.Users.Include(u => u.Role).SingleOrDefaultAsync(u => u.Id == putUser.Id); if (user == null) { return new PutUserResult { Error = PutUserResultType.UserNotFound } } ; if (user.Role.Type == RoleType.Teacher) { var count = (await _context.Teachers.Include(t => t.Contests).Where(t => t.UserId == user.Id) .SingleOrDefaultAsync()).Contests.Count; if (count > 0) { return(new PutUserResult { Error = PutUserResultType.Forbiddance }); } } if (putUser.Password != null) { putUser.Password = GetRandomPassword(); user.PasswordHash = _passwordHasher.HashPassword(user, putUser.Password); } else { if (user.LoginName != putUser.LoginName) { var uu = await _context.Users.SingleOrDefaultAsync(u => u.LoginName == putUser.LoginName); if (uu != null) { return(new PutStudentResult { Error = PutUserResultType.Repeat }); } } user.LoginName = putUser.LoginName; user.UserName = putUser.UserName; try { var role = await _authService.GetRoleFromRoleTypeAsync(Enum.Parse <RoleType>(putUser.RoleType.ToString())); if (role == null) { return new PutUserResult { Error = PutUserResultType.Wrong } } ; if (user.Role.Type == RoleType.Teacher && role.Type != RoleType.Teacher) { var teacher = await _context.Teachers.Include(t => t.Contests).Where(t => t.UserId == user.Id) .SingleOrDefaultAsync(); if (teacher.Contests.Count > 0) { return(new PutUserResult { Error = PutUserResultType.Forbiddance }); } _context.Teachers.Remove(teacher); await _context.SaveChangesAsync(); } else if (user.Role.Type != RoleType.Teacher && role.Type == RoleType.Teacher) { var teacher = new TeacherModel { UserId = user.Id }; await _context.Teachers.AddAsync(teacher); await _context.SaveChangesAsync(); } else { user.RoleId = role.Id; } } catch (Exception) { return(new PutUserResult { Error = PutUserResultType.Wrong }); } } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return(new PutUserResult { Error = PutUserResultType.Ok, Data = putUser }); } catch (DbUpdateConcurrencyException) { return(new PutUserResult { Error = PutUserResultType.ConcurrencyException }); } }
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 }); } }