Skip to content
Snippets Groups Projects
Commit c25fd22a authored by Janis Daniel Dähne's avatar Janis Daniel Dähne
Browse files

- fixes frontent nr. 89 - delete plang

parent 9e7433e7
Branches
No related tags found
No related merge requests found
......@@ -86,17 +86,20 @@ namespace ClientServer.Controllers.Core.Exercises
DisplayName = p.DisplayName,
EditorHighlightModeName = p.EditorHighlightModeName,
FileExtensionsWithDot = p.FileExtensionsWithDot,
CanBeDeleted = false
CanBeDeleted = false,
}).ToList();
foreach (var pLangForFrontend in forFrontend)
{
pLangForFrontend.CanBeDeleted = await this._GetCanPLangBeDeleted(pLangForFrontend.Id);
var reasonCannotBeDeleted = await this._GetCanPLangBeDeleted(pLangForFrontend.Id);
pLangForFrontend.CanBeDeleted = reasonCannotBeDeleted == null;
pLangForFrontend.CannotBeDeletedReason = reasonCannotBeDeleted;
}
await
Response.WriteAsync(Jc.Serialize(new BasicResponseWithData<List<PLangWithExtrasFromBackend>>(ResponseCode.Ok,
Response.WriteAsync(Jc.Serialize(new BasicResponseWithData<List<PLangWithExtrasFromBackend>>(
ResponseCode.Ok,
"", forFrontend)));
}
......@@ -111,7 +114,7 @@ namespace ClientServer.Controllers.Core.Exercises
if (await this._CheckPermission() == false) return;
if (await this.ValidatePLang(pLang) == false) return;
var newPLang = new PLang()
{
......@@ -161,7 +164,7 @@ namespace ClientServer.Controllers.Core.Exercises
Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "no old plang found")));
return;
}
if (await this.ValidatePLang(pLang) == false) return;
oldPLang.DisplayName = pLang.DisplayName;
......@@ -179,6 +182,8 @@ namespace ClientServer.Controllers.Core.Exercises
return;
}
var reasonCannotBeDeleted = await _GetCanPLangBeDeleted(oldPLang.Id);
var forFrontend = new PLangWithExtrasFromBackend()
{
Id = oldPLang.Id,
......@@ -186,15 +191,16 @@ namespace ClientServer.Controllers.Core.Exercises
InternalName = oldPLang.InternalName,
EditorHighlightModeName = oldPLang.EditorHighlightModeName,
FileExtensionsWithDot = oldPLang.FileExtensionsWithDot,
CanBeDeleted = await _GetCanPLangBeDeleted(oldPLang.Id)
CanBeDeleted = reasonCannotBeDeleted == null,
CannotBeDeletedReason = reasonCannotBeDeleted,
};
await
Response.WriteAsync(Jc.Serialize(new BasicResponseWithData<PLangWithExtrasFromBackend>(ResponseCode.Ok,
"", forFrontend)));
}
/// <summary>
/// deletes the given plang by id
/// </summary>
......@@ -219,13 +225,13 @@ namespace ClientServer.Controllers.Core.Exercises
//check if we can delete plang... only if we have no release with this plang & no code template
var canBeDeleted = await this._GetCanPLangBeDeleted(oldPLang.Id);
var reason = await this._GetCanPLangBeDeleted(oldPLang.Id);
if (canBeDeleted == false)
if (reason != null)
{
await
Response.WriteAsync(
Jc.Serialize(new BasicResponse(ResponseCode.InvalidRequest, "cannot be deleted")));
Jc.Serialize(new BasicResponse(ResponseCode.InvalidRequest, reason)));
return;
}
......@@ -254,7 +260,7 @@ namespace ClientServer.Controllers.Core.Exercises
Jc.Serialize(new BasicResponse(ResponseCode.NotFound, "display name was empty")));
return false;
}
if (string.IsNullOrEmpty(pLang.InternalName))
{
await
......@@ -266,10 +272,53 @@ namespace ClientServer.Controllers.Core.Exercises
return true;
}
protected async Task<bool> _GetCanPLangBeDeleted(int pLangId)
/// <summary>
///
/// </summary>
/// <param name="pLangId"></param>
/// <returns>null: all ok, other the reason why the plang cannot be deleted</returns>
protected async Task<string> _GetCanPLangBeDeleted(int pLangId)
{
//TODO
return false;
var releaseCount = await _context.ExerciseReleases
.CountAsync(p => p.PLangId == pLangId)
;
if (releaseCount > 0)
{
return "there are still releases with this plang";
}
var exerciseInGroupCount = await _context.Exercises
.Where(p => p.IsOnlyVisibleToOwner == false &&
p.CodeTemplates.Any(k => k.PLangId == pLangId))
.ToListAsync()
;
if (exerciseInGroupCount.Count > 0)
{
return
$"there are {exerciseInGroupCount.Count} exercises with code templates that use the given plang." +
$" the first exercise is '{exerciseInGroupCount.First().DisplayName}'";
}
var exerciseFromUser = await _context.Exercises
.Include(p => p.User)
.Where(p => p.IsOnlyVisibleToOwner &&
p.CodeTemplates.Any(k => k.PLangId == pLangId))
.ToListAsync()
;
if (exerciseFromUser.Count > 0)
{
var firstExercise = exerciseFromUser.First();
return
$"there are {exerciseFromUser.Count} exercises (that are only visible to the owner) with code templates that use the given plang." +
$" the first exercise is '{firstExercise.DisplayName}' from the user '{firstExercise.User.LastName}, {firstExercise.User.FirstName} (token: {firstExercise.User.Token})'";
}
return null;
}
}
......@@ -287,12 +336,12 @@ namespace ClientServer.Controllers.Core.Exercises
/// ALSO make sure it's a valid file path would be good to use no spaces... c++ -> cpp
/// </summary>
public string InternalName { get; set; }
/// <summary>
/// a comma separated list of file extensions for this plang
/// </summary>
public string FileExtensionsWithDot { get; set; }
/// <summary>
/// the highlight mode for the editor (currently ace)
/// </summary>
......@@ -307,5 +356,11 @@ namespace ClientServer.Controllers.Core.Exercises
/// false: deleting plang would cascade and delete other data
/// </summary>
public bool CanBeDeleted { get; set; }
/// <summary>
/// the reason why the plang cannot be deleted
/// can be null if CanBeDeleted is true
/// </summary>
public string CannotBeDeletedReason { get; set; }
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment