TargetFramework update,
reference updates and added tests for Viewer
This commit is contained in:
@ -1,32 +1,32 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
|
||||
namespace OI.Metrology.Viewer.Services;
|
||||
|
||||
using OI.Metrology.Shared.DataModels;
|
||||
using OI.Metrology.Shared.Repositories;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using OI.Metrology.Shared.Services;
|
||||
using OI.Metrology.Viewer.Models;
|
||||
|
||||
public class AttachmentsService : IAttachmentsService
|
||||
{
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly IMetrologyRepository _MetrologyRepository;
|
||||
|
||||
private readonly IMetrologyRepo _Repo;
|
||||
|
||||
public AttachmentsService(AppSettings appSettings, IMetrologyRepo repo)
|
||||
public AttachmentsService(AppSettings appSettings, IMetrologyRepository metrologyRepository)
|
||||
{
|
||||
_AppSettings = appSettings;
|
||||
_Repo = repo;
|
||||
_MetrologyRepository = metrologyRepository;
|
||||
}
|
||||
|
||||
protected Stream GetAttachmentStream(string tableName, Guid attachmentId, string filename)
|
||||
protected Stream GetAttachmentStream(string? tableName, Guid attachmentId, string filename)
|
||||
{
|
||||
if (attachmentId.Equals(Guid.Empty))
|
||||
throw new Exception("No attachments found");
|
||||
|
||||
DateTime insertDate = Convert.ToDateTime(_Repo.GetAttachmentInsertDateByGUID(tableName, attachmentId));
|
||||
if (tableName is null)
|
||||
throw new NullReferenceException(nameof(tableName));
|
||||
|
||||
DateTime insertDate = Convert.ToDateTime(_MetrologyRepository.GetAttachmentInsertDateByGUID(tableName, attachmentId));
|
||||
int year = insertDate.Year;
|
||||
DateTime d = insertDate;
|
||||
CultureInfo cul = CultureInfo.CurrentCulture;
|
||||
@ -50,28 +50,28 @@ public class AttachmentsService : IAttachmentsService
|
||||
|
||||
public Stream GetAttachmentStreamByTitle(ToolType toolType, bool header, string title, string filename)
|
||||
{
|
||||
if (toolType == null)
|
||||
if (toolType is null)
|
||||
throw new Exception("Invalid tool type");
|
||||
Guid attachmentId;
|
||||
string tableName;
|
||||
string? tableName;
|
||||
if (header)
|
||||
{
|
||||
tableName = toolType.HeaderTableName;
|
||||
attachmentId = _Repo.GetHeaderAttachmentIDByTitle(toolType.ID, title);
|
||||
attachmentId = _MetrologyRepository.GetHeaderAttachmentIDByTitle(toolType.ID, title);
|
||||
}
|
||||
else
|
||||
{
|
||||
tableName = toolType.DataTableName;
|
||||
attachmentId = _Repo.GetDataAttachmentIDByTitle(toolType.ID, title);
|
||||
attachmentId = _MetrologyRepository.GetDataAttachmentIDByTitle(toolType.ID, title);
|
||||
}
|
||||
return GetAttachmentStream(tableName, attachmentId, filename);
|
||||
}
|
||||
|
||||
public Stream GetAttachmentStreamByAttachmentId(ToolType toolType, bool header, Guid attachmentId, string filename)
|
||||
{
|
||||
if (toolType == null)
|
||||
if (toolType is null)
|
||||
throw new Exception("Invalid tool type");
|
||||
string tableName;
|
||||
string? tableName;
|
||||
if (header)
|
||||
tableName = toolType.HeaderTableName;
|
||||
else
|
||||
@ -79,27 +79,27 @@ public class AttachmentsService : IAttachmentsService
|
||||
return GetAttachmentStream(tableName, attachmentId, filename);
|
||||
}
|
||||
|
||||
private void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, Microsoft.AspNetCore.Http.IFormFile uploadedFile)
|
||||
private void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, IFormFile uploadedFile)
|
||||
{
|
||||
if (toolType == null)
|
||||
if (toolType is null)
|
||||
throw new Exception("Invalid tool type");
|
||||
|
||||
using System.Transactions.TransactionScope trans = _Repo.StartTransaction();
|
||||
using System.Transactions.TransactionScope trans = _MetrologyRepository.StartTransaction();
|
||||
Guid attachmentId = Guid.Empty;
|
||||
DateTime insertDate = new();
|
||||
string tableName = "";
|
||||
string? tableName = "";
|
||||
|
||||
if (string.IsNullOrWhiteSpace(dataUniqueId))
|
||||
{
|
||||
attachmentId = _Repo.GetHeaderAttachmentID(toolType.ID, headerId);
|
||||
insertDate = Convert.ToDateTime(_Repo.GetHeaderInsertDate(toolType.ID, headerId));
|
||||
attachmentId = _MetrologyRepository.GetHeaderAttachmentID(toolType.ID, headerId);
|
||||
insertDate = Convert.ToDateTime(_MetrologyRepository.GetHeaderInsertDate(toolType.ID, headerId));
|
||||
tableName = toolType.HeaderTableName;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
attachmentId = _Repo.GetDataAttachmentID(toolType.ID, headerId, dataUniqueId);
|
||||
insertDate = Convert.ToDateTime(_Repo.GetDataInsertDate(toolType.ID, headerId, dataUniqueId));
|
||||
attachmentId = _MetrologyRepository.GetDataAttachmentID(toolType.ID, headerId, dataUniqueId);
|
||||
insertDate = Convert.ToDateTime(_MetrologyRepository.GetDataInsertDate(toolType.ID, headerId, dataUniqueId));
|
||||
// Get Date for new directory name
|
||||
tableName = toolType.DataTableName;
|
||||
|
||||
@ -131,7 +131,7 @@ public class AttachmentsService : IAttachmentsService
|
||||
|
||||
public void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, object uploadedFile)
|
||||
{
|
||||
Microsoft.AspNetCore.Http.IFormFile formFile = (Microsoft.AspNetCore.Http.IFormFile)uploadedFile;
|
||||
IFormFile formFile = (IFormFile)uploadedFile;
|
||||
SaveAttachment(toolType, headerId, dataUniqueId, filename, formFile);
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,17 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OI.Metrology.Shared.DataModels;
|
||||
using OI.Metrology.Shared.Repositories;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using OI.Metrology.Shared.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
#pragma warning disable CS8600, CS8602, CS8603, CS8604, CS8625
|
||||
|
||||
namespace OI.Metrology.Viewer.Services;
|
||||
|
||||
public class InboundDataService : IInboundDataService
|
||||
{
|
||||
private readonly IMetrologyRepo _Repo;
|
||||
private readonly IMetrologyRepository _MetrologyRepository;
|
||||
|
||||
public InboundDataService(IMetrologyRepo repo) => _Repo = repo;
|
||||
public InboundDataService(IMetrologyRepository metrologyRepository) => _MetrologyRepository = metrologyRepository;
|
||||
|
||||
public long DoSQLInsert(JToken jsonbody, ToolType toolType, List<ToolTypeMetadata> metaData)
|
||||
{
|
||||
@ -37,11 +36,11 @@ public class InboundDataService : IInboundDataService
|
||||
|
||||
long headerId = 0;
|
||||
|
||||
using (System.Transactions.TransactionScope transScope = _Repo.StartTransaction())
|
||||
using (System.Transactions.TransactionScope transScope = _MetrologyRepository.StartTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
_Repo.PurgeExistingData(toolType.ID, uniqueId);
|
||||
_MetrologyRepository.PurgeExistingData(toolType.ID, uniqueId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -50,7 +49,7 @@ public class InboundDataService : IInboundDataService
|
||||
|
||||
try
|
||||
{
|
||||
headerId = _Repo.InsertToolDataJSON(jsonbody, -1, metaData, toolType.HeaderTableName);
|
||||
headerId = _MetrologyRepository.InsertToolDataJSON(jsonbody, -1, metaData, toolType.HeaderTableName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -60,11 +59,11 @@ public class InboundDataService : IInboundDataService
|
||||
int detailrow = 1;
|
||||
try
|
||||
{
|
||||
if (detailsArray != null)
|
||||
if (detailsArray is not null)
|
||||
{
|
||||
foreach (JToken detail in detailsArray)
|
||||
{
|
||||
_ = _Repo.InsertToolDataJSON(detail, headerId, metaData, toolType.DataTableName);
|
||||
_ = _MetrologyRepository.InsertToolDataJSON(detail, headerId, metaData, toolType.DataTableName);
|
||||
detailrow += 1;
|
||||
}
|
||||
}
|
||||
@ -133,7 +132,7 @@ public class InboundDataService : IInboundDataService
|
||||
|
||||
if (jp.First is JArray array)
|
||||
detailsArray = array;
|
||||
else if ((jp.First is JValue value) && (value.Value == null))
|
||||
else if ((jp.First is JValue value) && (value.Value is null))
|
||||
detailsArray = null;
|
||||
else
|
||||
errors.Add("Invalid details field");
|
||||
@ -169,7 +168,7 @@ public class InboundDataService : IInboundDataService
|
||||
}
|
||||
|
||||
// if a Details container if found, process it by recursion
|
||||
if (detailsArray != null)
|
||||
if (detailsArray is not null)
|
||||
{
|
||||
int i = 1;
|
||||
foreach (JToken detail in detailsArray)
|
||||
@ -189,7 +188,7 @@ public class InboundDataService : IInboundDataService
|
||||
{
|
||||
// get the json data for this container field, ex: Points
|
||||
JProperty contJP = jsonbody.Children<JProperty>().Where(jp => string.Equals(jp.Name, containerField, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
|
||||
if ((contJP != null) && (contJP.Value is JArray array))
|
||||
if ((contJP is not null) && (contJP.Value is JArray array))
|
||||
{
|
||||
JArray contJPArray = array;
|
||||
|
||||
|
30
Viewer/Services/SQLDbConnectionFactory.cs
Normal file
30
Viewer/Services/SQLDbConnectionFactory.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using OI.Metrology.Shared.Repositories;
|
||||
using OI.Metrology.Viewer.Models;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
#pragma warning disable CS8600, CS8602, CS8603, CS8604, CS8625
|
||||
|
||||
namespace OI.Metrology.Viewer.Services;
|
||||
|
||||
public class SQLDbConnectionFactory : IDbConnectionFactory
|
||||
{
|
||||
private readonly AppSettings _AppSettings;
|
||||
|
||||
public SQLDbConnectionFactory(AppSettings appSettings) => _AppSettings = appSettings;
|
||||
|
||||
public DbConnection GetDbConnection()
|
||||
{
|
||||
DbProviderFactories.RegisterFactory(
|
||||
typeof(SqlConnection).Namespace,
|
||||
SqlClientFactory.Instance);
|
||||
|
||||
if (string.IsNullOrEmpty(_AppSettings.ConnectionString))
|
||||
throw new Exception("Connection string is missing");
|
||||
|
||||
DbConnection c = SqlClientFactory.Instance.CreateConnection();
|
||||
c.ConnectionString = _AppSettings.ConnectionString;
|
||||
c.Open();
|
||||
return c;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user