Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMStocks_DB
   Public Class Account : Implements System.IDisposable
   Dim _disposed As Boolean = False
   Private Const m_modName As String = "FMStocks_DB.Account"
   Private m_dbh As DBHelper = Nothing
   Public Sub New()
      MyBase.New()
      Class_Initialize()
   End Sub
   Private Sub Class_Initialize()
      'must call CreateObject or COM+ will
      'not call IObjectConstructor inferface that DBHelper needs
      m_dbh = New FMStocks_DB.DBHelper
   End Sub
   Public Sub Dispose() Implements System.IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(Me)
   End Sub
   Protected Overridable Sub Dispose(disposing As Boolean)
      If _disposed Then
         Return
      End If
      If (disposing) Then
         Class_Terminate()
      End If
      _disposed = True
   End Sub
   Private Sub Class_Terminate()
      m_dbh = Nothing
   End Sub
   Public Function Add(ByVal FirstName As String,ByVal LastName As String,ByVal password As String,ByVal Email As String,ByVal Balance As Decimal) As Integer
      Add = 0

      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Add = m_dbh.RunSPReturnInteger("Account_Add",New Object(){Helpers.mp("@FirstName",System.Data.SqlDbType.VarChar,30,FirstName),Helpers.mp("@LastName",System.Data.SqlDbType.VarChar,30,LastName),Helpers.mp("@Password",System.Data.SqlDbType.VarChar,15,password),Helpers.mp("@EMail",System.Data.SqlDbType.VarChar,50,Email),Helpers.mp("@Balance",System.Data.SqlDbType.Money,8,Balance)})


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         Helpers.RaiseError(m_modName,"Add")
      End Try
   End Function
   Public Function Summary(ByVal AccountID As Integer) As gmRTL.DataLib.SqlClient.Recordset
      Summary = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         Summary = m_dbh.RunSPReturnRS("Account_Summary",New Object(){Helpers.mp("@AccountID",System.Data.SqlDbType.Int,4,AccountID)})


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         Helpers.RaiseError(m_modName,"Summary(" & AccountID & ")")
      End Try
   End Function
   Public Function VerifyUser(ByVal Email As String,ByVal password As String,ByRef AccountID As Integer,ByRef FullName As String) As Boolean
      VerifyUser = False

      Dim cmd As System.Data.SqlClient.SqlCommand = Nothing
      Dim strUserInfo As String = ""
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         ' We couldn't use the generic RunSP* functions because this stored proc has 2 output parameters
         ' The following code is a modified version of RunSPWithString to support 2 outputs and 1 return value

         cmd = New System.Data.SqlClient.SqlCommand

         'Run the procedure
         Dim connectionString As String = ""
         connectionString = m_dbh.GetConnectionString()
         cmd.Connection = new System.Data.SqlClient.SqlConnection(connectionString)
         cmd.CommandText = "Account_VerifyLogin"
         cmd.CommandType = System.Data.CommandType.StoredProcedure

         'set Params
         cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@retval",System.Data.SqlDbType.Int,System.Data.ParameterDirection.Output,0,Nothing))
         cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@EMail",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Input,50,Email))
         cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@Password",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Input,50,password))
         cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@AccountID",System.Data.SqlDbType.Int,System.Data.ParameterDirection.Output,4,Nothing))
         cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@FullName",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Output,50,Nothing))

         'execute Stored Proc
         cmd.Connection.Open()
         cmd.ExecuteNonQuery()
         cmd.Connection.Close()

         If (cmd.Parameters("@retval").Value = 0) Then
            VerifyUser = False
         Else
            AccountID = CInt(cmd.Parameters(3).Value)
            FullName = Convert.ToString(cmd.Parameters(4).Value)
            VerifyUser = True
         End If

         cmd = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         cmd = Nothing

         Helpers.RaiseError(m_modName,"VerifyUser(" & Email & ",...)" & "  [** TURN OFF FIREWALL **]")
      End Try
   End Function
   Public Sub GetAccountInfo(ByVal AccountID As Integer,ByRef FullName As String,ByRef Email As String,ByRef Balance As Decimal)
      Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()


         rs = m_dbh.RunSPReturnRS("Account_GetInfo",New Object(){Helpers.mp("AccountID",System.Data.SqlDbType.Int,4,AccountID)})

         FullName = Convert.ToString(rs.Fields("Firstname").Value) & " " & Convert.ToString(rs.Fields("Lastname").Value)
         Email = Convert.ToString(rs.Fields("email").Value)
         Balance = rs.Fields("Balance").Value

         rs = Nothing
         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)
         rs = Nothing

         Helpers.RaiseError(m_modName,"GetAccountInfo(" & AccountID & ")")
      End Try
   End Sub
   End Class
End Namespace
