371 lines
11 KiB
VB.net

Imports System.IO
Imports MySql.Data.MySqlClient
Public Class person
' Person Data Variables
Private _id As Integer ' Member ID
Private _first_name As String ' First Name
Private _middle_name As String ' Middle Name
Private _last_name As String ' Last Name
Private _gender As String ' Gender
Private _birthday As Date ' Date Of Birth
Private _age As age ' Calculated From Birthday
Private _weight As weight ' Weight
Private _picture As MemoryStream ' Picture
' Person Country
Private _nationality As country ' Country Object
' Person Team / Club
Private _team As club ' Club Object
'--------------------------------------------------------------------------------------
' CONSTRUCTORS
'--------------------------------------------------------------------------------------
Public Sub New(Fname As String, Sname As String, Lname As String, DateOfBirth As String, Gender As String, Weight As String, CPR As String, Country As Integer, Mobile As String, Email As String, Address As String, Club As Integer, PictureBox As PictureBox)
' Create A New Person
CREATE(Fname, Sname, Lname, DateOfBirth, Gender, Weight, CPR, Country, Mobile, Email, Address, Club, PictureBox)
' Get Person Data From Returned ID
GetPersonData(_id)
End Sub
Public Sub New(ID As Integer)
If ID = 0 Then
ZeroID()
Else
GetPersonData(ID)
End If
End Sub
'--------------------------------------------------------------------------------------
' PROPERTIES
'--------------------------------------------------------------------------------------
Public Property ID As Integer
Get
Return _id
End Get
Set(value As Integer)
_id = value
GetPersonData(_id)
End Set
End Property
ReadOnly Property FirstName As String
Get
Return _first_name
End Get
End Property
ReadOnly Property LastName As String
Get
Return _last_name
End Get
End Property
ReadOnly Property MiddleName As String
Get
Return _first_name + " " + _last_name
End Get
End Property
ReadOnly Property ShortName As String
Get
Return _first_name + " " + _last_name
End Get
End Property
ReadOnly Property FullName As String
Get
Return _first_name + " " + _middle_name + " " + _last_name
End Get
End Property
ReadOnly Property Age As age
Get
Return _age
End Get
End Property
ReadOnly Property Birthday As Date
Get
Return _birthday
End Get
End Property
ReadOnly Property Weight As weight
Get
Return _weight
End Get
End Property
ReadOnly Property Gender As String
Get
Return _gender
End Get
End Property
ReadOnly Property Team As club
Get
Return _team
End Get
End Property
ReadOnly Property Nationality As country
Get
Return _nationality
End Get
End Property
ReadOnly Property Picture As MemoryStream
Get
Return _picture
End Get
End Property
'--------------------------------------------------------------------------------------
' Create Section
'--------------------------------------------------------------------------------------
Public Sub CREATE(Fname As String, Sname As String, Lname As String, DateOfBirth As String, Gender As String, Weight As String, CPR As String, Country As Integer, Mobile As String, Email As String, Address As String, Club As Integer, PictureBox As PictureBox)
Dim QRY As String = ("
insert into
person (fname, mname, lname, country, gender, date_of_birth, weight, cpr, mobile, email, address, club, picture)
values ('" & Fname & "', '" & Sname & "', '" & Lname & "', " & Country & ", '" & Gender & "', '" & DateOfBirth & "', '" & Weight & "', '" & CPR & "', '" & Mobile & "', '" & Email & "', '" & Address & "', " & Club & ", @picture);
select last_insert_id() as 'id';
")
' Generate Weight Object
_weight = New weight(ReadMySQLDate(DateOfBirth), Gender, CDbl(Val(Weight)))
' Connection
Dim CON As New MySqlConnection(CON_STRING)
Dim COM As New MySqlCommand(QRY, CON)
Dim Picture As New MemoryStream
PictureBox.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)
Dim arrImage() As Byte = Picture.GetBuffer
Picture.Close()
Dim RDR As MySqlDataReader
Try
CON.Open()
COM.Parameters.AddWithValue("@picture", arrImage)
RDR = COM.ExecuteReader
If RDR.HasRows = True Then
While RDR.Read
Try ' ID
_id = RDR("id")
Catch ex As Exception
_id = 0
End Try
End While
End If
CON.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
' Updating Weight
UpdateWeight(CDbl(Val(Weight)))
End Sub
'--------------------------------------------------------------------------------------
' Update Section
'--------------------------------------------------------------------------------------
Public Sub UpdatePicture(PictureBox As PictureBox)
' Query To Update
Dim QRY As String = "update person set picture = @picture where id=" & _id
' Connection
Dim CON As New MySqlConnection(CON_STRING)
Dim COM As New MySqlCommand(QRY, CON)
' Preparing The Image
Dim Picture As New MemoryStream
PictureBox.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)
Dim arrImage() As Byte = Picture.GetBuffer
Picture.Close()
Try
CON.Open()
COM.Parameters.AddWithValue("@picture", arrImage)
COM.ExecuteNonQuery()
CON.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
CON.Dispose()
End Try
End Sub
'--------------------------------------------------------------------------------------
' SUBROUTINES
'--------------------------------------------------------------------------------------
Private Sub GetPersonData(FighterID As Integer)
Dim QRY As String =
("
select
person.id as 'pid',
person.fname,
person.mname,
person.lname,
TIMESTAMPDIFF(YEAR, person.date_of_birth, CURDATE()) as 'age',
person.date_of_birth,
person.weight,
person.gender,
person.country as 'country',
clubs.id as 'club',
person.picture as 'picture'
FROM
person
left join
clubs on person.club = clubs.id
left join
countries on clubs.country = countries.id
where
person.id=" + FighterID.ToString
)
Dim CON As New MySqlConnection(CON_STRING)
Dim COM As New MySqlCommand(QRY, CON)
Dim RDR As MySqlDataReader
Try
CON.Open()
RDR = COM.ExecuteReader
If RDR.HasRows = True Then
While RDR.Read
Try ' ID
_id = RDR("pid")
Catch ex As Exception
_id = 0
End Try
Try ' First Name
_first_name = RDR("fname")
Catch ex As Exception
_first_name = Nothing
End Try
Try ' Middle Name
_middle_name = RDR("mname")
Catch ex As Exception
_middle_name = Nothing
End Try
Try ' Last Name
_last_name = RDR("lname")
Catch ex As Exception
_last_name = Nothing
End Try
Try ' Gender
_gender = RDR("gender")
Catch ex As Exception
_gender = Nothing
End Try
Try ' Birthday
_birthday = CDate(RDR("date_of_birth"))
Catch ex As Exception
_birthday = Nothing
End Try
Try ' Age
_age = New age(_birthday)
Catch ex As Exception
_age = Nothing
End Try
Try ' Weight
_weight = New weight(_birthday, _gender, RDR("weight"))
Catch ex As Exception
_weight = Nothing
End Try
Try ' Team Name
_team = New club(RDR("club"))
Catch ex As Exception
_team = Nothing
End Try
Try ' Country
_nationality = New country(RDR("country"))
Catch ex As Exception
_nationality = Nothing
End Try
Try ' Fighter Picture -------------------------------------------
Dim data As Byte() = DirectCast(RDR("picture"), Byte())
_picture = New System.IO.MemoryStream(data)
Catch ex As Exception
_picture = Nothing
End Try
End While
End If
CON.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
End Try
End Sub
'--------------------------------------------------------------------------------------
' FUNCTIONS
'--------------------------------------------------------------------------------------
Public Sub UpdateWeight(Weight As Double)
' This Updates The Weight Of The Person
_weight.UpdateWeight(Me._id, Weight)
End Sub
'--------------------------------------------------------------------------------------
' Zero ID Person
'--------------------------------------------------------------------------------------
Private Sub ZeroID()
_id = 0
_first_name = "B"
_middle_name = "Y"
_last_name = "E"
_gender = "EXGEN"
_birthday = ""
_age = Nothing
_weight = Nothing
_picture = Nothing
_team = Nothing
_nationality = Nothing
End Sub
End Class