Skip to content

Commit

Permalink
feat: assertions for required parameters (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Jun 22, 2024
1 parent 9753fec commit 903c4f6
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ local State = {
IMPOUNDED = 2
}

---@alias IdType 'citizenid'|'license'|'plate'|'vehicleId'

---@param idType IdType
---@return ErrorResult?
local function validateIdType(idType)
if idType ~= 'citizenid' and idType ~= 'license' and idType ~= 'plate' and idType ~= 'vehicleId' then
return {
code = 'bad_request',
message = 'idType:' .. json.encode(idType) .. ' is not a valid idType'
}
end
end

---Returns true if the given plate exists
---@param plate string
---@return boolean
Expand Down Expand Up @@ -122,6 +109,7 @@ exports('GetPlayerVehicles', getPlayerVehicles)
---@param filters? PlayerVehiclesFilters
---@return PlayerVehicle?
local function getPlayerVehicle(vehicleId, filters)
assert(vehicleId ~= nil, "required field vehicleId was nil")
if not filters then filters = {} end
---@diagnostic disable-next-line: inject-field
filters.vehicleId = vehicleId
Expand All @@ -140,12 +128,7 @@ exports('GetPlayerVehicle', getPlayerVehicle)
---@param request CreatePlayerVehicleRequest
---@return integer? vehicleId, ErrorResult? errorResult
local function createPlayerVehicle(request)
if not request.model then
return nil, {
code = 'bad_request',
message = 'missing required field model'
}
end
assert(request.model ~= nil, 'missing required field: model')

local props = request.props or {}
if not props.plate then
Expand Down Expand Up @@ -175,6 +158,7 @@ exports('CreatePlayerVehicle', createPlayerVehicle)
---@param citizenid? string
---@return boolean success, ErrorResult? errorResult
local function setPlayerVehicleOwner(vehicleId, citizenid)
assert(vehicleId ~= nil, "required field vehicleId was nil")
MySQL.update.await('UPDATE player_vehicles SET citizenid = ?, license = (SELECT license FROM players WHERE citizenid = @citizenid) WHERE id = @id', {
citizenid = citizenid,
id = vehicleId
Expand All @@ -184,12 +168,11 @@ end

exports('SetPlayerVehicleOwner', setPlayerVehicleOwner)

---@param idType IdType
---@param idType 'citizenid'|'license'|'plate'|'vehicleId'
---@param idValue string | number
---@return boolean success, ErrorResult? errorResult
local function deletePlayerVehicles(idType, idValue)
local err = validateIdType(idType)
if err then return false, err end
assert(idType == 'citizenid' or idType == 'license' or idType == 'plate' or idType == 'vehicleId', json.encode(idType) .. ' is not a valid idType')

local column = idType == 'vehicleId' and 'id' or idType
MySQL.query.await('DELETE FROM player_vehicles WHERE ' .. column .. ' = ?', {
Expand Down

0 comments on commit 903c4f6

Please sign in to comment.