From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/js/epiTinderDB/epiTinderDB.js | 204 +++++++++++++++++++++++++++++++++++ graphs/js/epiTinderDB/provided.sql | 17 +++ 2 files changed, 221 insertions(+) create mode 100644 graphs/js/epiTinderDB/epiTinderDB.js create mode 100644 graphs/js/epiTinderDB/provided.sql (limited to 'graphs/js/epiTinderDB') diff --git a/graphs/js/epiTinderDB/epiTinderDB.js b/graphs/js/epiTinderDB/epiTinderDB.js new file mode 100644 index 0000000..d9f35a8 --- /dev/null +++ b/graphs/js/epiTinderDB/epiTinderDB.js @@ -0,0 +1,204 @@ +const { Sequelize, Op, DataTypes } = require("sequelize"); + +let seq = null; +let userModel = null; + +async function connectToDB() { + if (!seq) { + seq = new Sequelize( + "postgres", + process.env.USERNAME, + process.env.PASSWORD, + { + port: 5432, + dialect: "postgres", + }, + ); + } + + if (!userModel) { + userModel = seq.define( + "epitinder_users", + { + name: { + type: DataTypes.STRING, + }, + age: { + type: DataTypes.INTEGER, + }, + description: { + type: DataTypes.STRING, + }, + }, + { + // Other model options go here + timestamps: false, + }, + ); + } + + return seq; +} + +async function getAllUsers() { + return userModel.findAll({ + attributes: ["id", "name", "age", "description"], + raw: true, + }); +} + +async function getUser(id) { + return userModel + .findOne({ + attributes: ["id", "name", "age", "description"], + where: { id: id }, + raw: true, + }) + .catch(() => { + return null; + }); +} + +async function addUser(newUser) { + if ( + newUser["id"] || + !newUser["name"] || + !newUser["age"] || + !newUser["description"] + ) { + return null; + } + + return userModel + .create({ + name: newUser.name, + age: newUser.age, + description: newUser.description, + }) + .then((usr) => usr.dataValues) + .catch(() => null); +} +async function updateUser(user) { + if ( + !user["id"] || + !user["name"] || + !user["age"] || + !user["description"] || + Object.keys(user).length > 4 + ) { + return null; + } + + const res = await userModel + .findOne({ + where: { id: user.id }, + raw: true, + }) + .catch(() => null); + + if (!res) { + return null; + } + + await userModel + .update( + { + name: user.name, + age: user.age, + description: user.description, + }, + { + where: { id: user.id }, + raw: true, + }, + ) + .catch(() => null); + return user; +} + +async function deleteUser(id) { + let res = await userModel + .findOne({ + where: { id: id }, + raw: true, + }) + .catch(() => null); + + if (!res) { + return null; + } + + res = { + id: res.id, + name: res.name, + age: res.age, + description: res.description, + }; + await userModel.destroy({ where: { id: id } }); + return res; +} + +async function getAllUsersName() { + return userModel + .findAll({ + attributes: ["name"], + raw: true, + }) + .catch(() => { + return null; + }); +} + +async function getAllYoungAdults() { + return userModel + .findAll({ + where: { + age: { + [Op.between]: [18, 29], + }, + }, + raw: true, + }) + .catch(() => { + return null; + }); +} + +module.exports = { + getAllUsers, + getAllUsersName, + getAllYoungAdults, + getUser, + addUser, + deleteUser, + updateUser, + connectToDB, +}; + +async function main() { + await connectToDB(); + //console.log(await getAllUsers()); + //console.log(await getAllUsersName()); + //console.log(await getAllYoungAdults()); + //console.log(await getUser(5)); + /*console.log( + await addUser({ + name: "Martial Simon", + age: 19, + description: "Sex is like pointers, I like it raw", + }), + );*/ + console.log( + await updateUser({ + id: 56, + name: "Mickael Razzouk", + age: 21, + sex_appeal: 100, + description: "Le goat", + }), + ); + //console.log(await getUser(5)); + //console.log(await deleteUser(23)); +} + +main(); diff --git a/graphs/js/epiTinderDB/provided.sql b/graphs/js/epiTinderDB/provided.sql new file mode 100644 index 0000000..81d65da --- /dev/null +++ b/graphs/js/epiTinderDB/provided.sql @@ -0,0 +1,17 @@ +CREATE TABLE epitinder_users (id SERIAL PRIMARY KEY, name VARCHAR(255), age INT, description VARCHAR(255)); + +INSERT INTO epitinder_users (name, age, description) VALUES ('Puff',2, 'I like everyone who does javascript (I m the mascot of the workshop)'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Bubble',1, 'I m the real javascript mascot but don t tell puff he ll cry again'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Maxime Buisson',40, 'I already have my other half but I want to make friends.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Dorian Penso',25, 'Gym enthusiast by day, code lover by night, seeking a partner to write code and lift weights together.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Pierre-Alexis Valbrun',32, 'I m a full-stack developer, I can handle both your front-end and back-end.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Paul Genillon',21, 'It is not compiling since it is a wooclap.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Botumrath Morchoisne',24, 'What am I doing here ??'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Pierre Santamaria',22, 'I will love you like I love JavaScript, I won t get anything you tell me but it will be passionate.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Baptiste Schaudel',9, 'I am operational to manage my team, I will let you discover if I am too with love.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Maxim Payeur',8, 'What am I doing here ??'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Vinh-Toan Phan',17, 'I am an excelent cooker, do you want to be my cookware.'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Angelina Kuntz',44, 'You can call me Ada'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Ewan Lemonnier',19, 'Barely legal'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Anaïs Druelle',21, 'What is the only part of a vegetable you can t eat?'); +INSERT INTO epitinder_users (name, age, description) VALUES ('Mathéo Romé',22, 'I can’t sing but I will charm you anyway'); \ No newline at end of file -- cgit v1.2.3