Imports EDSL.Entities
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Configuration
Imports System.Data.Common
Public Class dbMainNLR
    Private Shared strMainConnString As String = "MainConnString"
    Public Function GetAll(ByVal strWHEREclause As String, ByVal strORDERBYclause As String) As List(Of entMainNewsLetterRecipients)
        Dim objNLRs As New List(Of entMainNewsLetterRecipients)
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNLRGetAll")
                db.AddInParameter(dbCommand, "WHEREclause", DbType.String, strWHEREclause)
                db.AddInParameter(dbCommand, "ORDERBYclause", DbType.String, strORDERBYclause)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    objNLRs.Add(New entMainNewsLetterRecipients() With { _
                      .NLRID = Convert.ToInt32(oRecordset("NLRID")), _
                      .RecipientName = Convert.ToString(oRecordset("NLRName")), _
                      .RecipientEmail = Convert.ToString(oRecordset("NLREmail")), _
                      .CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate) _
                    })
                End While
                oRecordset.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
        Return objNLRs
    End Function
    Public Function GetItem(ByVal intNLRID As Integer) As entMainNewsLetterRecipients
        Dim objNLR As New entMainNewsLetterRecipients
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNLRGet")
                db.AddInParameter(dbCommand, "NLRID", DbType.Int16, intNLRID)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    With objNLR
                        .NLRID = Convert.ToInt32(oRecordset("NLRID"))
                        .RecipientName = Convert.ToString(oRecordset("NLRName"))
                        .RecipientEmail = Convert.ToString(oRecordset("NLREmail"))
                        .CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate)
                    End With
                End While
                oRecordset.Close()
            End Using
            Return objNLR
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Check to see if an email address already exists in the table
    ''' </summary>
    ''' <param name="emailAddress"></param>
    ''' <returns>true/false</returns>
    ''' <remarks></remarks>
    Public Function CheckEmailAddressExists(ByVal emailAddress As String) As Boolean
        Dim objNLR As New entMainNewsLetterRecipients
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Dim hasEmailAddress As New Boolean
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNLRCheckEmailExists")
                db.AddInParameter(dbCommand, "EmailAddress", DbType.String, emailAddress)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                If oRecordset.Read Then
                    hasEmailAddress = True
                End If
 
                oRecordset.Close()
            End Using
            Return hasEmailAddress
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function
    Public Function DeleteItem(ByVal intNLRID As Integer, ByVal intUserID As Integer) As Boolean
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNLRDelete")
                db.AddInParameter(dbCommand, "NLRID", DbType.Int16, intNLRID)
                db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
                db.ExecuteNonQuery(dbCommand)
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Function SaveItem(ByVal intNLRID As Integer, ByVal strNLRName As String, ByVal strNLREmail As String, ByVal intUserID As Integer) As Integer
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Dim strSPName As String = ""
        Try
            If intNLRID = 0 Then
                strSPName = "MainAdminNLRINSERT"
            Else
                strSPName = "MainAdminNLRUPDATE"
            End If
            Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
                If intNLRID <> 0 Then
                    db.AddInParameter(dbCommand, "NLRID", DbType.Int16, intNLRID)
                End If
                db.AddInParameter(dbCommand, "NLRName", DbType.String, strNLRName)
                db.AddInParameter(dbCommand, "NLREmail", DbType.String, strNLREmail)
                db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
                db.ExecuteNonQuery(dbCommand)
            End Using
            Return 0
        Catch ex As Exception
            Return 1
        End Try
    End Function

End Class
