Imports Microsoft.VisualBasic
Imports System.Data.Common

Public Class Cart
    Private _isbn As String
    Private _title As String
    Private _price As Integer
    Private _quantity As Integer
    Private _subTotal As Integer
    Property Isbn() As String
        Get
            Return Me._isbn
        End Get
        Set(ByVal value As String)
            Me._isbn = value
        End Set
    End Property
    Property Title() As String
        Get
            Return Me._title
        End Get
        Set(ByVal value As String)
            Me._title = value
        End Set
    End Property
    Property Price() As Integer
        Get
            Return Me._price
        End Get
        Set(ByVal value As Integer)
            Me._price = value
        End Set
    End Property
    Property Quantity() As Integer
        Get
            Return Me._quantity
        End Get
        Set(ByVal value As Integer)
            Me._quantity = value
        End Set
    End Property
    Property SubTotal() As Integer
        Get
            Return Me._subTotal
        End Get
        Set(ByVal value As Integer)
            Me._subTotal = value
        End Set
    End Property
    Public Function GetCartInfo() As ArrayList
        Dim list As New ArrayList()
        Dim Context As HttpContext = HttpContext.Current
        Dim Session As HttpSessionState = Context.Session
        For i As Integer = 0 To Session.Count - 1
            Dim crt As New Cart()
            crt.Isbn = Session.Keys(i)
            crt.Quantity = Session(i)
            Dim setting As ConnectionStringSettings = _
              ConfigurationManager.ConnectionStrings("MyDB")
            Dim factory As DbProviderFactory = _
              DbProviderFactories.GetFactory(setting.ProviderName)
            Using db As DbConnection = factory.CreateConnection()
                db.ConnectionString = setting.ConnectionString
                Dim comm As DbCommand = factory.CreateCommand()
                comm.CommandText = _
                  "SELECT title, price FROM Book WHERE isbn=@isbn"
                comm.Connection = db
                Dim param As DbParameter = factory.CreateParameter()
                param.ParameterName = "@isbn"
                param.Value = crt.Isbn
                comm.Parameters.Add(param)
                db.Open()
                Dim reader As DbDataReader = comm.ExecuteReader()
                If reader.Read() Then
                    crt.Title = reader.GetString(0)
                    crt.Price = reader.GetInt32(1)
                    crt.SubTotal = crt.Price * crt.Quantity
                    list.Add(crt)
                End If
            End Using
        Next
        Return list
    End Function
    Public Sub DeleteCartInfo(ByVal crt As Cart)
        Dim Context As HttpContext = HttpContext.Current
        Dim Session As HttpSessionState = Context.Session
        Session.Remove(crt.Isbn)
    End Sub
    Public Sub InsertCartInfo()
        Dim Context As HttpContext = HttpContext.Current
        Dim Session As HttpSessionState = Context.Session
        Dim setting As ConnectionStringSettings = _
          ConfigurationManager.ConnectionStrings("MyDB")
        Dim factory As DbProviderFactory = _
          DbProviderFactories.GetFactory(setting.ProviderName)
        Using db As DbConnection = factory.CreateConnection()
            db.ConnectionString = setting.ConnectionString
            Dim comm As DbCommand = factory.CreateCommand()
            comm.CommandText = _
              "INSERT INTO OrderInfo(uid,isbn,quantity) VALUES(@uid, @isbn, @quantity)"
            comm.Connection = db
            db.Open()
            For i As Integer = 0 To Session.Count - 1
                comm.Parameters.Clear()
                Dim pUid As DbParameter = factory.CreateParameter()
                pUid.ParameterName = "@uid"
                pUid.Value = Context.User.Identity.Name
                comm.Parameters.Add(pUid)
                Dim pIsbn As DbParameter = factory.CreateParameter()
                pIsbn.ParameterName = "@isbn"
                pIsbn.Value = Session.Keys(i)
                comm.Parameters.Add(pIsbn)
                Dim pQuantity As DbParameter = factory.CreateParameter()
                pQuantity.ParameterName = "@quantity"
                pQuantity.Value = Session(i)
                comm.Parameters.Add(pQuantity)
                comm.ExecuteNonQuery()
                Session.Remove(i)
            Next
        End Using
    End Sub
End Class