Doing It With Chat GPT

Apex does offer integration with chat GPT to assist with development. Full details can be found on the Chat GPT Code Generation chapter of the developer documentation, but simplistically, it will generate a new database schema for you then using the built-in code generation utilities within Apex will generate all necessary models, views, and controllers.

To start, you can either install Apex from scratch or delete the disaster package with the command:

apex package delete disaster

Initialize OpenAI Access

You will need an OpenAI API Key, and once obtained run the apex gpt init command:

apex gpt init <API_KEY>

This will set the OpenAI API key into Apex for future use.

Generate Code

Now simply run the gpt package CLI command:

apex gpt package disaster

This will create the disaster package again, and prompt you to describe the package in plain text. Enter the following:

[data-line=-1]
disaster relief coordination system that allows the administrator to manage  volunteers, locations, projects and donations.  Every project should be assigned to a location.  Each project can have multiple volunteers assigned to it.

The projects table should contain a 'status' column that allows the options, "in_progress", "pending", "halted", "completed".

The volunteer table should only contain columns -- is_active boolean, uuid varchar(30), and a skills textbox.

Public site should have three views added.  One that lists all projects in a foreach loop, another that displays all details on a specific project, and one that allows a user to submit a donation to a project.

This will generate a SQL database schema and save it within the chat_gpt.sql file. Open the file and modify the database schema as you desire. If preferred, below is a schema you can use:

CREATE TABLE disaster_locations (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  state VARCHAR(255) NOT NULL,
  country VARCHAR(255) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP
);

CREATE TABLE disaster_projects (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  location_id INT NOT NULL,
  title VARCHAR(255) NOT NULL,
  description TEXT NOT NULL,
  status ENUM('in_progress', 'pending', 'halted', 'completed') NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP,
  FOREIGN KEY (location_id) REFERENCES disaster_locations(id) ON DELETE CASCADE
);

CREATE TABLE disaster_volunteers (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  is_active BOOLEAN NOT NULL,
  uuid VARCHAR(30) NOT NULL,
  skills TEXT NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP,
  FOREIGN KEY (uuid) REFERENCES armor_users(uuid) ON DELETE CASCADE
);

CREATE TABLE disaster_donations (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  project_id INT NOT NULL,
  amount DECIMAL(10, 2) NOT NULL,
  donor_name VARCHAR(255) NOT NULL,
  donor_email VARCHAR(255) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP,
  FOREIGN KEY (project_id) REFERENCES disaster_projects(id) ON DELETE CASCADE
);

Once you're happy with the schema, press enter within the terminal. This will ask you a couple questions, then generate everything including database migrations, models, views, controllers, admin panel menus, et al. Once complete, login to the admin panel and you will see the new Disaster menu.

Generate REST API

Now within terminal, run the command:

apex gpt rest-api disaster

When prompted, enter:

```text [data-line=-1] Develop CRUD endpoints for projects. The create, update and delete functionality must require user authentication, but the retrieve functionality is open to the public.

~~~

Once complete, you will notice the necessary PHP classes for API endpoints within the /src/Disaster/Api/ directory.