924cdd3d4b
Defined structure of project
165 lines
5.2 KiB
C#
165 lines
5.2 KiB
C#
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;
|
|
|
|
public class SiteService : ISiteService
|
|
{
|
|
private readonly ILogger<SiteService> 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<SiteService> 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.GetProjectSite();
|
|
var listTitle = siteOptions.GetProjListTitle();
|
|
using var ctx = authMgr.GetContext(site);
|
|
if (spc.SiteExist(ctx))
|
|
{
|
|
return true;
|
|
}
|
|
tenantService.CreateForProject();
|
|
projectYearService.ProjectsYList(listTitle,ctx);
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Site Service");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CreateSubSiteIfNotExists(string projName)
|
|
{
|
|
|
|
var projectSite = siteOptions.GetProjectSite();
|
|
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()
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
private void CreateProjectListEntry(string projName, ClientContext ctx) //TODO: PP var
|
|
{
|
|
try
|
|
{
|
|
var listTitle = siteOptions.GetProjListTitle();
|
|
var tenant = siteOptions.GetProjTenant().Replace(" ","");
|
|
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");
|
|
}
|
|
}
|
|
|
|
} |