103 lines
3.8 KiB
VB.net
103 lines
3.8 KiB
VB.net
Imports System.Data.SqlClient
|
|
Imports DigitalData.Modules.Logging
|
|
Imports DigitalData.Modules.Base
|
|
Imports System.Data
|
|
|
|
Public Class EnvelopeModel
|
|
Inherits BaseModel
|
|
|
|
Public Sub New(pState As State)
|
|
MyBase.New(pState)
|
|
End Sub
|
|
|
|
Private Function ToEnvelope(pRow As DataRow) As Envelope
|
|
Dim oEnvelope = New Envelope() With {
|
|
.Id = pRow.ItemEx("GUID", 0),
|
|
.Uuid = pRow.ItemEx("ENVELOPE_UUID", ""),
|
|
.Subject = pRow.ItemEx("SUBJECT", ""),
|
|
.Message = pRow.ItemEx("MESSAGE", ""),
|
|
.UserId = State.UserId,
|
|
.Status = ObjectEx.ToEnum(Of Constants.EnvelopeStatus)(pRow.ItemEx("STATUS", "Created"))
|
|
}
|
|
|
|
Return oEnvelope
|
|
End Function
|
|
|
|
Public Function List() As IEnumerable(Of Envelope)
|
|
Try
|
|
Dim oSql = $"SELECT * FROM [dbo].[TBSIG_ENVELOPE] WHERE USER_ID = {State.UserId}"
|
|
Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
Return oTable?.Rows.Cast(Of DataRow).
|
|
Select(AddressOf ToEnvelope).
|
|
ToList()
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function Insert(pEnvelope As Envelope) As Boolean
|
|
Try
|
|
Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE] (SUBJECT, MESSAGE, ENVELOPE_UUID, STATUS, USER_ID) VALUES (@SUBJECT, @MESSAGE, @UUID, @STATUS, @USER_ID)"
|
|
Dim oCommand As New SqlCommand(oSql)
|
|
oCommand.Parameters.Add("SUBJECT", SqlDbType.NVarChar).Value = String.Empty
|
|
oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = String.Empty
|
|
oCommand.Parameters.Add("UUID", SqlDbType.NVarChar).Value = pEnvelope.Uuid
|
|
oCommand.Parameters.Add("STATUS", SqlDbType.NVarChar).Value = Constants.EnvelopeStatus.Created
|
|
oCommand.Parameters.Add("USER_ID", SqlDbType.Int).Value = pEnvelope.UserId
|
|
|
|
If Database.ExecuteNonQuery(oCommand) Then
|
|
pEnvelope.Id = GetEnvelopeId(pEnvelope)
|
|
Return True
|
|
Else
|
|
Return False
|
|
End If
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Public Function Update(pEnvelope As Envelope, pTransaction As SqlTransaction) As Boolean
|
|
Try
|
|
Dim oSql = "UPDATE [dbo].[TBSIG_ENVELOPE] SET [SUBJECT] = @SUBJECT, [MESSAGE] = @MESSAGE, [STATUS] = @STATUS WHERE GUID = @ID AND USER_ID = @USER_ID"
|
|
Dim oCommand As New SqlCommand(oSql)
|
|
oCommand.Parameters.Add("SUBJECT", SqlDbType.NVarChar).Value = pEnvelope.Subject
|
|
oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = pEnvelope.Message
|
|
oCommand.Parameters.Add("STATUS", SqlDbType.NVarChar).Value = pEnvelope.Status
|
|
|
|
oCommand.Parameters.Add("ID", SqlDbType.Int).Value = pEnvelope.Id
|
|
oCommand.Parameters.Add("USER_ID", SqlDbType.Int).Value = pEnvelope.UserId
|
|
|
|
Return Database.ExecuteNonQuery(oCommand, pTransaction)
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Public Function Delete(pEnvelope As Envelope) As Boolean
|
|
Try
|
|
Dim oSql = $"DELETE FROM [dbo].[TBSIG_ENVELOPE] WHERE GUID = {pEnvelope.Id}"
|
|
Return Database.ExecuteNonQuery(oSql)
|
|
|
|
Catch ex As Exception
|
|
Return False
|
|
Logger.Error(ex)
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetEnvelopeId(pEnvelope As Envelope) As Integer
|
|
Try
|
|
Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_ENVELOPE WHERE USER_ID = {pEnvelope.UserId}")
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
End Class
|