public async Task <ResultModel> DeleteContest([FromBody] ContestIdModel model) { var(user, privilege) = await GetUserPrivilegeAsync(); var ret = new ResultModel { IsSucceeded = true }; if (!HasTeacherPrivilege(privilege)) { ret.IsSucceeded = false; ret.ErrorMessage = "没有权限"; return(ret); } using (var db = new ApplicationDbContext(_dbContextOptions)) { var contest = await db.Contest.FindAsync(model.Id); if (contest == null) { ret.IsSucceeded = false; ret.ErrorMessage = "比赛不存在"; return(ret); } db.Contest.Remove(contest); await db.SaveChangesAsync(); return(ret); } }
public async Task <ContestIdModel> UpdateContestConfig([FromBody] ContestEditModel model) { var(user, privilege) = await GetUserPrivilegeAsync(); var ret = new ContestIdModel { IsSucceeded = true }; if (!HasTeacherPrivilege(privilege)) { ret.IsSucceeded = false; ret.ErrorMessage = "没有权限"; return(ret); } using (var db = new ApplicationDbContext(_dbContextOptions)) { var contest = model.Id == 0 ? new Contest() : await db.Contest.FindAsync(model.Id); if (contest == null) { ret.IsSucceeded = false; ret.ErrorMessage = "比赛不存在"; return(ret); } contest.Name = model.Name; contest.Config = JsonConvert.SerializeObject(model.Config); contest.Description = model.Description; contest.Hidden = model.Hidden; contest.StartTime = model.StartTime; contest.EndTime = model.EndTime; contest.SpecifyCompetitors = model.SpecifyCompetitors; contest.Password = model.Password; if (model.Id == 0) { contest.UserId = user.Id; await db.Contest.AddAsync(contest); await db.SaveChangesAsync(); } ; db.ContestRegister.RemoveRange(db.ContestRegister.Where(i => i.ContestId == contest.Id)); if (contest.SpecifyCompetitors) { foreach (var competitor in model.Competitors) { db.ContestRegister.Add(new ContestRegister { ContestId = contest.Id, UserId = competitor.Id }); } } if (!string.IsNullOrEmpty(model.ProblemSet)) { var problemSet = model.ProblemSet.Trim().Split(';', StringSplitOptions.RemoveEmptyEntries).ToList().ConvertAll(i => { if (int.TryParse(i.Trim(), out var t)) { return(t); } return(0); }); problemSet.RemoveAll(i => i == 0); var dict = new Dictionary <int, ContestProblemConfig>(); foreach (var i in db.ContestProblemConfig.Where(i => i.ContestId == contest.Id)) { if (problemSet.Contains(i.ProblemId)) { dict[i.ProblemId] = new ContestProblemConfig { ContestId = i.ContestId, AcceptCount = i.AcceptCount, ProblemId = i.ProblemId, SubmissionCount = i.SubmissionCount }; } db.ContestProblemConfig.Remove(i); } foreach (var pid in problemSet) { if (db.Problem.Any(i => i.Id == pid)) { if (dict.ContainsKey(pid)) { db.ContestProblemConfig.Add(dict[pid]); } else { db.ContestProblemConfig.Add(new ContestProblemConfig { ProblemId = pid, ContestId = contest.Id, AcceptCount = 0, SubmissionCount = 0 }); } } } } else { db.ContestProblemConfig.RemoveRange(db.ContestProblemConfig.Where(i => i.ContestId == contest.Id)); } await db.SaveChangesAsync(); ret.Id = contest.Id; return(ret); } }