-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d65f66
commit 3dc2a06
Showing
36 changed files
with
6,576 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TOKEN_SECRET=gwEtS=KfKfR^zxJP83ULiw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
|
||
!images/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Backend API - Sophie Bluel | ||
|
||
Ce repo contient le code backend de l'architecte Sophie Bluel. | ||
|
||
## Lancement du backend | ||
|
||
Après avoir récupéré le REPO executez la commande `npm install` pour installer les dépendances du projet | ||
|
||
Une fois les dépendances installées lancez le projet avec la commande `npm start` | ||
|
||
Compte de test pour Sophie Bluel | ||
|
||
``` | ||
email: [email protected] | ||
password: S0phie | ||
``` | ||
Lien pour voir la | ||
[documentation Swagger](http://localhost:5678/api-docs/) | ||
|
||
Pour lire la documentation, utiliser Chrome ou Firefox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const cors = require('cors') | ||
require('dotenv').config(); | ||
const helmet = require('helmet'); | ||
const swaggerUi = require('swagger-ui-express') | ||
const yaml = require('yamljs') | ||
const swaggerDocs = yaml.load('swagger.yaml') | ||
const app = express() | ||
app.use(cors()) | ||
app.use(express.json()) | ||
app.use(express.urlencoded({ extended: true })) | ||
app.use(helmet({ | ||
crossOriginResourcePolicy: false, | ||
})); | ||
app.use('/images', express.static(path.join(__dirname, 'images'))) | ||
|
||
const db = require("./models"); | ||
const userRoutes = require('./routes/user.routes'); | ||
const categoriesRoutes = require('./routes/categories.routes'); | ||
const worksRoutes = require('./routes/works.routes'); | ||
db.sequelize.sync().then(()=> console.log('db is ready')); | ||
app.use('/api/users', userRoutes); | ||
app.use('/api/categories', categoriesRoutes); | ||
app.use('/api/works', worksRoutes); | ||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs)) | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
dialect: "sqlite", | ||
storage: './database.sqlite' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const db = require('./../models'); | ||
const Categories = db.categories | ||
|
||
exports.findAll = async (req, res) => { | ||
try{ | ||
const works = await Categories.findAll(); | ||
return res.status(200).json(works); | ||
}catch(err){ | ||
return res.status(500).json({ error: new Error('Something went wrong')}) | ||
} | ||
|
||
} | ||
|
||
exports.create = async (req, res) => { | ||
|
||
const category = await Categories.create({ | ||
name : req.body.name | ||
}) | ||
return res.status(201).json(category) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const db = require('./../models'); | ||
const bcrypt = require('bcrypt'); | ||
const jwt = require('jsonwebtoken'); | ||
const Users = db.users; | ||
|
||
exports.signup = async (req, res) => { | ||
if(!req.body.email || !req.body.password){ | ||
return res.status(400).send({ | ||
message: "Must have email and password" | ||
}); | ||
} | ||
try{ | ||
const hash = await bcrypt.hash(req.body.password, 10) | ||
const user = { | ||
email: req.body.email, | ||
password: hash | ||
} | ||
await Users.create(user) | ||
return res.status(201).json({message: 'User Created'}) | ||
}catch (err){ | ||
return res.status(500).send({ | ||
message: err.message | ||
}); | ||
} | ||
|
||
} | ||
|
||
exports.login = async (req, res) => { | ||
const user = await Users.findOne({where: {email: req.body.email}}); | ||
if(user === null){ | ||
return res.status(404).json({message: 'user not found'}) | ||
}else { | ||
const valid = await bcrypt.compare(req.body.password, user.password) | ||
if(!valid){ | ||
return res.status(401).json({ error: new Error('Not Authorized') }) | ||
} | ||
return res.status(200).json({ | ||
userId: user.id, | ||
token: jwt.sign( | ||
{userId : user.id}, | ||
process.env.TOKEN_SECRET, | ||
{ expiresIn: '24h' } | ||
) | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const db = require('./../models'); | ||
const Works = db.works | ||
|
||
exports.findAll = async (req, res) => { | ||
const works = await Works.findAll({include: 'category'}); | ||
return res.status(200).json(works); | ||
} | ||
|
||
exports.create = async (req, res) => { | ||
const host = req.get('host'); | ||
const title = req.body.title; | ||
const categoryId = req.body.category; | ||
const userId = req.auth.userId; | ||
const imageUrl = `${req.protocol}://${host}/images/${req.file.filename}`; | ||
try{ | ||
const work = await Works.create({ | ||
title, | ||
imageUrl, | ||
categoryId, | ||
userId | ||
}) | ||
return res.status(201).json(work) | ||
}catch (err) { | ||
return res.status(500).json({ error: new Error('Something went wrong') }) | ||
} | ||
} | ||
|
||
exports.delete = async (req, res) => { | ||
try{ | ||
await Works.destroy({where:{id: req.params.id}}) | ||
return res.status(204).json({message: 'Work Deleted Successfully'}) | ||
}catch(e){ | ||
return res.status(500).json({error: new Error('Something went wrong')}) | ||
} | ||
|
||
} |
Binary file not shown.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const jwt = require('jsonwebtoken') | ||
|
||
module.exports = (req, res, next) => { | ||
try { | ||
console.log(req.headers.authorization); | ||
const token = req.headers.authorization.split(' ')[1] | ||
const decodedToken = jwt.verify(token, process.env.TOKEN_SECRET) | ||
const userId = decodedToken.userId | ||
req.auth = { userId } | ||
if (req.body.userId && req.body.userId !== userId) { | ||
throw 'Invalid user ID' | ||
} else { | ||
next() | ||
} | ||
} catch { | ||
res.status(401).json({ | ||
error: new Error('You are not authenticated') | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = (req, res, next) => { | ||
try{ | ||
const host = req.get('host'); | ||
const title = req.body.title.trim() ?? undefined; | ||
const categoryId = parseInt(req.body.category) ?? undefined; | ||
const userId = req.auth.userId ?? undefined; | ||
const imageUrl = `${req.protocol}://${host}/images/${req.file.filename}` ?? undefined; | ||
console.log(title,categoryId,userId,imageUrl) | ||
if(title !== undefined && | ||
title.length > 0 && | ||
categoryId !== undefined && | ||
categoryId > 0 && | ||
userId !== undefined && | ||
userId > 0 && | ||
imageUrl !== undefined){ | ||
req.work = {title, categoryId, userId, imageUrl} | ||
next() | ||
}else{ | ||
return res.status(400).json({error: new Error("Bad Request")}) | ||
} | ||
}catch(e){ | ||
return res.status(500).json({error: new Error("Something wrong occured")}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const multer = require('multer') | ||
|
||
const MIME_TYPE = { | ||
'image/jpg': 'jpg', | ||
'image/jpeg': 'jpg', | ||
'image/png': 'png', | ||
'image/webp': 'webp', | ||
} | ||
|
||
const storage = multer.diskStorage({ | ||
destination: function (req, file, callback) { | ||
callback(null, './images') | ||
}, | ||
filename: (req, file, callback) => { | ||
const filename = file.originalname.split(' ').join('_') | ||
const filenameArray = filename.split('.') | ||
filenameArray.pop() | ||
const filenameWithoutExtention = filenameArray.join('.') | ||
const extension = MIME_TYPE[file.mimetype] | ||
callback(null, filenameWithoutExtention + Date.now() + '.' + extension) | ||
} | ||
}) | ||
|
||
module.exports = multer({storage}).single('image') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Categories = sequelize.define( | ||
"categories", | ||
{ | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
}, | ||
{timestamps: false} | ||
) | ||
return Categories | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const dbConfig = require("./../config/db.config.js"); | ||
const {Sequelize} = require("sequelize"); | ||
const config = require("../config/db.config"); | ||
|
||
const sequelize = new Sequelize('project6-db', 'user', 'pass', config) | ||
|
||
const db = {} | ||
|
||
db.Sequelize = Sequelize; | ||
db.sequelize = sequelize; | ||
|
||
db.users = require('./users.model.js')(sequelize, Sequelize); | ||
db.works = require('./works.model.js')(sequelize, Sequelize); | ||
db.categories = require('./categories.model.js')(sequelize, Sequelize); | ||
|
||
// Works and Categories Relationships | ||
db.categories.hasMany(db.works, {as: "works"}) | ||
db.works.belongsTo(db.categories, { | ||
foreignKey: 'categoryId', | ||
as: 'category' | ||
}); | ||
|
||
// Works and Users Relationships | ||
db.users.hasMany(db.works, {as: "works"}) | ||
db.works.belongsTo(db.users, { | ||
foreignKey: 'userId', | ||
as: 'user' | ||
}); | ||
|
||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Users = sequelize.define( | ||
"users", | ||
{ | ||
email: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
password: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
} | ||
}, | ||
{timestamps:false} | ||
) | ||
return Users | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Works = sequelize.define( | ||
"works", | ||
{ | ||
title: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
imageUrl: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
} | ||
}, | ||
{timestamps:false} | ||
) | ||
return Works | ||
} |
Oops, something went wrong.