Imports System.Data.SqlClient Imports DigitalData.Modules.Base Imports EnvelopeGenerator.Domain.Entities Public Class DocumentModel Inherits BaseModel Private ElementModel As ElementModel Public Sub New(pState As State) MyBase.New(pState) ElementModel = New ElementModel(pState) End Sub Private Function ToDocument(pRow As DataRow) As Document Return ToDocument(pRow, 0) End Function Private Function ToDocument(pRow As DataRow, pReceiverId As Integer) As Document Dim oDocumentId = pRow.ItemEx("GUID", 0) Dim byteDataObj = pRow.Item("BYTE_DATA") Return New Document() With { .Id = oDocumentId, .EnvelopeId = pRow.ItemEx("ENVELOPE_ID", 0), .Filename = pRow.ItemEx("FILENAME", ""), .Filepath = pRow.ItemEx("FILEPATH", ""), .FileNameOriginal = pRow.ItemEx("FILENAME_ORIGINAL", ""), .IsTempFile = False, .Elements = ElementModel.List(oDocumentId, pReceiverId), .ByteData = If(byteDataObj Is DBNull.Value, Nothing, DirectCast(byteDataObj, Byte())) } End Function Public Function GetById(pDocumentId As Integer) As Document Try Dim oSql = $"SELECT * FROM [dbo].[TBSIG_ENVELOPE_DOCUMENT] WHERE GUID = {pDocumentId}" Dim oTable = Database.GetDatatable(oSql) Return oTable?.Rows.Cast(Of DataRow). Select(Function(row) ToDocument(row)). Single() Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Public Function List(pEnvelopeId As Integer) As IEnumerable(Of Document) Try Dim oSql = $"SELECT * FROM [dbo].[TBSIG_ENVELOPE_DOCUMENT] WHERE ENVELOPE_ID = {pEnvelopeId}" Dim oTable = Database.GetDatatable(oSql) Return oTable?.Rows.Cast(Of DataRow). Select(Function(row) ToDocument(row)). ToList() Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Public Function List(pEnvelopeId As Integer, pReceiverId As Integer) As IEnumerable(Of Document) Try Dim oSql = $"SELECT * FROM [dbo].[TBSIG_ENVELOPE_DOCUMENT] WHERE ENVELOPE_ID = {pEnvelopeId}" Dim oTable = Database.GetDatatable(oSql) Return oTable?.Rows.Cast(Of DataRow). Select(Function(row) ToDocument(row, pReceiverId)). ToList() Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Public Function Insert(pEnvelope As Envelope, pDocument As Document, pTransaction As SqlTransaction) As Boolean Try Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE_DOCUMENT] ([FILENAME] ,[FILENAME_ORIGINAL] ,[FILEPATH] ,[ENVELOPE_ID] ,[BYTE_DATA]) VALUES (@FILENAME ,@FILENAME_ORIGINAL ,@FILEPATH ,@ENVELOPE_ID ,@BYTE_DATA)" Dim oCommand As New SqlCommand(oSql) oCommand.Parameters.Add("FILENAME", SqlDbType.NVarChar).Value = pDocument.Filename oCommand.Parameters.Add("FILENAME_ORIGINAL", SqlDbType.NVarChar).Value = pDocument.FileNameOriginal oCommand.Parameters.Add("FILEPATH", SqlDbType.NVarChar).Value = pDocument.Filepath oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.Int).Value = pEnvelope.Id oCommand.Parameters.Add(New SqlParameter("@BYTE_DATA", DirectCast(pDocument.ByteData, Object))) If Database.ExecuteNonQuery(oCommand, pTransaction) Then pDocument.EnvelopeId = pEnvelope.Id pDocument.Id = GetDocumentId(pDocument.Filename, pEnvelope, pTransaction) Return True Else Return False End If Catch ex As Exception Logger.Error(ex) Return False End Try End Function Public Function Delete(pDocumentId As Integer, pTransaction As SqlTransaction) As Boolean Try Dim oSql = "DELETE FROM [dbo].[TBSIG_ENVELOPE_DOCUMENT] WHERE GUID = @GUID" Dim oCommand As New SqlCommand(oSql) oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = pDocumentId Return Database.ExecuteNonQuery(oCommand, pTransaction) Catch ex As Exception Logger.Error(ex) Return False End Try End Function Private Function GetDocumentId(pFilename As String, pEnvelope As Envelope, pTransaction As SqlTransaction) As Integer Try Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_ENVELOPE_DOCUMENT WHERE FILENAME = '{pFilename}' AND ENVELOPE_ID = {pEnvelope.Id}", pTransaction) Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function End Class