Imports EDSL.Entities
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Configuration
Imports System.Data.Common
Public Class dbMainNews
    Private Shared strMainConnString As String = "MainConnString"
    Public Function GetAll(ByVal strWHEREclause As String, ByVal strORDERBYclause As String, Optional ByVal intLimit As Integer = 0) As List(Of entMainNews)
        Dim objNews As New List(Of entMainNews)
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNewsGetAll")
                db.AddInParameter(dbCommand, "WHEREclause", DbType.String, strWHEREclause)
                db.AddInParameter(dbCommand, "ORDERBYclause", DbType.String, strORDERBYclause)
                If intLimit > 0 Then
                    db.AddInParameter(dbCommand, "LIMIT", DbType.String, " TOP " & intLimit)
                Else
                    db.AddInParameter(dbCommand, "LIMIT", DbType.String, "")
                End If


                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    Dim strNewsDate As String
                    If Convert.ToString(oRecordset("NewsDate")) = "" Then
                        strNewsDate = Nothing
                    Else
                        strNewsDate = FormatDateTime(Convert.ToString(oRecordset("NewsDate")), DateFormat.ShortDate)
                    End If
                    Dim entMainNews As New entMainNews
                    With entMainNews
                        .NewsID = Convert.ToInt32(oRecordset("NewsID"))
                        .NewsHeadline = Convert.ToString(oRecordset("NewsHeadline"))
                        .NewsSummary = Convert.ToString(oRecordset("NewsSummary"))
                        .NewsText = Convert.ToString(oRecordset("NewsText"))
                        .NewsDate = strNewsDate
                        .ThumbImage = Convert.ToString(oRecordset("NewsThumbImage"))
                        .MainImage = Convert.ToString(oRecordset("NewsMainImage"))
                        .Active = Convert.ToString(oRecordset("Active"))
                        .Deleted = Convert.ToString(oRecordset("Deleted"))
                        .CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate)

                    End With
                    objNews.Add(entMainNews)
                   
                End While
                oRecordset.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
        Return objNews
    End Function
    Public Function GetItem(ByVal intNewsID As Integer) As entMainNews
        Dim objNews As New entMainNews
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNewsGet")
                db.AddInParameter(dbCommand, "NewsID", DbType.Int16, intNewsID)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    With objNews
                        Dim strNewsDate As String
                        If Convert.ToString(oRecordset("NewsDate")) = "" Then
                            strNewsDate = Nothing
                        Else
                            strNewsDate = FormatDateTime(Convert.ToString(oRecordset("NewsDate")), DateFormat.ShortDate)
                        End If
                        .NewsID = Convert.ToInt32(oRecordset("NewsID"))
                        .NewsHeadline = Convert.ToString(oRecordset("NewsHeadline"))
                        .NewsSummary = Convert.ToString(oRecordset("NewsSummary"))
                        .NewsText = Convert.ToString(oRecordset("NewsText"))
                        .NewsDate = strNewsDate
                        .ThumbImage = Convert.ToString(oRecordset("NewsThumbImage"))
                        .MainImage = Convert.ToString(oRecordset("NewsMainImage"))
                        .Active = Convert.ToString(oRecordset("Active"))
                        .Deleted = Convert.ToString(oRecordset("Deleted"))
                        .CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate)
                    End With
                End While
                oRecordset.Close()
            End Using
            Return objNews
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function
    Public Function DeleteItem(ByVal intNewsID As Integer, ByVal intUserID As Integer) As Boolean
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminNewsDelete")
                db.AddInParameter(dbCommand, "NewsID", DbType.Int16, intNewsID)
                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 intNewsID As Integer, ByVal strHeadline As String, ByVal strNewsSummary As String, ByVal strNewstext As String, ByVal strDate As String, ByVal strThumbnail As String, ByVal strMainImage As String, ByVal intActive As Integer, ByVal intUserID As Integer) As Integer
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
        Dim strSPName As String = ""
        Try
            If intNewsID = 0 Then
                strSPName = "MainAdminNewsINSERT"
            Else
                strSPName = "MainAdminNewsUPDATE"
            End If
            Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
                If intNewsID <> 0 Then
                    db.AddInParameter(dbCommand, "NewsID", DbType.Int16, intNewsID)
                End If
                db.AddInParameter(dbCommand, "NewsHeadline", DbType.String, strHeadline)
                db.AddInParameter(dbCommand, "NewsSummary", DbType.String, strNewsSummary)
                db.AddInParameter(dbCommand, "NewsText", DbType.String, strNewstext)
                If strDate = "" Then
                    'db.AddInParameter(dbCommand, "NewsDate", DbType.DateTime, DBNull.Value)
                    db.AddInParameter(dbCommand, "NewsDate", DbType.DateTime, Today())
                Else
                    db.AddInParameter(dbCommand, "NewsDate", DbType.DateTime, strDate)
                End If
                db.AddInParameter(dbCommand, "ThumbImage", DbType.String, strThumbnail)
                db.AddInParameter(dbCommand, "MainImage", DbType.String, strMainImage)
                db.AddInParameter(dbCommand, "Active", DbType.Int16, intActive)
                db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
                If intNewsID = 0 Then
                    db.AddOutParameter(dbCommand, "NewID", DbType.Int16, 255)
                End If
                db.ExecuteNonQuery(dbCommand)

                

                If intNewsID = 0 Then
                    Dim strNewID As String = db.GetParameterValue(dbCommand, "NewID")
                    Return strNewID  '@@IDENTITY
                Else
                    Return intNewsID
                End If
            End Using
            'Return 0
        Catch ex As Exception
            'Return 1
        End Try
    End Function
    Public Function GetItemSideBar(ByVal newsType As Integer) As entMainNews
        Dim objNews As New entMainNews
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainNewsArticleGetByType")
                db.AddInParameter(dbCommand, "Type", DbType.Int32, newsType)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    With objNews
                        Dim strNewsDate As String
                        If Convert.ToString(oRecordset("NewsDate")) = "" Then
                            strNewsDate = Nothing
                        Else
                            strNewsDate = FormatDateTime(Convert.ToString(oRecordset("NewsDate")), DateFormat.ShortDate)
                        End If
                        .NewsID = Convert.ToInt32(oRecordset("NewsID"))
                        .NewsHeadline = Convert.ToString(oRecordset("NewsHeadline"))
                        .NewsSummary = Convert.ToString(oRecordset("NewsSummary"))
                        .NewsText = Convert.ToString(oRecordset("NewsText"))
                        .NewsDate = strNewsDate
                        .ThumbImage = Convert.ToString(oRecordset("NewsThumbImage"))
                        .MainImage = Convert.ToString(oRecordset("NewsMainImage"))
                        .Active = Convert.ToString(oRecordset("Active"))
                        .Deleted = Convert.ToString(oRecordset("Deleted"))
                        '.CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate)
                    End With
                End While
                oRecordset.Close()
            End Using
            Return objNews
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function

    Public Function GetItemSideBar_Product(ByVal caseStudyType As Integer, ByVal productID As Integer) As entMainNews
        Dim objNews As New entMainNews
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainNewsArticleGetByProductAndType")
                db.AddInParameter(dbCommand, "Type", DbType.Int32, caseStudyType)
                db.AddInParameter(dbCommand, "productID", DbType.Int32, productID)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()
                    With objNews
                        Dim strNewsDate As String
                        If Convert.ToString(oRecordset("NewsDate")) = "" Then
                            strNewsDate = Nothing
                        Else
                            strNewsDate = FormatDateTime(Convert.ToString(oRecordset("NewsDate")), DateFormat.ShortDate)
                        End If
                        .NewsID = Convert.ToInt32(oRecordset("NewsID"))
                        .NewsHeadline = Convert.ToString(oRecordset("NewsHeadline"))
                        .NewsSummary = Convert.ToString(oRecordset("NewsSummary"))
                        .NewsText = Convert.ToString(oRecordset("NewsText"))
                        .NewsDate = strNewsDate
                        .ThumbImage = Convert.ToString(oRecordset("NewsThumbImage"))
                        .MainImage = Convert.ToString(oRecordset("NewsMainImage"))
                        .Active = Convert.ToString(oRecordset("Active"))
                        .Deleted = Convert.ToString(oRecordset("Deleted"))
                        '.CreatedDate = FormatDateTime(Convert.ToString(oRecordset("CreatedDate")), DateFormat.ShortDate)
                    End With
                End While
                oRecordset.Close()
            End Using
            Return objNews
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function

    Public Function GetItemsByType(ByVal newsType As Integer) As List(Of entMainNews)
        Dim newsList As New List(Of entMainNews)
        Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

        Try
            Using dbCommand As DbCommand = db.GetStoredProcCommand("MainNewsGetByTypeList")
                db.AddInParameter(dbCommand, "newsType", DbType.Int32, newsType)
                Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
                While oRecordset.Read()

                    Dim news As New entMainNews
                    With news
                        Dim newsDate As String
                        If Convert.ToString(oRecordset("NewsDate")) = "" Then
                            newsDate = Nothing
                        Else
                            newsDate = FormatDateTime(Convert.ToString(oRecordset("NewsDate")), DateFormat.ShortDate)
                        End If
                        .NewsID = Convert.ToInt32(oRecordset("NewsID"))
                        .NewsHeadline = Convert.ToString(oRecordset("NewsHeadline"))
                        .NewsSummary = Convert.ToString(oRecordset("NewsSummary"))
                        .NewsText = Convert.ToString(oRecordset("NewsText"))
                        .NewsDate = newsDate
                        .ThumbImage = Convert.ToString(oRecordset("NewsThumbImage"))
                        .MainImage = Convert.ToString(oRecordset("NewsMainImage"))
                        .Active = Convert.ToString(oRecordset("Active"))
                        .Deleted = Convert.ToString(oRecordset("Deleted"))

                    End With
                    newsList.Add(news)
                End While
                oRecordset.Close()
            End Using
            Return newsList
        Catch ex As Exception
            Throw ex
            Return Nothing
        End Try
    End Function
  
End Class
