Skip to content

Commit

Permalink
Merge pull request #9 from philippebeck/dev
Browse files Browse the repository at this point in the history
Release 0.2.0-alpha
  • Loading branch information
philippebeck authored Dec 16, 2023
2 parents c4abd33 + 797288a commit 643feaf
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 176 deletions.
4 changes: 2 additions & 2 deletions controller/GalleryCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ exports.deleteGallery = (req, res) => {
const id = parseInt(req.params.id);

Image
.findAll({ where: { gallery_id: id }})
.findAll({ where: { galleryId: id }})
.then(images => {
for (let image of images) {
fs.unlink(GALLERIES_THUMB + image.name, () => {
fs.unlink(GALLERIES_IMG + image.name, () => {});
});
}
Image
.destroy({ where: { gallery_id: id }})
.destroy({ where: { galleryId: id }})
.then(() =>

Gallery
Expand Down
43 changes: 13 additions & 30 deletions controller/ImageCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,36 @@ exports.setImage = (image, newFilename) => {
*
* @param {string} name - The name of the image.
* @param {string} description - The description of the image.
* @param {number} gallery_id - The ID of the gallery the image belongs to.
* @param {number} galleryId - The ID of the gallery the image belongs to.
* @return {object} - An object containing the name, description & gallery ID of the image.
*/
exports.getImage = (name, description, gallery_id) => {
exports.getImage = (name, description, galleryId) => {

return {
name: name,
description: description,
gallery_id: gallery_id
galleryId: galleryId
}
}

//! ******************** PUBLIC ********************

/**
* ? LIST GALLERY IMAGES
* ? LIST IMAGES
* * Retrieves a list of gallery images based on the provided gallery ID.
*
* @param {Object} req - The request object.
* @param {Object} req.params - The parameters object containing the gallery ID.
* @param {number} req.params.id - The ID of the gallery to retrieve images for.
* @param {Object} res - The response object.
* @return {Promise} A promise that resolves to a response containing the list of gallery images.
* @throws {Error} If the images are not found in the database.
*/
exports.listGalleryImages = (req, res) => {
Image
.findAll({ where: { gallery_id: req.params.id }})
.then((images) => { res.status(200).json(images) })
.catch(() => res.status(404).json({ message: process.env.IMAGES_NOT_FOUND }));
};

//! ******************** PRIVATE ********************

/**
* ? LIST IMAGES
* * Retrieves a list of images & modifies the gallery ID of each image by appending the gallery name.
*
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @return {Promise} A promise that resolves to a response containing the list of images.
* @throws {Error} If the images are not found in the database.
*/
exports.listImages = (req, res) => {
Image.belongsTo(Gallery, { foreignKey: "gallery_id" });
Image.belongsTo(Gallery, { foreignKey: "galleryId" });

Image
.findAll({
attributes: ["id", "name", "description", "gallery_id"],
where: { galleryId: req.params.id },
attributes: ["id", "name", "description", "galleryId"],
include: {
model: Gallery,
attributes: ["name"],
Expand All @@ -113,6 +94,8 @@ exports.listImages = (req, res) => {
.catch(() => res.status(404).json({ message: process.env.IMAGES_NOT_FOUND }));
};

//! ******************** PRIVATE ********************

/**
* ? CREATE IMAGE
* * Creates an image based on the request data.
Expand All @@ -130,18 +113,18 @@ exports.createImage = (req, res, next) => {
this.checkImageData(fields.description, res);

Gallery
.findOne({ where: { id: fields.gallery_id }})
.findOne({ where: { id: fields.galleryId }})
.then((gallery) => {

Image
.findAll({ where: { gallery_id: fields.gallery_id }})
.findAll({ where: { galleryId: fields.galleryId }})
.then((images) => {
let index = images.length + 1;
if (index < 10) { index = "0" + index }

let name = nem.getName(gallery.name) + "-" + index + "." + process.env.IMG_EXT;
this.setImage(name, files.image.newFilename);
let image = this.getImage(name, fields.description, fields.gallery_id);
let image = this.getImage(name, fields.description, fields.galleryId);

Image
.create(image)
Expand Down Expand Up @@ -176,7 +159,7 @@ exports.updateImage = (req, res, next) => {
let name = fields.name;

if (files.image) this.setImage(name, files.image.newFilename);
let image = this.getImage(name, fields.description, fields.gallery_id);
let image = this.getImage(name, fields.description, fields.galleryId);

Image
.update(image, { where: { id: parseInt(req.params.id) }})
Expand Down
17 changes: 6 additions & 11 deletions controller/OrderCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ exports.setMailer = (fields, res) => {
* * Sets the message content for an order.
*
* @param {number} total - The total amount of the order.
* @param {string} payment_id - The ID of the payment.
* @param {string} paymentId - The ID of the payment.
* @param {Array} products - An array of products in the order.
* @return {Object} message - The message object containing the subject, text, and html properties.
*/
exports.setMessage = (total, payment_id, products) => {
exports.setMessage = (total, paymentId, products) => {
let message = {};
message.subject = process.env.ORDER_SUBJECT;

Expand All @@ -54,7 +54,7 @@ exports.setMessage = (total, payment_id, products) => {
${process.env.ORDER_TOTAL}
<b>${total} ${process.env.CURRENCY_SYMBOL}</b>,
${process.env.ORDER_PAYMENT}
<b>#${payment_id}</b> !
<b>#${paymentId}</b> !
</p>
<p>${process.env.ORDER_LIST}</p>`;

Expand Down Expand Up @@ -104,7 +104,7 @@ exports.listOrders = (req, res) => {
*/
exports.listUserOrders = (req, res) => {
Order
.findAll({ where: { user_id: parseInt(req.params.id) }})
.findAll({ where: { userId: parseInt(req.params.id) }})
.then((orders) => res.status(200).json(orders))
.catch(() => res.status(404).json({ message: process.env.ORDERS_NOT_FOUND }));
};
Expand All @@ -123,17 +123,14 @@ exports.createOrder = (req, res, next) => {
form.parse(req, (err, fields) => {
if (err) { next(err); return }

fields.products = JSON.parse(fields.products);
let message = this.setMessage(fields.total, fields.payment_id, fields.products);
let message = this.setMessage(fields.total, fields.paymentId, fields.products);

Order
.create(fields)
.then(() => {

User
.findByPk(fields.user_id)
.findByPk(fields.userId)
.then((user) => {

message.email = user.email;
this.setMailer(message, res);
})
Expand All @@ -156,9 +153,7 @@ exports.createOrder = (req, res, next) => {
*/
exports.updateOrder = (req, res, next) => {
form.parse(req, (err, fields) => {

if (err) { next(err); return }
if (fields.products) { fields.products = JSON.parse(fields.products) }

Order
.update(fields, { where: { id: parseInt(req.params.id) }})
Expand Down
12 changes: 2 additions & 10 deletions controller/ProductCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ exports.setImage = (name, newFilename) => {
exports.listProducts = (req, res) => {
Product
.findAll()
.then((products) => {
for (const product of products) {
product.options = JSON.parse(product.options).join(",");
}
res.status(200).json(products);
})
.then((products) => { res.status(200).json(products) })
.catch(() => res.status(404).json({ message: process.env.PRODUCTS_NOT_FOUND }));
};

Expand All @@ -168,10 +163,7 @@ exports.listProducts = (req, res) => {
exports.readProduct = (req, res) => {
Product
.findByPk(parseInt(req.params.id))
.then((product) => {
product.options = JSON.parse(product.options).join(",");
res.status(200).json(product);
})
.then((product) => { res.status(200).json(product) })
.catch(() => res.status(404).json({ message: process.env.PRODUCT_NOT_FOUND }));
}

Expand Down
2 changes: 1 addition & 1 deletion data/DBPublic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ INSERT INTO Links (name, url, cat) VALUES
('How to become a 10x dev','dev.to/idboussadel/how-to-become-a-10x-dev-ake','JS'),
('Sequelize with Vue & Node', 'www.bezkoder.com/vue-js-node-js-express-mysql-crud-example', 'JS');

INSERT INTO Images (name, description, gallery_id) VALUES
INSERT INTO Images (name, description, galleryId) VALUES
('964-kitab-suwar-al-kawakib-01.webp','Ursa Minor',1),
('964-kitab-suwar-al-kawakib-02.webp','Ursa Major',1),
('964-kitab-suwar-al-kawakib-03.webp','Draco',1),
Expand Down
78 changes: 32 additions & 46 deletions data/DBTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ CREATE DATABASE sen CHARACTER SET 'utf8';

USE sen;

CREATE TABLE Articles
(
CREATE TABLE Articles(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
text TEXT NOT NULL UNIQUE,
Expand All @@ -13,74 +12,61 @@ CREATE TABLE Articles
likes TEXT NOT NULL,
cat VARCHAR(20) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL
)
updatedAt DATETIME NOT NULL)
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Galleries
(
CREATE TABLE Galleries(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
author VARCHAR(100) NOT NULL,
cover VARCHAR(100) NOT NULL UNIQUE
)
cover VARCHAR(100) NOT NULL UNIQUE)
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Images
(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
CREATE TABLE Images(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
description VARCHAR(200) NOT NULL,
gallery_id SMALLINT UNSIGNED NOT NULL,
CONSTRAINT fk_gallery_id FOREIGN KEY (gallery_id) REFERENCES Galleries(id)
)
galleryId SMALLINT UNSIGNED NOT NULL,
CONSTRAINT fk_galleryId FOREIGN KEY (galleryId) REFERENCES Galleries(id))
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Links
(
CREATE TABLE Links(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
url VARCHAR(200) NOT NULL UNIQUE,
cat VARCHAR(20) NOT NULL
)
cat VARCHAR(20) NOT NULL)
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Products
(
CREATE TABLE Products(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
description TEXT NOT NULL UNIQUE,
image VARCHAR(100) NOT NULL UNIQUE,
alt VARCHAR(100) NOT NULL UNIQUE,
price DECIMAL NOT NULL,
options JSON NOT NULL,
cat VARCHAR(20) NOT NULL
)
options TEXT NOT NULL,
cat VARCHAR(20) NOT NULL)
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Users
(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
image VARCHAR(100) NOT NULL UNIQUE,
pass VARCHAR(100) NOT NULL,
role VARCHAR(10) NOT NULL,
created DATETIME NOT NULL,
updated DATETIME NOT NULL
)
CREATE TABLE Users(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
image VARCHAR(100) NOT NULL UNIQUE,
pass VARCHAR(100) NOT NULL,
role VARCHAR(10) NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL)
ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE Orders
(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
products JSON NOT NULL,
total DECIMAL NOT NULL,
payment_id VARCHAR(50) NOT NULL UNIQUE,
status VARCHAR(10) NOT NULL,
user_id SMALLINT UNSIGNED NOT NULL,
created DATETIME NOT NULL,
updated DATETIME NOT NULL,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES Users(id)
)
CREATE TABLE Orders(
id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
products TEXT NOT NULL,
total DECIMAL NOT NULL,
paymentId VARCHAR(50) NOT NULL UNIQUE,
status VARCHAR(10) NOT NULL,
userId SMALLINT UNSIGNED NOT NULL,
createdAt DATETIME NOT NULL,
updatedAt DATETIME NOT NULL,
CONSTRAINT fk_userId FOREIGN KEY (userId) REFERENCES Users(id))
ENGINE=INNODB DEFAULT CHARSET=utf8;
7 changes: 0 additions & 7 deletions model/ArticleModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,35 @@ const { Sequelize, DataTypes } = require("sequelize");
*/
module.exports = (Sequelize, DataTypes) => {
const ArticleModel = Sequelize.define("Articles", {

id: {
type: DataTypes.SMALLINT.UNSIGNED,
primaryKey: true,
autoIncrement: true
},

name: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true
},

text: {
type: DataTypes.TEXT,
allowNull: false,
unique: true
},

image: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true
},

alt: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true
},

likes: {
type: DataTypes.TEXT,
defaultValue: "[]"
},

cat: {
type: DataTypes.STRING(20),
allowNull: false
Expand Down
Loading

0 comments on commit 643feaf

Please sign in to comment.