Private Sub PutProductById (Id As Int)
' #Desc = Update Product by id
' #Body = {<br> "cat_id": category_id,<br> "code": "product_code",<br> "name": "product_name",<br> "price": product_price,<br> "stock": 0<br>}
' #Elements = [":id"]
Dim data As Map = WebApiUtils.RequestData(Request)
If Not(data.IsInitialized) Then
HRM.ResponseCode = 400
HRM.ResponseError = "Invalid json object"
ReturnApiResponse
Return
End If
' Deprecated: Make it compatible with Web API Client v1 (will be removed)
If data.ContainsKey("cat_id") Then
data.Put("category_id", data.Get("cat_id"))
data.Remove("cat_id")
End If
If data.ContainsKey("code") Then
data.Put("product_code", data.Get("code"))
data.Remove("code")
End If
If data.ContainsKey("name") Then
data.Put("product_name", data.Get("name"))
data.Remove("name")
End If
If data.ContainsKey("price") Then
data.Put("product_price", data.Get("price"))
data.Remove("price")
End If
If data.ContainsKey("stock") Then
data.Put("opening_stock", data.Get("stock"))
data.Remove("stock")
End If
' Check conflict product code
DB.Table = "tbl_products"
DB.Where = Array("product_code = ?", "id <> ?")
DB.Parameters = Array As String(data.Get("product_code"), Id)
DB.Query
If DB.Found Then
HRM.ResponseCode = 409
HRM.ResponseError = "Product Code already exist"
ReturnApiResponse
DB.Close
Return
End If
DB.Find(Id)
If DB.Found = False Then
HRM.ResponseCode = 404
HRM.ResponseError = "Product not found"
ReturnApiResponse
DB.Close
Return
End If
DB.Reset
DB.Columns = Array("category_id", _
"product_code", _
"product_name", _
"product_price", _
"opening_stock", _
"modified_date")
DB.Parameters = Array(data.Get("category_id"), _
data.Get("product_code"), _
data.Get("product_name"), _
data.GetDefault("product_price", 0), _
data.GetDefault("opening_stock", 0), _
data.GetDefault("modified_date", WebApiUtils.CurrentDateTime))
DB.Id = Id
DB.Save
HRM.ResponseCode = 200
HRM.ResponseMessage = "Product updated successfully"
HRM.ResponseObject = DB.First
ReturnApiResponse
DB.Close
End Sub