new directory structure for api endpoints / prepare image endpoint / default product image

This commit is contained in:
Leon Meier 2025-03-02 22:11:48 +01:00
parent 5ab2df351c
commit 551f72f3e0
10 changed files with 147 additions and 14 deletions

BIN
images/default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 KiB

View File

@ -0,0 +1,94 @@
import { Request, Response } from 'express';
import path from 'node:path';
import fs from 'node:fs';
import db, { handlePrismaError } from '../../../../handlers/db.js'; // Database
import log from '../../../../handlers/log.js';
import __path from '../../../../handlers/path.js';
import { schema_get, schema_post, schema_del } from './image_schema.js';
// MARK: GET image
async function get(req: Request, res: Response) {
const { error, value } = schema_get.validate(req.query);
if (error) {
log.api?.debug('GET image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('GET image Success:', req.query, value);
await db.products
.findUnique({
where: {
id: value.id
}
})
.then((result) => {
if (result) {
const img_path = path.join(__path, 'images', `${value.id}.png`);
// Serve stored or default image
fs.existsSync(img_path) ? res.sendFile(img_path) : res.sendFile(path.join(__path, 'images', 'default.png'));
} else {
// Product does not exist
res.sendFile(path.join(__path, 'images', 'default.png'));
}
})
.catch((err) => {
handlePrismaError(err, res, 'GET image_provider');
});
}
}
// MARK: CREATE image (upload)
async function post(req: Request, res: Response) {
const { error, value } = schema_post.validate(req.query);
if (error) {
log.api?.debug('POST image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('POST image Success:', req.query, value);
// if (!req.files || Object.keys(req.files).length === 0) {
// res.status(400).send('No files were uploaded.');
// }
// const upload_file = req.files?.file;
// let upload_path;
// //uploadPath = __dirname + '/somewhere/on/your/server/' + sampleFile.name;
// const allowedMimeTypes = ['image/png', 'image/jpeg'];
// // if (!allowedMimeTypes.includes(upload_file.mimetype)) {
// // return res.status(400).send('Invalid file type. Only PNG and JPEG are allowed.');
// // }
// uploadedFile.mv(`/path/to/save/${uploadedFile.name}`, (err) => {
// if (err) {
// return res.status(500).send(err);
// }
// res.send('File uploaded!');
}
}
// MARK: DELETE image
async function del(req: Request, res: Response) {
const { error, value } = schema_del.validate(req.query);
if (error) {
log.api?.debug('DEL image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('DEL image Success:', req.query, value);
// await db.products
// .delete({
// where: {
// id: value.id
// }
// })
// .then((result) => {
// res.status(200).json({ status: 'DELETED', message: 'Successfully deleted product', id: result.id });
// })
// .catch((err) => {
// handlePrismaError(err, res, 'DEL products');
// });
}
}
export default { get, post, del };

View File

@ -0,0 +1,33 @@
import { Request, Response } from 'express';
import validator from 'joi'; // DOCS: https://joi.dev/api
// MARK: GET image
const schema_get = validator.object({
id: validator.number().positive().precision(0)
});
// MARK: CREATE image
const schema_post = validator.object({
id: validator.number().positive().precision(0)
});
// MARK: DELETE products
const schema_del = validator.object({
id: validator.number().positive().precision(0)
});
// Describe all schemas
const schema_get_desc = schema_get.describe();
const schema_post_desc = schema_post.describe();
const schema_del_desc = schema_del.describe();
// GET route
export default async function get(req: Request, res: Response) {
res.status(200).json({
GET: schema_get_desc,
POST: schema_post_desc,
DELETE: schema_del_desc
});
}
export { schema_get, schema_post, schema_del };

View File

@ -4,14 +4,17 @@ import express from 'express';
import testRoute from './test.js';
import versionRoute from './version.js';
import user_route from './user.js';
import user_schema from './user_schema.js';
import user_route from './user/user.js';
import user_schema from './user/user_schema.js';
import user_codecheck_route from './user_codecheck.js';
import user_codecheck_schema from './user_codecheck_schema.js';
import user_codecheck_route from './user/user_codecheck.js';
import user_codecheck_schema from './user/user_codecheck_schema.js';
import products_route from './products.js';
import products_schema from './products_schema.js';
import products_route from './products/products.js';
import products_schema from './products/products_schema.js';
import image_route from './image/image.js';
import image_schema from './image/image_schema.js';
// Router base is '/api/v1'
const Router = express.Router({ strict: false });
@ -36,6 +39,9 @@ Router.route('/user/codecheck/describe').get(user_codecheck_schema);
Router.route('/products').get(products_route.get).post(products_route.post).patch(products_route.patch).delete(products_route.del);
Router.route('/products/describe').get(products_schema);
Router.route('/image').get(image_route.get).post(image_route.post).delete(image_route.del);
Router.route('/image/describe').get(image_schema); // TODO: Checken, ob das probleme mit dem image_provider endpoint macht.
Router.route('/version').get(versionRoute.get);
Router.route('/test').get(testRoute.get);

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express';
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
import log from '../../../handlers/log.js';
import { parseDynamicSortBy } from '../../../helpers/prisma_helpers.js';
import db, { handlePrismaError } from '../../../../handlers/db.js'; // Database
import log from '../../../../handlers/log.js';
import { parseDynamicSortBy } from '../../../../helpers/prisma_helpers.js';
import { schema_get, schema_post, schema_patch, schema_del } from './products_schema.js';
// MARK: GET products

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express';
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
import log from '../../../handlers/log.js';
import { parseDynamicSortBy } from '../../../helpers/prisma_helpers.js';
import db, { handlePrismaError } from '../../../../handlers/db.js'; // Database
import log from '../../../../handlers/log.js';
import { parseDynamicSortBy } from '../../../../helpers/prisma_helpers.js';
import { schema_get, schema_post, schema_patch, schema_del } from './user_schema.js';
// MARK: GET user

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import db, { handlePrismaError } from '../../../handlers/db.js'; // Database
import log from '../../../handlers/log.js';
import db, { handlePrismaError } from '../../../../handlers/db.js'; // Database
import log from '../../../../handlers/log.js';
import { schema_get } from './user_codecheck_schema.js';
// MARK: GET user codecheck