Beispiel #1
0
        /// <summary>
        /// Checks the references of a map if it can be deleted
        /// </summary>
        /// <param name="id">Id of the map</param>
        /// <returns>Error message if it can not be deleted, else null</returns>
        private async Task<string> CheckMapReferencesForDeletion(string id) 
        {
            List<KartaMap> kartaMaps = await _mapDbAccess.GetAllMapsMapIsMarkedIn(id);
            if(kartaMaps.Count > 0)
            {
                string markedInMaps = string.Join(", ", kartaMaps.Select(p => p.Name));
                return _localizer["CanNotDeleteMapMarkedInKartaMap", markedInMaps].Value;
            }

            List<TaleDialog> taleDialogs = await _taleDbAccess.GetDialogsObjectIsReferenced(id);
            if(taleDialogs.Count > 0)
            {
                List<KortistoNpc> npcs = await _kortistoNpcDbAccess.ResolveFlexFieldObjectNames(taleDialogs.Select(t => t.RelatedObjectId).ToList());
                string referencedInDialogs = string.Join(", ", npcs.Select(n => n.Name));
                return _localizer["CanNotDeleteMapReferencedInTaleDialog", referencedInDialogs].Value;
            }

            List<KortistoNpc> usedNpcs = await _kortistoNpcDbAccess.GetNpcsObjectIsReferencedInDailyRoutine(id);
            if(usedNpcs.Count > 0)
            {
                string referencedInNpcs = string.Join(", ", usedNpcs.Select(p => p.Name));
                return _localizer["CanNotDeleteMapReferencedInNpc", referencedInNpcs].Value;
            }

            List<AikaQuest> aikaQuests = await _questDbAccess.GetQuestsObjectIsReferenced(id);
            if(aikaQuests.Count > 0)
            {
                string referencedInQuests = string.Join(", ", aikaQuests.Select(p => p.Name));
                return _localizer["CanNotDeleteMapReferencedInAikaQuest", referencedInQuests].Value;
            }

            List<EvneSkill> referencedInSkills = await _skillDbAccess.GetSkillsObjectIsReferencedIn(id);
            if(referencedInSkills.Count > 0)
            {
                string usedInSkills = string.Join(", ", referencedInSkills.Select(m => m.Name));
                return _localizer["CanNotDeleteMapReferencedInSkill", usedInSkills].Value;
            }

            List<ObjectExportSnippet> referencedInSnippets = await _objectExportSnippetDbAccess.GetExportSnippetsObjectIsReferenced(id);
            if(referencedInSnippets.Count > 0)
            {
                List<ObjectExportSnippetReference> references = await _exportSnippetRelatedObjectNameResolver.ResolveExportSnippetReferences(referencedInSnippets, true, true, true);
                string usedInDailyRoutines = string.Join(", ", references.Select(m => string.Format("{0} ({1})", m.ObjectName, m.ExportSnippet)));
                return _localizer["CanNotDeleteMapReferencedInExportSnippet", usedInDailyRoutines].Value;
            }

            List<StateMachine> referencedInStateMachine = await _stateMachineDbAccess.GetStateMachinesObjectIsReferenced(id);
            if(referencedInStateMachine.Count > 0)
            {
                List<KortistoNpc> npcs = await _kortistoNpcDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachine.Select(t => t.RelatedObjectId).ToList());
                List<KortistoNpc> npcTemplates = await _npcTemplateDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachine.Select(t => t.RelatedObjectId).ToList());
                string usedInStateMachines = string.Join(", ", npcs.Union(npcTemplates).Select(n => n.Name));
                return _localizer["CanNotDeleteMapReferencedInStateMachines", usedInStateMachines].Value;
            }

            return null;
        }
        /// <summary>
        /// Checks if a object is referenced before a delete
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>Empty string if no references exists, error string if references exists</returns>
        protected override async Task <string> CheckObjectReferences(string id)
        {
            List <AikaQuest> aikaQuests = await _aikaQuestDbAccess.GetQuestsObjectIsReferenced(id);

            if (aikaQuests.Count > 0)
            {
                string referencedInQuests = string.Join(", ", aikaQuests.Select(p => p.Name));
                return(_localizer["CanNotDeleteNpcReferencedInAikaQuest", referencedInQuests].Value);
            }

            List <KirjaPage> kirjaPages = await _kirjaPageDbAccess.GetPagesByNpc(id);

            if (kirjaPages.Count > 0)
            {
                string mentionedInPages = string.Join(", ", kirjaPages.Select(p => p.Name));
                return(_localizer["CanNotDeleteNpcMentionedInKirjaPage", mentionedInPages].Value);
            }

            List <KartaMapMarkerQueryResult> kartaMaps = await _kartaMapDbAccess.GetAllMapsNpcIsMarkedIn(id);

            if (kartaMaps.Count > 0)
            {
                string markedInMaps = string.Join(", ", kartaMaps.Select(m => m.Name));
                return(_localizer["CanNotDeleteNpcMarkedInKartaMap", markedInMaps].Value);
            }

            List <TaleDialog> taleDialogs = await _taleDbAccess.GetDialogsObjectIsReferenced(id);

            taleDialogs = taleDialogs.Where(t => t.RelatedObjectId != id).ToList();
            if (taleDialogs.Count > 0)
            {
                List <KortistoNpc> npcs = await _objectDbAccess.ResolveFlexFieldObjectNames(taleDialogs.Select(t => t.RelatedObjectId).ToList());

                string referencedInDialogs = string.Join(", ", npcs.Select(n => n.Name));
                return(_localizer["CanNotDeleteNpcReferencedInTaleDialog", referencedInDialogs].Value);
            }

            List <KortistoNpc> referencedInDailyRoutines = await((IKortistoNpcDbAccess)_objectDbAccess).GetNpcsObjectIsReferencedInDailyRoutine(id);

            if (referencedInDailyRoutines.Count > 0)
            {
                string usedInDailyRoutines = string.Join(", ", referencedInDailyRoutines.Select(m => m.Name));
                return(_localizer["CanNotDeleteNpcUsedInDailyRoutine", usedInDailyRoutines].Value);
            }

            List <StateMachine> referencedInStateMachines = await _stateMachineDbAccess.GetStateMachinesObjectIsReferenced(id);

            if (referencedInStateMachines.Count > 0)
            {
                List <KortistoNpc> npcs = await _objectDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachines.Select(t => t.RelatedObjectId).ToList());

                List <KortistoNpc> npcTemplates = await _templateDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachines.Select(t => t.RelatedObjectId).ToList());

                string usedInStateMachines = string.Join(", ", npcs.Union(npcTemplates).Select(n => n.Name));
                return(_localizer["CanNotDeleteNpcUsedInStateMachines", usedInStateMachines].Value);
            }

            List <EvneSkill> referencedInSkills = await _skillDbAccess.GetSkillsObjectIsReferencedIn(id);

            if (referencedInSkills.Count > 0)
            {
                string referencedInSkillsString = string.Join(", ", referencedInSkills.Select(n => n.Name));
                return(_localizer["CanNotDeleteNpcUsedInSkill", referencedInSkillsString].Value);
            }

            return(string.Empty);
        }
