USER
local ESX = exports.es_extended:getSharedObject()
ESX.RegisterServerCallback('mGang:GetAllJobs', function(source, cb)
MySQL.Async.fetchAll('SELECT name FROM jobs', {}, function(jobs)
local jobList = {}
for _, job in ipairs(jobs) do
jobList[job.name] = true
end
cb(jobList)
end)
end)
ESX.RegisterServerCallback('mGang:GetGangOutfits', function(source, cb, jobName)
MySQL.Async.fetchAll('SELECT * FROM gang_outfits WHERE gang = @gang', {
['@gang'] = jobName
}, function(outfits)
cb(outfits)
end)
end)
ESX.RegisterServerCallback('mGang:SaveOutfit', function(source, cb, jobName, clothes, outfitName)
MySQL.Async.execute('INSERT INTO gang_outfits (gang, outfitName, clothes) VALUES (@gang, @name, @clothes)', {
['@gang'] = jobName,
['@name'] = outfitName,
['@clothes'] = json.encode(clothes)
}, function(rowsChanged)
cb(rowsChanged > 0)
end)
end)
ESX.RegisterServerCallback('mGang:UpdateOutfit', function(source, cb, jobName, outfitId, outfitName)
MySQL.Async.execute('UPDATE gang_outfits SET outfitName = @name WHERE id = @id AND gang = @gang', {
['@id'] = outfitId,
['@name'] = outfitName,
['@gang'] = jobName
}, function(rowsChanged)
cb(rowsChanged > 0)
end)
end)
ESX.RegisterServerCallback('mGang:DeleteOutfit', function(source, cb, jobName, outfitId)
MySQL.Async.execute('DELETE FROM gang_outfits WHERE id = @id AND gang = @gang', {
['@id'] = outfitId,
['@gang'] = jobName
}, function(rowsChanged)
cb(rowsChanged > 0)
end)
end)
napravi sqlASSISTANT
To create the SQL tables you'll need for the provided Lua code, here are the SQL statements that define the structure for the `jobs` and `gang_outfits` tables. This will ensure everything works as expected with the ESX framework in your environment.
### SQL Table Definitions
#### 1. Create the `jobs` table
This table should hold the job names for gangs.
```sql
CREATE TABLE IF NOT EXISTS jobs (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
```
#### 2. Create the `gang_outfits` table
This table stores the outfits associated with each gang. It contains fields for the gang name, outfit name, and the clothing data.
```sql
CREATE TABLE IF NOT EXISTS gang_outfits (
id INT AUTO_INCREMENT PRIMARY KEY,
gang VARCHAR(255) NOT NULL,
outfitName VARCHAR(255) NOT NULL,
clothes TEXT NOT NULL,
FOREIGN KEY (gang) REFERENCES jobs(name) ON DELETE CASCADE
);
```
### Notes
- In the `gang_outfits` table, you may want to consider how the `clothes` field is structured. It’s currently a JSON string (as shown in your Lua code), so ensure that any application using it can handle JSON data properly.
- The `FOREIGN KEY` constraint in the `gang_outfits` table ensures that an outfit can be linked to a valid job in the `jobs` table. If a job is deleted, it will cascade and delete associated outfits.
- Always make sure to back up your database before making changes to the schema.
You can execute these SQL statements in your MySQL database management tool (like phpMyAdmin, MySQL Workbench, etc.) to set up the necessary tables for your server script.