Imports EDSL.Entities
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Configuration
Imports System.Data.Common
Public Class dbMainPage
  Private Shared strMainConnString As String = "MainConnString"
  Public Function GetPageDetails(ByVal strPageName As String) As entMainPage
    ' Used to load the page's display details into each page
    Dim objMainPage As New entMainPage
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

    Try
      Using dbCommand As DbCommand = db.GetStoredProcCommand("MainGetPageMain")
        db.AddInParameter(dbCommand, "PageMainName", DbType.String, strPageName)
        Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
        While oRecordset.Read()
          With objMainPage
            .PageID = Convert.ToString(oRecordset("PageMainID"))
            .PageName = Convert.ToString(oRecordset("PageMainName"))
            .PageContent = Convert.ToString(oRecordset("PageMainContent"))
            .PageTitle = Convert.ToString(oRecordset("PageMainTitle"))
            .PageMetaName = Convert.ToString(oRecordset("PageMainMetaName"))
            .PageMetaContent = Convert.ToString(oRecordset("PageMainMetaContent"))
            .MetaTitle = Convert.ToString(oRecordset("PageMainMetaTitle"))
            .MainImage = Convert.ToString(oRecordset("PageMainImage"))
            .MenuStrucGroup = Convert.ToString(oRecordset("MenuStrucGroup"))
            .Active = Convert.ToString(oRecordset("Active"))
          End With
        End While
        oRecordset.Close()
      End Using
      Return objMainPage
    Catch ex As Exception
      Throw ex
      Return Nothing
    End Try
  End Function
  Public Function GetItem(ByVal intPageMainID As Integer) As entMainPage
    Dim objMainPage As New entMainPage
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

    Try
      Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminPageMainGet")
        db.AddInParameter(dbCommand, "PageMainID", DbType.Int64, intPageMainID)
        Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
        While oRecordset.Read()
          With objMainPage
            .PageID = Convert.ToString(oRecordset("PageMainID"))
            .PageName = Convert.ToString(oRecordset("PageMainName"))
            .PageContent = Convert.ToString(oRecordset("PageMainContent"))
            .PageTitle = Convert.ToString(oRecordset("PageMainTitle"))
            .PageMetaName = Convert.ToString(oRecordset("PageMainMetaName"))
            .MetaTitle = Convert.ToString(oRecordset("PageMainMetaTitle"))
            .PageMetaContent = Convert.ToString(oRecordset("PageMainMetaContent"))
            .MainImage = Convert.ToString(oRecordset("PageMainImage"))
            .MenuStrucGroup = Convert.ToString(oRecordset("MenuStrucGroup"))
            .Active = Convert.ToString(oRecordset("Active"))
          End With
        End While
        oRecordset.Close()
      End Using
      Return objMainPage
    Catch ex As Exception
      Throw ex
      Return Nothing
    End Try
  End Function
  Public Function GetAll(ByVal strWHEREclause As String, ByVal strORDERBYclause As String) As List(Of entMainPage)
    Dim objMainPage As New List(Of entMainPage)
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
    Try
      Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminPageMainGetAll")
        db.AddInParameter(dbCommand, "WHEREclause", DbType.String, strWHEREclause)
        db.AddInParameter(dbCommand, "ORDERBYclause", DbType.String, strORDERBYclause)
        Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
        While oRecordset.Read()
          objMainPage.Add(New entMainPage() With { _
            .PageID = Convert.ToInt32(oRecordset("PageMainID")), _
            .PageName = Convert.ToString(oRecordset("PageMainName")), _
            .PageContent = Convert.ToString(oRecordset("PageMainContent")), _
            .PageTitle = Convert.ToString(oRecordset("PageMainTitle")), _
            .PageMetaName = Convert.ToString(oRecordset("PageMainMetaName")), _
            .MetaTitle = Convert.ToString(oRecordset("PageMainMetaTitle")), _
            .PageMetaContent = Convert.ToString(oRecordset("PageMainMetaContent")), _
            .MenuStrucGroup = Convert.ToString(oRecordset("MenuStrucGroup")), _
            .Active = Convert.ToString(oRecordset("Active")) _
          })
        End While
        oRecordset.Close()
      End Using
    Catch ex As Exception
      Throw ex
    End Try
    Return objMainPage
  End Function
  Public Function SaveItem(ByVal intPageMainID As Integer, ByVal strPageMainTitle As String, ByVal strPageMainContent As String, ByVal strPageMainMetaName As String, ByVal strPageMainMetaContent As String, ByVal strPageMainMetaTitle As String, ByVal strMainImage As String, ByVal intUserID As Integer) As Integer
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
    Dim strSPName As String = ""
    Try
      If intPageMainID = 0 Then
        strSPName = "MainAdminPageMainINSERT"
      Else
        strSPName = "MainAdminPageMainUPDATE"
      End If
      Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
        If intPageMainID <> 0 Then
          db.AddInParameter(dbCommand, "PageMainID", DbType.Int16, intPageMainID)
        End If
        db.AddInParameter(dbCommand, "PageMainTitle", DbType.String, strPageMainTitle)
        db.AddInParameter(dbCommand, "PageMainContent", DbType.String, strPageMainContent)
        db.AddInParameter(dbCommand, "PageMainMetaName", DbType.String, strPageMainMetaName)
        db.AddInParameter(dbCommand, "PageMainMetaContent", DbType.String, strPageMainMetaContent)
        db.AddInParameter(dbCommand, "PageMainMetaTitle", DbType.String, strPageMainMetaTitle)
        db.AddInParameter(dbCommand, "PageMainImage", DbType.String, strMainImage)
        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
