diff --git a/Open Judge System/Data/OJS.Data/IOjsData.cs b/Open Judge System/Data/OJS.Data/IOjsData.cs index c63276ac84..b6c4a0b15f 100644 --- a/Open Judge System/Data/OJS.Data/IOjsData.cs +++ b/Open Judge System/Data/OJS.Data/IOjsData.cs @@ -12,7 +12,7 @@ public interface IOjsData : IDisposable { IContestsRepository Contests { get; } - IDeletableEntityRepository Problems { get; } + IProblemsRepository Problems { get; } ITestRepository Tests { get; } diff --git a/Open Judge System/Data/OJS.Data/OJS.Data.csproj b/Open Judge System/Data/OJS.Data/OJS.Data.csproj index 619e07f5f3..7264917a83 100644 --- a/Open Judge System/Data/OJS.Data/OJS.Data.csproj +++ b/Open Judge System/Data/OJS.Data/OJS.Data.csproj @@ -91,6 +91,7 @@ + @@ -100,6 +101,7 @@ + diff --git a/Open Judge System/Data/OJS.Data/OjsData.cs b/Open Judge System/Data/OJS.Data/OjsData.cs index 88e7673226..4dd0ce65f3 100644 --- a/Open Judge System/Data/OJS.Data/OjsData.cs +++ b/Open Judge System/Data/OJS.Data/OjsData.cs @@ -29,7 +29,7 @@ public OjsData(IOjsDbContext context) public IContestsRepository Contests => (ContestsRepository)this.GetRepository(); - public IDeletableEntityRepository Problems => this.GetDeletableEntityRepository(); + public IProblemsRepository Problems => (ProblemsRepository)this.GetRepository(); public ITestRepository Tests => (TestRepository)this.GetRepository(); @@ -111,6 +111,11 @@ private IRepository GetRepository() type = typeof(ContestsRepository); } + if (typeof(T).IsAssignableFrom(typeof(Problem))) + { + type = typeof(ProblemsRepository); + } + if (typeof(T).IsAssignableFrom(typeof(Submission))) { type = typeof(SubmissionsRepository); diff --git a/Open Judge System/Data/OJS.Data/Repositories/Contracts/IProblemsRepository.cs b/Open Judge System/Data/OJS.Data/Repositories/Contracts/IProblemsRepository.cs new file mode 100644 index 0000000000..d3b320459a --- /dev/null +++ b/Open Judge System/Data/OJS.Data/Repositories/Contracts/IProblemsRepository.cs @@ -0,0 +1,14 @@ +namespace OJS.Data.Repositories.Contracts +{ + using System; + using System.Linq; + using System.Linq.Expressions; + + using OJS.Data.Contracts; + using OJS.Data.Models; + + public interface IProblemsRepository : IRepository, IDeletableEntityRepository + { + IQueryable AllIncludesSubmissionsTestsAndResources(Expression> orExpressionProblemIds); + } +} diff --git a/Open Judge System/Data/OJS.Data/Repositories/ProblemsRepository.cs b/Open Judge System/Data/OJS.Data/Repositories/ProblemsRepository.cs new file mode 100644 index 0000000000..380e3c3500 --- /dev/null +++ b/Open Judge System/Data/OJS.Data/Repositories/ProblemsRepository.cs @@ -0,0 +1,24 @@ +namespace OJS.Data.Repositories +{ + using System; + using System.Data.Entity; + using System.Linq; + using System.Linq.Expressions; + + using OJS.Data.Models; + using OJS.Data.Repositories.Base; + using OJS.Data.Repositories.Contracts; + + public class ProblemsRepository : DeletableEntityRepository, IProblemsRepository + { + public ProblemsRepository(IOjsDbContext context) + : base(context) + { + } + + public IQueryable AllIncludesSubmissionsTestsAndResources(Expression> orExpressionProblemIds) + { + return this.All().Where(orExpressionProblemIds).Include(p => p.Submissions).Include(p => p.Tests).Include(p => p.Resources); + } + } +} \ No newline at end of file diff --git a/Open Judge System/Web/OJS.Web/Areas/Administration/Controllers/ProblemsController.cs b/Open Judge System/Web/OJS.Web/Areas/Administration/Controllers/ProblemsController.cs index d5b3e7545a..3691e17c92 100644 --- a/Open Judge System/Web/OJS.Web/Areas/Administration/Controllers/ProblemsController.cs +++ b/Open Judge System/Web/OJS.Web/Areas/Administration/Controllers/ProblemsController.cs @@ -374,11 +374,32 @@ public ActionResult ConfirmDeleteAll(int? id) return this.RedirectToAction(GlobalConstants.Index); } + var orExpressionProblemIds = ExpressionBuilder.BuildOrExpression(contest.Problems.Select(p => p.Id), p => p.Id); + + var problems = this.Data.Problems.AllIncludesSubmissionsTestsAndResources(orExpressionProblemIds); + // TODO: check for N + 1 - foreach (var problem in contest.Problems.ToList()) + foreach (var problem in problems.ToList()) { - // TODO: Add cascading deletion of submissions, tests, resources this.Data.Problems.Delete(problem.Id); + + var tests = problem.Tests; + foreach (var test in tests) + { + this.Data.Tests.Delete(test.Id); + } + + var submissions = problem.Submissions; + foreach (var submission in submissions) + { + this.Data.Submissions.Delete(submission.Id); + } + + var resources = problem.Resources; + foreach (var resource in resources) + { + this.Data.Resources.Delete(resource.Id); + } } this.Data.SaveChanges();