public async Task <IActionResult> Retag(int id, string tags) { tags = tags ?? ""; if (Regex.IsMatch(tags, "[^0-9a-zA-Z,-]")) { return(Json(new { success = false, error = "Invalid tags. Separate your tags with commas. Tags can only contain letters and digits 0 to 9." })); } string[] tagArray = String.IsNullOrWhiteSpace(tags) ? new string[] { } : tags.ToLower().Split(','); Puzzle p = await puzzleRepository.GetAsync(id); if (p == null) { return(Json(new { success = false, error = "Puzzle not found." })); } p.Tags = tagArray; bool retagSuccess = await puzzleRepository.RetagAsync(id, tagArray); if (!retagSuccess) { return(Json(new { success = false, error = "An unexpected error while attempting to change the database occurred." })); } Task.WaitAll(tagArray.Select(x => tagRepository.MaybeAddTagAsync(p.Variant, x)).ToArray()); return(Json(new { success = true })); }