ECMJobRunner/ECM.JobRunner.Web/Data/ImportProfileService.cs
Jonathan Jenne 7e7eee7299 23-12-2022
2022-12-23 13:27:30 +01:00

106 lines
3.2 KiB
C#

using DigitalData.Modules.Logging;
using ECM.JobRunner.Common.JobRunnerReference;
namespace ECM.JobRunner.Web.Data
{
public class ImportProfileService
{
private readonly Logger logger;
private readonly IEDMIServiceChannel channel;
public ImportProfileService(LoggingService Logging, WcfService Wcf)
{
logger = Logging.LogConfig.GetLogger();
channel = Wcf.Channel;
}
public async Task<List<ImportProfile>> GetProfiles()
{
try
{
var resp = await channel.GetJobConfigAsync();
if (resp == null) return new();
if (resp.OK == false) return new();
var profiles = resp.ProfileDefinitions.ImportProfiles.ToList();
return profiles;
}
catch (Exception ex)
{
logger.Error(ex);
return new();
}
}
public async Task<ImportProfile?> GetProfile(int pProfileId)
{
try
{
var resp = await channel.GetJobConfigAsync();
if (resp == null) return null;
if (resp.OK == false) return null;
var jobs = resp.JobDefinitions.ToList();
var profiles = resp.ProfileDefinitions.ImportProfiles.ToList();
return profiles.
Where(p => p.Id == pProfileId).
SingleOrDefault();
}
catch (Exception ex)
{
logger.Error(ex);
return null;
}
}
public async Task RunProfile(ImportProfile profile)
{
try
{
await channel.RunJobAsync(new RunJobRunJobRequest() { JobId = profile.JobId });
}
catch (Exception ex)
{
logger.Error(ex);
}
}
public async Task<bool> CreateProfile(ImportProfile profile) =>
await DoUpdateProfile(profile, UpdateProfileUpdateProfileRequest.UpdateProfileAction.Create);
public async Task<bool> UpdateProfile(ImportProfile profile) =>
await DoUpdateProfile(profile, UpdateProfileUpdateProfileRequest.UpdateProfileAction.Update);
public async Task<bool> DeleteProfile(ImportProfile profile) =>
await DoUpdateProfile(profile, UpdateProfileUpdateProfileRequest.UpdateProfileAction.Delete);
private async Task<bool> DoUpdateProfile(ImportProfile profile, UpdateProfileUpdateProfileRequest.UpdateProfileAction action)
{
try
{
var req = new UpdateProfileUpdateProfileRequest()
{
ImportProfile = profile,
Action = action
};
var resp = await channel.UpdateProfileAsync(req);
if (resp == null) return false;
if (resp.OK == false) return false;
return true;
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
}
}
}