Create a Model (POST /api/models)
Relevant source files
The following files were used as context for generating this wiki page:The POST /api/models endpoint is a core administrative feature for adding new model profiles to the application's database. This functionality is restricted to users with an 'admin' role. When a new model is created, it is persisted in the MongoDB collection, and an initial history log entry is automatically generated to track its creation.
The endpoint is defined within the main server.js file and relies on a Mongoose schema to structure and validate the model data. The front-end interface provides a simple prompt-based method for admins to input and submit the new model's information.
Sources: server.js:77-85, public/index.html:152-163
API Endpoint Specification
| Property | Description |
|---|---|
| Path | /api/models |
| Method | POST |
| Description | Creates a new model record in the database. |
| Authorization | admin role required. |
Sources: server.js:42-45, 77
Request and Response Flow
The process of creating a new model begins with a request from an authenticated admin user and flows through authorization and data persistence layers before a response is returned.
This sequence diagram illustrates the complete flow for a successful model creation request.
Sources: server.js:77-85, public/index.html:152-163
Authorization
Access to this endpoint is controlled by the isAdmin middleware. This function checks the user's session for a role property equal to 'admin'. If the user is not an admin, the server responds with a 403 Forbidden status and an error message. The admin role is assigned during login if the user's email ends with @admin.com.
Sources: server.js:28-32, 42-45
Request Handling Logic
The core logic for the endpoint is defined in server.js.
// File: server.js:77-85
app.post("/api/models", isAdmin, async (req, res) => {
try {
const model = new Model(req.body);
model.history.push({ timestamp: new Date(), action: 'create', user: req.session.user.email, changes: 'Initial creation' });
await model.save();
res.json(model);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
The server performs the following steps:
- Instantiates a Model: A new
Modelobject is created from the request body. - Records History: A history entry is pushed into the model's
historyarray, logging the creation event, timestamp, and the admin user's email. - Saves to Database: The
model.save()method is called to persist the new document in MongoDB. - Sends Response: The newly created model object, including the database-generated
_idand timestamps, is returned as a JSON response.
Sources: server.js:77-85
Data Model Schema
The data for each model is structured according to a Mongoose schema defined in APP/models/model.model.js. This schema dictates the fields, types, and validation rules for the data stored in the models collection.
The following diagram shows the entity relationship for the Model schema.
Sources: APP/models/model.model.js:2-15
Schema Fields
| Field | Type | Required | Description |
|---|---|---|---|
id | String | Yes | A unique identifier for the model. |
name | String | Yes | The name of the model. |
age | Number | No | The age of the model. |
location | String | No | The location of the model. |
profile | String | Yes | A detailed profile or biography of the model. |
pricing | String | Yes | A string containing pricing information. |
dosAndDonts | String | Yes | A string detailing activities the model will and will not do. |
extraTabs | Array | No | An array for additional information tabs. Defaults to []. |
history | Array | No | An array to log changes to the model record. Defaults to []. |
Sources: APP/models/model.model.js:2-15, seed.js:4-10
Request Body Example
The POST request must include a JSON body that conforms to the model schema.
{
"name": "ROXIE SINNER",
"profile": "Model Name: Roxie Sinner\nImportant Info: ...",
"pricing": "PRICE LIST - LAST UPDATED: 08/12/2024...",
"dosAndDonts": "DOS AND DON'TS - LAST UPDATED: 01/07/2024...",
"id": "roxie_sinner_001"
}
Note: The example above is simplified. The actual data for fields like profile is extensive.
Sources: seed.js:4-10, public/index.html:155-162
Responses
- Success (200 OK): On successful creation, the server returns the complete model object as saved in the database, including the
_id,createdAt,updatedAt, andhistoryfields.
Sources: server.js:82 - Error (403 Forbidden): Returned by the
isAdminmiddleware if the user is not an administrator.
Sources: server.js:43-44 - Error (500 Internal Server Error): Returned if there is a database error during the save operation (e.g., validation failure, connection issue). The response body will contain an
errorkey with the error message.
Sources: server.js:84