public async Task <IActionResult> ValidateChapterDetailDelete(string id) { if (string.IsNullOrEmpty(id)) { return(StatusCode((int)HttpStatusCode.BadRequest)); } AikaChapterDetail detail = await _chapterDetailDbAccess.GetChapterDetailById(id); ChapterDetailDeleteValidationResult validationResult = new ChapterDetailDeleteValidationResult(); validationResult.CanBeDeleted = true; if ((detail.Finish != null && detail.Finish.Count > 0) || (detail.Detail != null && detail.Detail.Count > 0) || (detail.Quest != null && detail.Quest.Count > 0) || (detail.AllDone != null && detail.AllDone.Count > 0)) { bool isDetailView = string.IsNullOrEmpty(detail.ChapterId); bool canBeDeleted = false; if (isDetailView) { canBeDeleted = await _chapterDetailDbAccess.DetailUsedInNodesCount(id, string.Empty) > 1; } if (!canBeDeleted) { validationResult.CanBeDeleted = false; validationResult.ErrorMessage = isDetailView ? _localizer["CanNotDeleteNonEmptyChapterDetail"].Value : _localizer["CanNotDeleteNonEmptyChapter"].Value; } } return(Ok(validationResult)); }
public async Task <IActionResult> CreateChapterDetail([FromBody] AikaChapterDetail chapterDetail) { GoNorthProject defaultProject = await _projectDbAccess.GetDefaultProject(); AikaChapterDetail newChapterDetail = new AikaChapterDetail(); newChapterDetail.ProjectId = defaultProject.Id; newChapterDetail.ChapterId = string.Empty; newChapterDetail.Name = chapterDetail.Name; CopyValidChapterDetailProperties(newChapterDetail, chapterDetail); await this.SetModifiedData(_userManager, newChapterDetail); bool detailSuccess = await CreateDetailsForDetail(defaultProject, newChapterDetail); if (!detailSuccess) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } newChapterDetail = await _chapterDetailDbAccess.CreateChapterDetail(newChapterDetail); // Timeline Entry await _timelineService.AddTimelineEntry(TimelineEvent.AikaChapterDetailCreated, newChapterDetail.Id); return(Ok(newChapterDetail)); }
/// <summary> /// Copies the valid chapter detail properties to a target chapter detail /// </summary> /// <param name="targetDetail">Target chapter detail to copy to</param> /// <param name="sourceDetail">Source chapter detail to copy from</param> private void CopyValidChapterDetailProperties(AikaChapterDetail targetDetail, AikaChapterDetail sourceDetail) { targetDetail.Start = GetStartNodeList(sourceDetail.Start); targetDetail.Detail = sourceDetail.Detail != null ? sourceDetail.Detail : new List <AikaChapterDetailNode>(); targetDetail.Quest = sourceDetail.Quest != null ? sourceDetail.Quest : new List <AikaQuestNode>(); targetDetail.AllDone = sourceDetail.AllDone != null ? sourceDetail.AllDone : new List <AikaAllDone>(); targetDetail.Finish = sourceDetail.Finish != null ? sourceDetail.Finish : new List <AikaFinish>(); targetDetail.Link = sourceDetail.Link != null ? sourceDetail.Link : new List <NodeLink>(); }
public async Task <IActionResult> DeleteChapterDetail(string id) { AikaChapterDetail deletedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(id); await _chapterDetailDbAccess.DeleteChapterDetail(deletedChapterDetail); // Timeline Entry await _timelineService.AddTimelineEntry(TimelineEvent.AikaChapterDetailDeleted); return(Ok(id)); }
public async Task <IActionResult> GetChapterDetail(string id) { if (string.IsNullOrEmpty(id)) { return(StatusCode((int)HttpStatusCode.BadRequest)); } AikaChapterDetail detail = await _chapterDetailDbAccess.GetChapterDetailById(id); return(Ok(detail)); }
/// <summary> /// Creates the detail views for detail nodes in a chapter detail /// </summary> /// <param name="defaultProject">Default Project</param> /// <param name="detail">Detail for which to create the views</param> /// <returns>true if successfull, else false</returns> private async Task <bool> CreateDetailsForDetail(GoNorthProject defaultProject, AikaChapterDetail detail) { // Create Detail Views _logger.LogInformation("Creating chapter detail views"); bool errorOccured = false; List <AikaChapterDetail> createdChapterDetails = new List <AikaChapterDetail>(); foreach (AikaChapterDetailNode curDetail in detail.Detail) { if (string.IsNullOrEmpty(curDetail.DetailViewId)) { try { AikaChapterDetail newChapterDetail = await CreateNewChapterDetail(defaultProject.Id, string.Empty, curDetail.Name); curDetail.DetailViewId = newChapterDetail.Id; createdChapterDetails.Add(newChapterDetail); } catch (Exception ex) { _logger.LogError(ex, "Could not create chapter detail view."); errorOccured = true; break; } } } // Rollback on error if (errorOccured) { foreach (AikaChapterDetail curDetail in createdChapterDetails) { await _chapterDetailDbAccess.DeleteChapterDetail(curDetail); } return(false); } return(true); }
/// <summary> /// Creates a new chapter detail /// </summary> /// <param name="projectId">Project Id</param> /// <param name="chapterId">Chapter Id</param> /// <param name="name">Name of the detail</param> /// <returns>Created Chapter Detail</returns> private async Task <AikaChapterDetail> CreateNewChapterDetail(string projectId, string chapterId, string name) { AikaChapterDetail newChapterDetail = new AikaChapterDetail(); newChapterDetail.ProjectId = projectId; newChapterDetail.ChapterId = chapterId; newChapterDetail.Name = name; newChapterDetail.Start = new List <AikaStart>(); newChapterDetail.Start.Add(CreateEmptyStartNode()); newChapterDetail.Detail = new List <AikaChapterDetailNode>(); newChapterDetail.Quest = new List <AikaQuestNode>(); newChapterDetail.AllDone = new List <AikaAllDone>(); newChapterDetail.Finish = new List <AikaFinish>(); newChapterDetail.Link = new List <NodeLink>(); await this.SetModifiedData(_userManager, newChapterDetail); newChapterDetail = await _chapterDetailDbAccess.CreateChapterDetail(newChapterDetail); return(newChapterDetail); }
public async Task <IActionResult> UpdateChapterDetail(string id, [FromBody] AikaChapterDetail chapterDetail) { GoNorthProject defaultProject = await _projectDbAccess.GetDefaultProject(); AikaChapterDetail updatedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(id); if (updatedChapterDetail.Detail == null) { updatedChapterDetail.Detail = new List <AikaChapterDetailNode>(); } if (chapterDetail.Detail == null) { chapterDetail.Detail = new List <AikaChapterDetailNode>(); } // Check deleted chapters List <AikaChapterDetail> chapterDetailsToDelete = new List <AikaChapterDetail>(); List <AikaChapterDetail> chapterDetailsToRename = new List <AikaChapterDetail>(); List <AikaChapterDetailNode> deletedChapterDetails = updatedChapterDetail.Detail.Where(c => !chapterDetail.Detail.Any(uc => uc.Id == c.Id)).ToList(); foreach (AikaChapterDetailNode curDeletedChapterDetail in deletedChapterDetails) { if (string.IsNullOrEmpty(curDeletedChapterDetail.DetailViewId)) { continue; } bool detailIsStillUsed = await _chapterDetailDbAccess.DetailUsedInNodesCount(curDeletedChapterDetail.DetailViewId, curDeletedChapterDetail.Id) > 0; if (detailIsStillUsed) { continue; } AikaChapterDetail deletedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(curDeletedChapterDetail.DetailViewId); if ((deletedChapterDetail.Detail != null && deletedChapterDetail.Detail.Count > 0) || (deletedChapterDetail.Quest != null && deletedChapterDetail.Quest.Count > 0) || (deletedChapterDetail.AllDone != null && deletedChapterDetail.AllDone.Count > 0) || (deletedChapterDetail.Finish != null && deletedChapterDetail.Finish.Count > 0)) { _logger.LogInformation("Tried to delete non empty chapter detail"); return(StatusCode((int)HttpStatusCode.BadRequest, _localizer["CanNotDeleteNonEmptyChapterDetail"].Value)); } chapterDetailsToDelete.Add(deletedChapterDetail); } // Check renamed chapters List <AikaChapterDetailNode> renamedChapterDetails = chapterDetail.Detail.Where(c => updatedChapterDetail.Detail.Any(uc => uc.Id == c.Id && uc.Name != c.Name)).ToList(); foreach (AikaChapterDetailNode curRenamedChapterDetail in renamedChapterDetails) { if (string.IsNullOrEmpty(curRenamedChapterDetail.DetailViewId)) { continue; } AikaChapterDetail renamedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(curRenamedChapterDetail.DetailViewId); renamedChapterDetail.Name = curRenamedChapterDetail.Name; chapterDetailsToRename.Add(renamedChapterDetail); } CopyValidChapterDetailProperties(updatedChapterDetail, chapterDetail); await this.SetModifiedData(_userManager, updatedChapterDetail); bool detailSuccess = await CreateDetailsForDetail(defaultProject, updatedChapterDetail); if (!detailSuccess) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } await _chapterDetailDbAccess.UpdateChapterDetail(updatedChapterDetail); // Rename Chapter details foreach (AikaChapterDetail curRenamedChapterDetail in chapterDetailsToRename) { await _chapterDetailDbAccess.UpdateChapterDetail(curRenamedChapterDetail); } // Remove Chapter Details for deleted chapters foreach (AikaChapterDetail curDeletedChapterDetail in chapterDetailsToDelete) { await _chapterDetailDbAccess.DeleteChapterDetail(curDeletedChapterDetail); } // Timeline Entry await _timelineService.AddTimelineEntry(TimelineEvent.AikaChapterDetailUpdated, updatedChapterDetail.Id); return(Ok(updatedChapterDetail)); }
public async Task <IActionResult> SaveChapterOverview([FromBody] AikaChapterOverview overview) { // Check Data if (overview.Chapter == null) { overview.Chapter = new List <AikaChapter>(); } if (overview.Link == null) { overview.Link = new List <NodeLink>(); } // Get Current Overview GoNorthProject defaultProject = await _projectDbAccess.GetDefaultProject(); AikaChapterOverview chapterOverview = await _chapterOverviewDbAccess.GetChapterOverviewByProjectId(defaultProject.Id); bool overviewExisted = true; List <AikaChapterDetail> chapterDetailsToDelete = new List <AikaChapterDetail>(); List <AikaChapterDetail> chapterDetailsToRename = new List <AikaChapterDetail>(); List <int> deletedChapterNumbers = new List <int>(); if (chapterOverview == null) { chapterOverview = new AikaChapterOverview(); chapterOverview.ProjectId = defaultProject.Id; overviewExisted = false; } else { // Check deleted chapter numbers deletedChapterNumbers = chapterOverview.Chapter.Where(c => !overview.Chapter.Any(uc => uc.ChapterNumber == c.ChapterNumber)).Select(c => c.ChapterNumber).ToList(); // Check deleted chapters List <AikaChapter> deletedChapters = chapterOverview.Chapter.Where(c => !overview.Chapter.Any(uc => uc.Id == c.Id)).ToList(); foreach (AikaChapter curDeletedChapter in deletedChapters) { if (string.IsNullOrEmpty(curDeletedChapter.DetailViewId)) { continue; } AikaChapterDetail deletedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(curDeletedChapter.DetailViewId); if ((deletedChapterDetail.Detail != null && deletedChapterDetail.Detail.Count > 0) || (deletedChapterDetail.Quest != null && deletedChapterDetail.Quest.Count > 0) || (deletedChapterDetail.AllDone != null && deletedChapterDetail.AllDone.Count > 0) || (deletedChapterDetail.Finish != null && deletedChapterDetail.Finish.Count > 0)) { _logger.LogInformation("Tried to delete non empty chapter"); return(StatusCode((int)HttpStatusCode.BadRequest, _localizer["CanNotDeleteNonEmptyChapter"].Value)); } chapterDetailsToDelete.Add(deletedChapterDetail); } // Check renamed chapters List <AikaChapter> renamedChapters = overview.Chapter.Where(c => chapterOverview.Chapter.Any(uc => uc.Id == c.Id && uc.Name != c.Name)).ToList(); foreach (AikaChapter curRenamedChapter in renamedChapters) { if (string.IsNullOrEmpty(curRenamedChapter.DetailViewId)) { continue; } AikaChapterDetail renamedChapterDetail = await _chapterDetailDbAccess.GetChapterDetailById(curRenamedChapter.DetailViewId); renamedChapterDetail.Name = curRenamedChapter.Name; chapterDetailsToRename.Add(renamedChapterDetail); } } // Update Data chapterOverview.Link = overview.Link != null ? overview.Link : new List <NodeLink>(); chapterOverview.Chapter = overview.Chapter != null ? overview.Chapter : new List <AikaChapter>(); // Create Detail Views _logger.LogInformation("Creating chapter detail views"); bool errorOccured = false; List <AikaChapterDetail> createdChapterDetails = new List <AikaChapterDetail>(); foreach (AikaChapter curChapter in chapterOverview.Chapter) { if (string.IsNullOrEmpty(curChapter.DetailViewId)) { try { AikaChapterDetail newChapterDetail = await CreateNewChapterDetail(defaultProject.Id, curChapter.Id, curChapter.Name); curChapter.DetailViewId = newChapterDetail.Id; createdChapterDetails.Add(newChapterDetail); } catch (Exception ex) { _logger.LogError(ex, "Could not create chapter detail view."); errorOccured = true; break; } } } // Rollback on error if (errorOccured) { foreach (AikaChapterDetail curDetail in createdChapterDetails) { await _chapterDetailDbAccess.DeleteChapterDetail(curDetail); } return(StatusCode((int)HttpStatusCode.InternalServerError)); } // Save Chapter overview await this.SetModifiedData(_userManager, chapterOverview); if (overviewExisted) { await _chapterOverviewDbAccess.UpdateChapterOverview(chapterOverview); } else { chapterOverview = await _chapterOverviewDbAccess.CreateChapterOverview(chapterOverview); } // Rename Chapter details foreach (AikaChapterDetail curRenamedChapter in chapterDetailsToRename) { await _chapterDetailDbAccess.UpdateChapterDetail(curRenamedChapter); } // Remove Chapter Details for deleted chapters foreach (AikaChapterDetail curDeletedChapterDetail in chapterDetailsToDelete) { await _chapterDetailDbAccess.DeleteChapterDetail(curDeletedChapterDetail); } // Adjust Aika markers for deleted chapters int minChapterNumber = chapterOverview.Chapter.Min(c => c.ChapterNumber); int maxChapterNumber = chapterOverview.Chapter.Max(c => c.ChapterNumber); foreach (int curChapterNumber in deletedChapterNumbers) { await AdjustKartaMapMarkersForDeletedChapter(defaultProject.Id, curChapterNumber, minChapterNumber, maxChapterNumber, chapterOverview.Chapter); } // Timeline Entry await _timelineService.AddTimelineEntry(TimelineEvent.AikaChapterOverviewUpdated); return(Ok(chapterOverview)); }