Beispiel #3
0
        public async Task <IActionResult> DeletePage(string id)
        {
            List <KirjaPage> kirjaPages = await _pageDbAccess.GetPagesByPage(id);

            if (kirjaPages.Count > 0)
            {
                string mentionedInPages = string.Join(", ", kirjaPages.Select(p => p.Name));
                return(BadRequest(_localizer["CanNotDeletePageMentionedInOtherPages", mentionedInPages].Value));
            }

            List <KartaMapMarkerQueryResult> kartaMaps = await _kartaMapDbAccess.GetAllMapsKirjaPageIsMarkedIn(id);

            if (kartaMaps.Count > 0)
            {
                string markedInMaps = string.Join(", ", kartaMaps.Select(p => p.Name));
                return(BadRequest(_localizer["CanNotDeletePageMarkedInKartaMap", markedInMaps].Value));
            }

            List <TaleDialog> taleDialogs = await _dialogDbAccess.GetDialogsObjectIsReferenced(id);

            if (taleDialogs.Count > 0)
            {
                List <KortistoNpc> npcs = await _npcDbAccess.ResolveFlexFieldObjectNames(taleDialogs.Select(t => t.RelatedObjectId).ToList());

                string referencedInDialogs = string.Join(", ", npcs.Select(n => n.Name));
                return(BadRequest(_localizer["CanNotDeletePageReferencedInDialog", referencedInDialogs].Value));
            }

            List <KortistoNpc> usedNpcs = await _npcDbAccess.GetNpcsObjectIsReferencedInDailyRoutine(id);

            if (usedNpcs.Count > 0)
            {
                string referencedInNpcs = string.Join(", ", usedNpcs.Select(p => p.Name));
                return(BadRequest(_localizer["CanNotDeletePageReferencedInNpc", referencedInNpcs].Value));
            }

            List <AikaQuest> aikaQuests = await _questDbAccess.GetQuestsObjectIsReferenced(id);

            if (aikaQuests.Count > 0)
            {
                string referencedInQuests = string.Join(", ", aikaQuests.Select(p => p.Name));
                return(BadRequest(_localizer["CanNotDeletePageReferencedInQuest", referencedInQuests].Value));
            }

            List <EvneSkill> referencedInSkills = await _skillDbAccess.GetSkillsObjectIsReferencedIn(id);

            if (referencedInSkills.Count > 0)
            {
                string usedInSkills = string.Join(", ", referencedInSkills.Select(m => m.Name));
                return(BadRequest(_localizer["CanNotDeletePageReferencedInSkill", usedInSkills].Value));
            }

            List <ObjectExportSnippet> referencedInSnippets = await _objectExportSnippetDbAccess.GetExportSnippetsObjectIsReferenced(id);

            if (referencedInSnippets.Count > 0)
            {
                List <ObjectExportSnippetReference> references = await _exportSnippetRelatedObjectNameResolver.ResolveExportSnippetReferences(referencedInSnippets, true, true, true);

                string usedInDailyRoutines = string.Join(", ", references.Select(m => string.Format("{0} ({1})", m.ObjectName, m.ExportSnippet)));
                return(BadRequest(_localizer["CanNotDeletePageReferencedInExportSnippet", usedInDailyRoutines].Value));
            }

            List <StateMachine> referencedInStateMachines = await _stateMachineDbAccess.GetStateMachinesObjectIsReferenced(id);

            if (referencedInStateMachines.Count > 0)
            {
                List <KortistoNpc> npcs = await _npcDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachines.Select(t => t.RelatedObjectId).ToList());

                List <KortistoNpc> npcTemplates = await _npcTemplateDbAccess.ResolveFlexFieldObjectNames(referencedInStateMachines.Select(t => t.RelatedObjectId).ToList());

                string usedInStateMachines = string.Join(", ", npcs.Union(npcTemplates).Select(n => n.Name));
                return(BadRequest(_localizer["CanNotDeletePageUsedInStateMachines", usedInStateMachines].Value));
            }

            KirjaPage page = await _pageDbAccess.GetPageById(id);

            if (page.IsDefault)
            {
                return(BadRequest(_localizer["CanNotDeleteRootPage"].Value));
            }

            await _pageDbAccess.DeletePage(page);

            _logger.LogInformation("Page was deleted.");

            // Delete Images
            List <string> allImages = new List <string>();

            if (page.UplodadedImages != null)
            {
                allImages.AddRange(page.UplodadedImages);
            }

            List <KirjaPageVersion> oldVersions = await _pageVersionDbAccess.GetVersionsOfPage(page.Id, 0, int.MaxValue);

            foreach (KirjaPageVersion curVersion in oldVersions)
            {
                if (curVersion.UplodadedImages != null)
                {
                    allImages.AddRange(curVersion.UplodadedImages);
                }
            }

            allImages = allImages.Distinct().ToList();
            foreach (string curImage in allImages)
            {
                _fileAccess.DeleteFile(curImage);
            }

            // Delete Attachments
            if (page.Attachments != null)
            {
                foreach (KirjaPageAttachment curAttachment in page.Attachments)
                {
                    _fileAccess.DeleteFile(curAttachment.Filename);
                }
            }

            await _pageVersionDbAccess.DeletePageVersionsByPage(page.Id);

            await _timelineService.AddTimelineEntry(page.ProjectId, TimelineEvent.KirjaPageDeleted, page.Name);

            return(Ok(id));
        }