Imports System.Data Imports System.Data.SqlClient Imports System.Transactions Imports DigitalData.Modules.Base Imports EnvelopeGenerator.Common.My.Resources Public Class DocumentModel Inherits BaseModel Public Sub New(pState As State) MyBase.New(pState) End Sub Private Function ToDocument(pRow As DataRow) As EnvelopeDocument Return New EnvelopeDocument() With { .Id = pRow.ItemEx("GUID", 0), .EnvelopeId = pRow.ItemEx("ENVELOPE_ID", 0), .FileInfo = New IO.FileInfo(pRow.ItemEx("FILEPATH", "")), .IsTempFile = False } End Function Public Function List(pEnvelopeId As Integer) As IEnumerable(Of EnvelopeDocument) 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(AddressOf ToDocument). ToList() Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function Public Function Insert(pEnvelope As Envelope, pDocument As EnvelopeDocument, pTransaction As SqlTransaction) As Boolean Try Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE_DOCUMENT] ([FILENAME] ,[FILEPATH] ,[ENVELOPE_ID]) VALUES (@FILENAME ,@FILEPATH ,@ENVELOPE_ID)" Dim oCommand As New SqlCommand(oSql) oCommand.Parameters.Add("FILENAME", SqlDbType.NVarChar).Value = pDocument.Filename oCommand.Parameters.Add("FILEPATH", SqlDbType.NVarChar).Value = pDocument.Filepath oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.Int).Value = pEnvelope.Id 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