using console_spo_utils.Enums; using console_spo_utils.Interfaces.Repositories; using console_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; namespace console_spo_utils.Services; internal class SiteService : ISiteService { private readonly ILogger logger; private readonly ISharePointAuthenticationManager authMgr; private readonly ISharePointCustomOperation spc; private readonly ISiteOptions siteOptions; private readonly ISubSiteService subSiteService; private readonly IOneNoteService oneNoteService; private readonly IProjectSettingsRepository projectSettingsRepository; private readonly IProjectDocLibraryService projectDocLibraryService; private readonly IProjectQuickMenuService projectQuickMenuService; private readonly ITenantService tenantService; private readonly IProjectYearService projectYearService; public SiteService( ILogger logger, ISharePointAuthenticationManager authMgr, ISharePointCustomOperation spc, ISiteOptions siteOptions, ISubSiteService subSiteService, IOneNoteService oneNoteService, IProjectSettingsRepository projectSettingsRepository, IProjectDocLibraryService projectDocLibraryService, IProjectQuickMenuService projectQuickMenuService, ITenantService tenantService, IProjectYearService projectYearService) { this.logger = logger; this.authMgr = authMgr; this.spc = spc; this.siteOptions = siteOptions; this.subSiteService = subSiteService; this.oneNoteService = oneNoteService; this.projectSettingsRepository = projectSettingsRepository; this.projectDocLibraryService = projectDocLibraryService; this.projectQuickMenuService = projectQuickMenuService; this.tenantService = tenantService; this.projectYearService = projectYearService; } public bool CreateProjectSiteIfNotExists() { try { var site = siteOptions.GetProjectYearSite(); var listTitle = siteOptions.GetProjListTitle(); using var ctx = authMgr.GetContext(site); if (spc.SiteExist(ctx)) { return true; } tenantService.CreateForProject(); projectYearService.CreateList(listTitle,ctx); } catch (Exception e) { logger.LogError(e, "Site Service"); return false; } return true; } public bool CreateSubSiteIfNotExists(string projName) { var projectSite = siteOptions.GetProjectYearSite(); var subProjSite = siteOptions.GetSubProjSite(projName); var siteContext = authMgr.GetContext(projectSite); var subSiteContext = authMgr.GetContext(subProjSite); if (!spc.SiteExist(siteContext)) { throw new Exception($"Site {siteContext.Url} not exists"); } if (spc.SiteExist(subSiteContext)) { logger.LogInformation($"The subsite {subSiteContext.Url} already exists"); return true; } var existSubSite = subSiteService.Create(projName, siteContext); if (!existSubSite) { return true; } oneNoteService.CreateFolderInsideProject(projName, siteContext); CreateProjectListEntry(projName, siteContext); projectDocLibraryService.Create(projName, subSiteContext); subSiteService.AddColumnsToListView(projName, subSiteContext); projectQuickMenuService.CreateForProject(projName, subSiteContext); return true; } public bool CreateQuotationSiteIfNotExists(string quotationName) { try { var site = siteOptions.GetQuotationSite(); using var ctx = authMgr.GetContext(site); if (spc.SiteExist(ctx)) { return true; } tenantService.CreateForQuotation(); } catch (Exception e) { logger.LogError(e, "Site Service"); return false; } return true; } public bool CreateNonComplianceSiteIfNotExists(string nonComplianceName) { try { var site = siteOptions.GetNonComplianceSite(); using var ctx = authMgr.GetContext(site); if (spc.SiteExist(ctx)) { return true; } tenantService.CreateForNonCompliance(); } catch (Exception e) { logger.LogError(e, "Site Service"); return false; } return true; } private void CreateProjectListEntry(string projName, ClientContext ctx) { try { var listTitle = siteOptions.GetProjListTitle(); var tenant = siteOptions.GetProjYearTenant().Replace(" ",string.Empty); var list = ctx.Web.Lists.GetByTitle(listTitle); var itemCreateInfo = new ListItemCreationInformation(); var oItem = list.AddItem(itemCreateInfo); var link = new FieldUrlValue { Url = $"/sites/{tenant}/{projName}", Description = projName }; oItem["PAL_ID_Project"] = link; oItem["PAL_Customer"] = projectSettingsRepository.DefaultCostumer(projName); oItem["PAL_DlvReason"] = projectSettingsRepository.DefaultDlvReason(projName); oItem.Update(); ctx.ExecuteQuery(); logger.LogInformation($"> La Commessa {projName} è stata aggiunta alla lista {listTitle} con successo!"); } catch (Exception ex) { logger.LogError(ex, "Create List Entry"); } } }