From 34932efb1428044caaf0dbe042fa15c73af5a526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janis=20Daniel=20Da=CC=88hne?= <janis.daehne2@student.uni-halle.de> Date: Sun, 9 Jun 2019 12:46:25 +0200 Subject: [PATCH] - some participation null checking and formatting - changed get all asset in exercise editor to take the exerciseId because we want the files from all exercises (where the user has access to) --- .../EditCustomProjectController.cs | 110 +++++++++--------- .../Core/Exercises/DoExerciseController.cs | 68 ++++++++--- .../Exercises/ExerciseEditorController.cs | 12 +- 3 files changed, 111 insertions(+), 79 deletions(-) diff --git a/src/ClientServer/Controllers/Core/CustomProjects/EditCustomProjectController.cs b/src/ClientServer/Controllers/Core/CustomProjects/EditCustomProjectController.cs index 9ce22a0..01b4244 100644 --- a/src/ClientServer/Controllers/Core/CustomProjects/EditCustomProjectController.cs +++ b/src/ClientServer/Controllers/Core/CustomProjects/EditCustomProjectController.cs @@ -77,8 +77,9 @@ namespace ClientServer.Controllers.Core.customProjects var markdownUserAssetsBasePath = Files.GetUploadFilePath(UploadDirType.UserAssets); - var markdownUserFilesDict = await Files.ReadUploadedFiles(markdownUserAssetsBasePath, markdownUserFilesIdsToRead); - + var markdownUserFilesDict = + await Files.ReadUploadedFiles(markdownUserAssetsBasePath, markdownUserFilesIdsToRead); + var customProjectForFrontend = new CustomProjectFullBase() { @@ -125,7 +126,7 @@ namespace ClientServer.Controllers.Core.customProjects Id = asset.FileReferenceUserFileAsset.Id, OriginalName = asset.FileReferenceUserFileAsset.OriginalName, MimeType = asset.FileReferenceUserFileAsset.MimeType, - SizeInBytes= asset.FileReferenceUserFileAsset.SizeInBytes, + SizeInBytes = asset.FileReferenceUserFileAsset.SizeInBytes, }).ToList(), TestSettings = new TestSettingsFullBase() { @@ -288,7 +289,7 @@ namespace ClientServer.Controllers.Core.customProjects #region tests - + //assets are managed separately var allTestTypes = await _context.TestTypes.ToListAsync(); @@ -307,13 +308,13 @@ namespace ClientServer.Controllers.Core.customProjects $"test type id {test.TestTypeId} was not found for test '{test.DisplayName}'"))); return; } - + tests.Add(new CustomProjectTest() { Content = test.Content, DisplayIndex = test.DisplayIndex, DisplayName = test.DisplayName, - TestTypeId = testType.Id, + TestTypeId = testType.Id, Weight = test.Weight, TestSettings = new TestSettings() { @@ -321,7 +322,7 @@ namespace ClientServer.Controllers.Core.customProjects TimeoutInMs = test.TestSettings.TimeoutInMs, MaxDiskSpaceInKb = test.TestSettings.MaxDiskSpaceInKb, CompileTimeoutInMs = test.TestSettings.CompileTimeoutInMs, - CompilerOptions = test.TestSettings.CompilerOptions, + CompilerOptions = test.TestSettings.CompilerOptions, }, }); } @@ -753,7 +754,7 @@ namespace ClientServer.Controllers.Core.customProjects } //assets are managed separately - + var newTest = new CustomProjectTest() { Content = testFullBase.Content, @@ -1116,13 +1117,13 @@ namespace ClientServer.Controllers.Core.customProjects if (!await base.IsLoggedIn()) return; int userId = GetUserId(); - + var customProject = await _context.CustomProjects .Include(p => p.Description) .FirstOrDefaultAsync(p => p.UserId == userId && //just ensure the project is from the current user p.Id == customProjectId) ; - + if (customProject == null) { await @@ -1130,7 +1131,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return; } - + if (customProject.Description == null) { await @@ -1138,7 +1139,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project description not found"))); return; } - + if (file == null || file.Length == 0) { await @@ -1146,7 +1147,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "no files selected"))); return; } - + var targetHardDrivePath = Files.GetUploadFilePath(UploadDirType.UserAssets); FileReferenceUserFileAsset fileRef = null; try @@ -1222,13 +1223,13 @@ namespace ClientServer.Controllers.Core.customProjects if (!await base.IsLoggedIn()) return; int userId = GetUserId(); - + var customProject = await _context.CustomProjects .Include(p => p.Description) .FirstOrDefaultAsync(p => p.UserId == userId && //just ensure the project is from the current user p.Id == customProjectId) ; - + if (customProject == null) { await @@ -1236,7 +1237,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return; } - + if (customProject.Description == null) { await @@ -1244,7 +1245,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project description not found"))); return; } - + var fileRef = await _context.FileReferenceUserFileAssets .FirstOrDefaultAsync(p => p.Id == fileReferenceId); @@ -1281,7 +1282,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.Ok, ""))); } - /// <summary> + /// <summary> /// removes the file from the custom project (custom project description) /// /// IF the file was only attached to this last custom project then the file gets deleted @@ -1296,13 +1297,13 @@ namespace ClientServer.Controllers.Core.customProjects if (!await base.IsLoggedIn()) return; int userId = GetUserId(); - + var customProject = await _context.CustomProjects .Include(p => p.Description) .FirstOrDefaultAsync(p => p.UserId == userId && //just ensure the project is from the current user p.Id == customProjectId) ; - + if (customProject == null) { await @@ -1310,7 +1311,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return; } - + if (customProject.Description == null) { await @@ -1318,7 +1319,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project description not found"))); return; } - + var assetFileReference = await _context.CustomProjectDescriptionWithFileAsAssetReferences .FirstOrDefaultAsync(p => p.FileReferenceUserFileAssetId == fileReferenceId && @@ -1344,11 +1345,11 @@ namespace ClientServer.Controllers.Core.customProjects var referenceCount = await _context.CustomProjectTestWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; - + var referenceCount2 = await _context.CustomProjectDescriptionWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; - + var referenceCount3 = await _context.CustomTestWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; @@ -1409,9 +1410,8 @@ namespace ClientServer.Controllers.Core.customProjects await Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.Ok, ""))); - } - + /// <summary> /// this is needed when we attach an existing file to an custom project then we need to content in the frontend /// </summary> @@ -1426,8 +1426,9 @@ namespace ClientServer.Controllers.Core.customProjects int userId = GetUserId(); - var assetForFrontend = await _GetSingleExerciseDescriptionAssetFile(userId, customProjectId, fileReferenceId, false); - + var assetForFrontend = + await _GetSingleExerciseDescriptionAssetFile(userId, customProjectId, fileReferenceId, false); + if (assetForFrontend == null) return; await @@ -1437,41 +1438,41 @@ namespace ClientServer.Controllers.Core.customProjects } - internal async Task<FileWithData> _GetSingleExerciseDescriptionAssetFile(int userId, int customProjectId, int fileReferenceId, bool isFileDownload) + internal async Task<FileWithData> _GetSingleExerciseDescriptionAssetFile(int userId, int customProjectId, + int fileReferenceId, bool isFileDownload) { var customProject = await _context.CustomProjects .Include(p => p.Description) .FirstOrDefaultAsync(p => p.UserId == userId && //just ensure the project is from the current user p.Id == customProjectId) ; - + if (customProject == null) { - if (isFileDownload) { Response.StatusCode = 406; } - + await Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return null; } - + if (customProject.Description == null) { if (isFileDownload) { Response.StatusCode = 406; } - + await Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project description not found"))); return null; } - + //file should only be read if it is connected to the exercise var connection = await _context.CustomProjectDescriptionWithFileAsAssetReferences @@ -1487,7 +1488,7 @@ namespace ClientServer.Controllers.Core.customProjects { Response.StatusCode = 406; } - + await Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.NotFound, @@ -1503,7 +1504,7 @@ namespace ClientServer.Controllers.Core.customProjects { Response.StatusCode = 406; } - + await Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "file was not found"))); @@ -1526,7 +1527,6 @@ namespace ClientServer.Controllers.Core.customProjects return assetForFrontend; } - #endregion #region custom project custom test assets @@ -1580,8 +1580,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "no files selected"))); return; } - - + var targetHardDrivePath = Files.GetUploadFilePath(UploadDirType.UserAssets); FileReferenceUserFileAsset fileRef = null; @@ -1654,9 +1653,9 @@ namespace ClientServer.Controllers.Core.customProjects /// <param name="fileReferenceId"></param> /// <returns></returns> [HttpPost("upload/attach/custom/tests/{customProjectId}/{customTestId}/{fileReferenceId}")] - public async Task AttachExitingFileToCustomProjectCustomTest(int customProjectId, int customTestId, int fileReferenceId) + public async Task AttachExitingFileToCustomProjectCustomTest(int customProjectId, int customTestId, + int fileReferenceId) { - if (!await base.IsLoggedIn()) return; int userId = GetUserId(); @@ -1673,7 +1672,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return; } - + var oldTest = await _context.CustomProjectTests .Where(p => p.Id == customTestId && p.CustomProjectId == customProjectId) @@ -1687,7 +1686,7 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "test not found"))); return; } - + var fileRef = await _context.FileReferenceUserFileAssets .FirstOrDefaultAsync(p => p.Id == fileReferenceId); @@ -1732,9 +1731,8 @@ namespace ClientServer.Controllers.Core.customProjects Response.WriteAsync( Jc.Serialize(new BasicResponseWithData<FilePreviewFromBackend>(ResponseCode.Ok, "", filePreview))); } - - - + + /// <summary> /// removes the file from the custom test /// @@ -1746,7 +1744,8 @@ namespace ClientServer.Controllers.Core.customProjects /// <param name="fileReferenceId"></param> /// <returns></returns> [HttpDelete("upload/detach/custom/tests/{customProjectId}/{customTestId}/{fileReferenceId}")] - public async Task DetachOrDeleteFileFromCustomProjectCustomTest(int customProjectId, int customTestId, int fileReferenceId) + public async Task DetachOrDeleteFileFromCustomProjectCustomTest(int customProjectId, int customTestId, + int fileReferenceId) { if (!await base.IsLoggedIn()) return; @@ -1764,9 +1763,8 @@ namespace ClientServer.Controllers.Core.customProjects Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "custom project not found"))); return; } - - + var oldTest = await _context.CustomProjectTests .Where(p => p.Id == customTestId && p.CustomProjectId == customProjectId) @@ -1781,8 +1779,9 @@ namespace ClientServer.Controllers.Core.customProjects return; } - var assetFileReference = await _context.CustomProjectTestWithFileAsAssetReferences.FirstOrDefaultAsync(p => - p.FileReferenceUserFileAssetId == fileReferenceId && p.CustomProjectTestId == oldTest.Id); + var assetFileReference = await _context.CustomProjectTestWithFileAsAssetReferences + .FirstOrDefaultAsync(p => + p.FileReferenceUserFileAssetId == fileReferenceId && p.CustomProjectTestId == oldTest.Id); if (assetFileReference == null) { @@ -1802,11 +1801,11 @@ namespace ClientServer.Controllers.Core.customProjects var referenceCount = await _context.CustomProjectTestWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; - + var referenceCount2 = await _context.CustomProjectDescriptionWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; - + var referenceCount3 = await _context.CustomTestWithFileAsAssetReferences .CountAsync(p => p.FileReferenceUserFileAssetId == assetFileReference.FileReferenceUserFileAssetId) ; @@ -1868,9 +1867,8 @@ namespace ClientServer.Controllers.Core.customProjects Response.WriteAsync( Jc.Serialize(new BasicResponse(ResponseCode.Ok, ""))); } - - #endregion + #endregion } diff --git a/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs b/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs index 0c2eec8..9038691 100644 --- a/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs +++ b/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs @@ -2782,8 +2782,8 @@ namespace ClientServer.Controllers.Core.Exercises return; } - var oldCustomTest = - oldExerciseReleaseWithUserAsParticipation.CustomTests.FirstOrDefault(p => p.Id == customTestId); + var oldCustomTest = oldExerciseReleaseWithUserAsParticipation.CustomTests + .FirstOrDefault(p => p.Id == customTestId); if (oldCustomTest == null) { @@ -2837,12 +2837,25 @@ namespace ClientServer.Controllers.Core.Exercises p.UserId == userId && p.ExerciseRelease.GeneratedCode == generatedCode); - var exerciseTuple = - await _context.Exercises - .Where(p => p.Id == oldExerciseReleaseWithUserAsParticipation.ExerciseRelease.ExerciseId) - .Select(p => new - {p.CanUserCreateCustomTests, p.IsPermanentlyLocked, p.IsOnlyVisibleToOwner, p.UserId}) - .FirstOrDefaultAsync() + if (oldExerciseReleaseWithUserAsParticipation == null) + { + await + Response.WriteAsync( + Jc.Serialize( + new BasicResponse(ResponseCode.NotFound, "participation not found"))); + return; + } + + var exerciseTuple = await _context.Exercises + .Where(p => p.Id == oldExerciseReleaseWithUserAsParticipation.ExerciseRelease.ExerciseId) + .Select(p => new + { + p.CanUserCreateCustomTests, + p.IsPermanentlyLocked, + p.IsOnlyVisibleToOwner, + p.UserId + }) + .FirstOrDefaultAsync() ; if (exerciseTuple == null) @@ -3002,11 +3015,25 @@ namespace ClientServer.Controllers.Core.Exercises .FirstOrDefaultAsync(p => p.UserId == userId && p.ExerciseRelease.GeneratedCode == generatedCode); + if (oldExerciseReleaseWithUserAsParticipation == null) + { + await + Response.WriteAsync( + Jc.Serialize( + new BasicResponse(ResponseCode.NotFound, "participation not found"))); + return; + } + var exerciseTuple = await _context.Exercises .Where(p => p.Id == oldExerciseReleaseWithUserAsParticipation.ExerciseRelease.ExerciseId) .Select(p => new - {p.CanUserCreateCustomTests, p.IsPermanentlyLocked, p.IsOnlyVisibleToOwner, p.UserId}) + { + p.CanUserCreateCustomTests, + p.IsPermanentlyLocked, + p.IsOnlyVisibleToOwner, + p.UserId + }) .FirstOrDefaultAsync() ; @@ -3143,6 +3170,14 @@ namespace ClientServer.Controllers.Core.Exercises .FirstOrDefaultAsync(p => p.UserId == userId && p.ExerciseRelease.GeneratedCode == generatedCode); + if (oldExerciseReleaseWithUserAsParticipation == null) + { + await + Response.WriteAsync( + Jc.Serialize( + new BasicResponse(ResponseCode.NotFound, "participation not found"))); + return; + } var exerciseTuple = await _context.Exercises @@ -3306,7 +3341,7 @@ namespace ClientServer.Controllers.Core.Exercises /// <summary> - /// returns all possible uploaded file ids the user can use TODO + /// returns all possible uploaded file ids the user can use /// </summary> /// <returns></returns> [HttpGet("upload/get/custom")] @@ -3324,7 +3359,8 @@ namespace ClientServer.Controllers.Core.Exercises .ToListAsync() ; - var customProjectDescriptionFileReferences = await _context.CustomProjectDescriptionWithFileAsAssetReferences + var customProjectDescriptionFileReferences = await _context + .CustomProjectDescriptionWithFileAsAssetReferences .Include(p => p.FileReferenceUserFileAsset) .Where(p => p.CustomProjectDescription.CustomProject.UserId == userId) .ToListAsync() @@ -3341,7 +3377,7 @@ namespace ClientServer.Controllers.Core.Exercises customTestFileReferences = customTestFileReferences.DistinctBy(p => p.FileReferenceUserFileAssetId).ToList(); - + foreach (var fileReference in customTestFileReferences) { allFileReferencesForFrontend.Add(new FilePreviewFromBackend() @@ -3355,7 +3391,7 @@ namespace ClientServer.Controllers.Core.Exercises customProjectDescriptionFileReferences = customProjectDescriptionFileReferences.DistinctBy(p => p.FileReferenceUserFileAssetId).ToList(); - + foreach (var fileReference in customProjectDescriptionFileReferences) { allFileReferencesForFrontend.Add(new FilePreviewFromBackend() @@ -3366,10 +3402,10 @@ namespace ClientServer.Controllers.Core.Exercises MimeType = fileReference.FileReferenceUserFileAsset.MimeType }); } - + customProjectCustomTestFileReferences = customProjectCustomTestFileReferences.DistinctBy(p => p.FileReferenceUserFileAssetId).ToList(); - + foreach (var fileReference in customProjectCustomTestFileReferences) { allFileReferencesForFrontend.Add(new FilePreviewFromBackend() @@ -3380,7 +3416,7 @@ namespace ClientServer.Controllers.Core.Exercises MimeType = fileReference.FileReferenceUserFileAsset.MimeType }); } - + await Response.WriteAsync( diff --git a/src/ClientServer/Controllers/Core/Exercises/ExerciseEditorController.cs b/src/ClientServer/Controllers/Core/Exercises/ExerciseEditorController.cs index b52c7ca..2c85385 100644 --- a/src/ClientServer/Controllers/Core/Exercises/ExerciseEditorController.cs +++ b/src/ClientServer/Controllers/Core/Exercises/ExerciseEditorController.cs @@ -1964,10 +1964,9 @@ namespace ClientServer.Controllers.Core.Exercises /// <summary> /// returns all possible uploaded file ids the user can use TODO /// </summary> - /// <param name="exerciseId"></param> /// <returns></returns> - [HttpGet("upload/get/description/all/{exerciseId}")] - public async Task GetPossibleExerciseDescriptionFiles(int exerciseId) + [HttpGet("upload/get/description/all")] + public async Task GetPossibleExerciseDescriptionFiles() { if (!await base.IsLoggedIn()) return; @@ -2377,12 +2376,11 @@ namespace ClientServer.Controllers.Core.Exercises } /// <summary> - /// returns all possible uploaded file ids the user can use TODO + /// returns all possible uploaded file ids the user can use /// </summary> - /// <param name="exerciseId"></param> /// <returns></returns> - [HttpGet("upload/get/tests/all/{exerciseId}")] - public async Task GetPossibleExerciseTestFiles(int exerciseId) + [HttpGet("upload/get/tests/all")] + public async Task GetPossibleExerciseTestFiles() { if (!await base.IsLoggedIn()) return; -- GitLab