' MiniHtml Model class
	Private DB As MiniORM
End Sub

Public Sub Initialize
	DB = Main.DB
End Sub

Public Sub GetRowById (Id As Int) As Map	
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Columns = Array("id", "category_id", "$endpoint$_code", "$endpoint$_name")
	DB.Condition = "id = ?"
	DB.Parameter = Id
	DB.Query
	If DB.Found Then
		Return DB.First
	End If
	Return CreateMap()
End Sub

Public Sub GetRowsByCategoryId (Category_Id As Int) As List	
	DB.Open
	DB.Table = "tbl_$endpoints$ p"
	DB.Columns = Array("p.id", "p.category_id", "c.category_name", "p.$endpoint$_code", "p.$endpoint$_name")
	DB.Join("", "tbl_categories c", Array("p.category_id = c.id"))
	DB.Condition = "c.id = ?"
	DB.Parameter = Category_Id
	DB.OrderBy = CreateMap("p.id": "")
	DB.Query
	Return DB.Results
End Sub

Public Sub FindRowById (Id As Int) As Boolean
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Find(Id)
	Return DB.Found
End Sub

Public Sub FindRowBy$Endpoint$Code (Code As String) As Boolean
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Conditions = Array("$endpoint$_code = ?")
	DB.Parameters = Array(Code)
	DB.Query
	Return DB.Found
End Sub

Public Sub FindRowBy$Endpoint$CodeNotEqualId (Code As String, Id As Int) As Boolean
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Conditions = Array("$endpoint$_code = ?", "id <> ?")
	DB.Parameters = Array(Code, Id)
	DB.Query
	Return DB.Found
End Sub

Public Sub Search (keyword As String) As List
	DB.Open
	DB.Table = "tbl_$endpoints$ p"
	DB.Columns = Array("p.id", "p.category_id", "c.category_name", "p.$endpoint$_code", "p.$endpoint$_name", "p.$endpoint$_price")
	DB.Join("", "tbl_categories c", Array("p.category_id = c.id"))
	If keyword <> "" Then
		DB.Conditions = Array("UPPER(p.$endpoint$_code) LIKE ? Or UPPER(p.$endpoint$_name) LIKE ? Or UPPER(c.category_name) LIKE ?")
		DB.Parameters = Array("%" & keyword.ToUpperCase & "%", "%" & keyword.ToUpperCase & "%", "%" & keyword.ToUpperCase & "%")
	End If
	DB.OrderBy = CreateMap("p.id": "DESC")
	DB.Query
	Return DB.Results
End Sub

Public Sub Found As Boolean
	Return DB.Found
End Sub

Public Sub First As Map
	Return DB.First
End Sub

Public Sub Error As Exception
	Return DB.Error
End Sub

Public Sub Create (Category As Int, Code As String, Name As String, Price As Double, Created_Date As String)
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Columns = Array("category_id", "$endpoint$_code", "$endpoint$_name", "created_date")
	DB.Parameters = Array(Category, Code, Name, Price, Created_Date)
	DB.ReturnRow = True
	DB.Save
End Sub

Public Sub Read As List
	DB.Open
	DB.Table = "tbl_$endpoints$ p"
	DB.Columns = Array("p.id", "p.category_id", "c.category_name", "p.$endpoint$_code", "p.$endpoint$_name")
	DB.Join("", "tbl_categories c", Array("p.category_id = c.id"))
	DB.OrderBy = CreateMap("p.id": "DESC")
	DB.Query
	Return DB.Results
End Sub

Public Sub Update (Id As Int, Category As Int, Code As String, Name As String, Price As Double, Modified_Date As String)
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Columns = Array("category_id", "$endpoint$_code", "$endpoint$_name", "modified_date")
	DB.Parameters = Array(Category, Code, Name, Price, Modified_Date)
	DB.Condition = "id = ?"
	DB.Parameter = Id
	DB.ReturnRow = True
	DB.Save
End Sub

Public Sub Delete (Id As Int)
	DB.Open
	DB.Table = "tbl_$endpoints$"
	DB.Id = Id
	DB.Delete
End Sub