public IActionResult SubmitPuzzle(string id, string solution, string explanation) { int puzzleId; if (!int.TryParse(id, out puzzleId)) { return(Json(new { success = false, error = "The given ID is invalid." })); } if (string.IsNullOrWhiteSpace(solution)) { return(Json(new { success = false, error = "There are no accepted variations." })); } Puzzle puzzle = puzzlesBeingEdited.Get(puzzleId); if (puzzle == null) { return(Json(new { success = false, error = string.Format("The given puzzle (ID: {0}) cannot be published because it isn't being created.", id) })); } if (puzzle.Author != loginHandler.LoggedInUserId(HttpContext).Value) { return(Json(new { success = false, error = "Only the puzzle author can access this right now." })); } puzzle.Solutions = new List <string>(solution.Split(';').Where(x => !string.IsNullOrWhiteSpace(x))); if (puzzle.Solutions.Count == 0) { return(Json(new { success = false, error = "There are no accepted variations." })); } puzzle.Game = null; puzzle.ExplanationUnsafe = explanation; puzzle.Rating = new Rating(1500, 350, 0.06); puzzle.Reviewers = new List <int>(); if (UserRole.HasAtLeastThePrivilegesOf(loginHandler.LoggedInUser(HttpContext).Roles, UserRole.PUZZLE_REVIEWER)) { puzzle.InReview = false; puzzle.Approved = true; puzzle.Reviewers.Add(loginHandler.LoggedInUserId(HttpContext).Value); } else { puzzle.InReview = true; puzzle.Approved = false; } puzzle.DateSubmittedUtc = DateTime.UtcNow; puzzle.ID = counterRepository.GetAndIncrease(Counter.PUZZLE_ID); if (puzzleRepository.Add(puzzle)) { return(Json(new { success = true, link = Url.Action("TrainId", "Puzzle", new { id = puzzle.ID }) })); } else { return(Json(new { success = false, error = "Something went wrong." })); } }
public IActionResult PostWithHistory(Puzzle puzzle) { UserProfile userProfile = GetCurrentUserProfile(); puzzle.CurrentOwnerId = userProfile.Id; puzzle.CreateDateTime = DateTime.Now; _puzzleRepository.Add(puzzle); History history = new History(); history.StartDateOwnership = DateTime.Now; history.PuzzleId = puzzle.Id; history.UserProfileId = userProfile.Id; _historyRepository.Add(history); return(CreatedAtAction("Get", new { id = puzzle.Id }, puzzle)); }