const { comperePassword, hashPassword} = require('../helpers/bcrypt');
const {Article, Category, User, History} = require('../models')
const {createToken} = require('../helpers/jwt')
const {OAuth2Client} = require('google-auth-library');
const { STRING } = require('sequelize');
const client = new OAuth2Client();
class Controller {
static async createHistory(req, res, next){
try {
const histories = await History.findAll({
order: [['id', 'DESC']]
});
res.json(histories);
} catch (error) {
next()
}
}
static async getArticle(req, res, next) {
try {
const article = await Article.findAll();
res.status(200).json(article);
} catch (err) {
next(err)
}
}
static async getUser(req, res, next){
try {
const user = await User.findAll();
res.status(200).json(user);
} catch (err) {
next(err)
}
}
static async getCategory(req, res, next){
try {
const category = await Category.findAll();
res.status(200).json(category);
} catch (err) {
next(err)
}
}
static async postCategory(req, res, next){
try {
const data = req.body
data.authorId = req.user.id
const createCategory = await Category.create(data)
if(createCategory){
await History.create({
title: 'POST',
description: `New category with id ${createCategory.id} created`,
updatedBy: `${req.user.username}`
})
}
res.status(201).json(createCategory)
} catch (err) {
next(err)
}
}
static async loginGoogle(req, res){
try {
// console.log(req.headers);
const {google_token} = req.headers
const ticket = await client.verifyIdToken({
idToken: google_token,
audience: '731493170239-2fc94ip6i8tjag20vec2fas5mq3iu4n4.apps.googleusercontent.com', // Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = ticket.getPayload();
// console.log(payload);
const {email, name } = payload
const [user, created] = await User.findOrCreate({
where: { email : email },
defaults: {
username: name,
email:email,
password: hashPassword(String(Math.random())),
role: 'Staff'
},
hooks: false
});
const access_token = createToken({id: user.id})
res.status(200).json(access_token)
} catch (error) {
console.log(error);
}
}
static async updatePatch(req, res, next){
try {
const id = +req.params.id
const article = await Article.findByPk(id)
if(!article){
res.status(400).json({msg: `Article ${id} not found`})
}
const {status} = req.body
await Article.update({status}, {where: {id}})
await History.create({
title: 'PATCH',
description: `Article with status id ${id} has been updated from ${article.status} to ${status}`,
updatedBy: `${req.user.username}`
})
res.status(201).json({msg: `Article ${id} updated from ${article.status} to ${status}`})
} catch (error) {
next(error)
}
}
static async updateData(req, res, next){
try {
const id = +req.params.id
const article = await Article.findByPk(id)
if(!article){
res.status(400).json({msg: `Article ${id} not found`})
}
const [isUpdated] = await Article.update(req.body, {where: {id}})
await History.create({
title: 'PUT',
description: `Article id ${id} has updated`,
updatedBy: `${req.user.username}`
})
res.status(201).json({msg: `Article ${id} updated`})
} catch (error) {
next(error)
}
}
static async getAllData(req, res, next){
try {
const article = await Article.findAll(
{include : [Category, User]}
);
res.status(200).json(article);
} catch (err) {
next(err)
}
}
static async createArticle(req, res, next) {
try {
const data = req.body
data.authorId = req.user.id
const createArticle = await Article.create(data)
if(createArticle){
await History.create({
title: 'POST',
description: `New article with id ${createArticle.id} created`,
updatedBy: `${req.user.username}`
})
}
res.status(201).json(createArticle)
} catch (err) {
next(err)
}
}
static async findArticleById (req, res, next){
try {
const id = req.params.id
const article = await Article.findByPk(id);
if(!article){
res.status(404).json({message: `Article with id ${id} not found`})
}else{
res.status(200).json(article)
}
} catch (error) {
next(error)
}
}
static async deleteById(req, res, next){
try {
const id = +req.params.id
const deleteArticle = await Article.findByPk(id)
if(!deleteArticle) throw {name: 'NotFound'}
await Article.destroy({where : {id}})
res.json({message: `id ${deleteArticle.id} deleted`})
} catch (err) {
next(err)
}
}
static async register(req, res, next){
try {
const {username, email, password, phoneNumber, address} = req.body
// console.log(username, email, password, phoneNumber, address);
const user = await User.create({username, email, password, phoneNumber, address})
res.status(201).json({
massage: `User with id ${user.id} has been created`
})
} catch (err) {
next(err)
}
}
static async login(req, res, next){
try {
const {email, password} = req.body
if(!email || !password) throw {name: 'InvalidCredential'}
const user = await User.findOne({where: {email}})
if(!user) throw {name: 'InvalidCredential'}
const isPassword = comperePassword(password, user.password)
if(!isPassword) throw {name: 'InvalidCredential'}
const access_token = createToken({id: user.id})
res.json({access_token})
} catch (err) {
next(err)
}
}
}
module.exports = Controller
No comments:
Post a Comment