Examples
CRUD App Example
This example shows the standard Netpress CRUD pattern:
Route -> Controller -> Service -> Model -> Response
Example Endpoints
GET /api/v1/postsPOST /api/v1/postsGET /api/v1/posts/:idPUT /api/v1/posts/:idDELETE /api/v1/posts/:id
Service
// app/Services/PostService.js
import Post from '../Models/Post.js';
export default class PostService {
static all() {
return Post.latest().get();
}
static create(payload) {
return Post.create(payload);
}
static show(id) {
return Post.findOrFail(id);
}
static update(id, payload) {
return Post.findOrFail(id).then((post) => post.update(payload));
}
static async destroy(id) {
const post = await Post.findOrFail(id);
await post.delete();
}
}
Controller
// app/Http/Controllers/PostController.js
import { BaseController } from '@admicaa/netpress';
import PostService from '../../Services/PostService.js';
class PostController extends BaseController {
async index(_req, res) {
return res.success(await PostService.all());
}
async store(req, res) {
return res.success(await PostService.create(req.body));
}
async show(req, res) {
return res.success(await PostService.show(req.params.id));
}
async update(req, res) {
return res.success(await PostService.update(req.params.id, req.body));
}
async destroy(req, res) {
await PostService.destroy(req.params.id);
return res.success(null);
}
}
export default new PostController();
Why This Pattern Works
- routes stay small
- controllers stay thin
- services hold the app logic
- responses stay consistent
For a longer end-to-end tutorial, see Quickstart: Blog.