Update userData to sql
This commit is contained in:
parent
298bfcaabb
commit
71a2245ce2
@ -1,4 +1,5 @@
|
||||
import { type Request, type Response } from "express";
|
||||
import UserModel, { values } from '../models/userModel';
|
||||
import { GameModel } from '../models/mediaModel';
|
||||
|
||||
export default {
|
||||
@ -18,7 +19,13 @@ export default {
|
||||
|
||||
create: async function (req: Request, res: Response) {
|
||||
var gameCode = req.body.code;
|
||||
const userFound = req.user;
|
||||
|
||||
const twitch_client_id = await UserModel.getValue(values.twitch_client_id);
|
||||
const twitch_client_secret = await UserModel.getValue(values.twitch_client_secret);
|
||||
|
||||
if (!twitch_client_id || !twitch_client_secret) {
|
||||
return res.status(500).json({ message: 'Error when creating game' });
|
||||
}
|
||||
|
||||
try {
|
||||
const gameFound = await GameModel.findOne({ code: gameCode });
|
||||
@ -26,13 +33,13 @@ export default {
|
||||
return res.status(409).json({ message: 'Game already exists' });
|
||||
}
|
||||
|
||||
const uri = "https://id.twitch.tv/oauth2/token?client_id=" + userFound.twitch_client_id + "&client_secret=" + userFound.twitch_client_secret + "&grant_type=client_credentials";
|
||||
const uri = "https://id.twitch.tv/oauth2/token?client_id=" + twitch_client_id + "&client_secret=" + twitch_client_secret + "&grant_type=client_credentials";
|
||||
var response = await fetch(uri, { method: 'POST' });
|
||||
const mData = await response.json();
|
||||
|
||||
const mheaders = {
|
||||
const mheaders: HeadersInit = {
|
||||
'Accept': 'application/json',
|
||||
'Client-ID': userFound.twitch_client_id,
|
||||
'Client-ID': twitch_client_id,
|
||||
'Authorization': 'Bearer ' + mData.access_token
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { type Request, type Response } from "express";
|
||||
import UserModel, { values } from '../models/userModel';
|
||||
import { MovieModel, SeriesModel } from '../models/mediaModel';
|
||||
|
||||
/**
|
||||
@ -31,7 +32,13 @@ export default {
|
||||
*/
|
||||
create: async function (req: Request, res: Response) {
|
||||
const mediaCode = req.body.code;
|
||||
const userFound = req.user;
|
||||
|
||||
const omdb_key = await UserModel.getValue(values.omdb_key);
|
||||
|
||||
if (!omdb_key) {
|
||||
return res.status(500).json({ message: 'Error when creating media' });
|
||||
}
|
||||
|
||||
try {
|
||||
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
|
||||
const mediaFound = await MediaModel.findOne({ code: mediaCode });
|
||||
@ -39,7 +46,7 @@ export default {
|
||||
return res.status(409).json({ message: 'Media already exists' });
|
||||
}
|
||||
|
||||
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${userFound.omdb_key}`;
|
||||
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${omdb_key}`;
|
||||
const mJson = await fetch(uri);
|
||||
const mData = await mJson.json();
|
||||
|
||||
|
@ -1,49 +1,50 @@
|
||||
import { type Request, type Response } from "express";
|
||||
import UserModel from '../models/userModel';
|
||||
import UserModel, { values } from '../models/userModel';
|
||||
|
||||
export default {
|
||||
|
||||
create: async function (req: Request, res: Response) {
|
||||
const pass = req.body.pass;
|
||||
|
||||
try {
|
||||
const userFound = await UserModel.findOne({ pass: pass });
|
||||
if (userFound) {
|
||||
return res.status(409).json({ message: 'User already exists' });
|
||||
}
|
||||
|
||||
const user = new UserModel({
|
||||
pass,
|
||||
omdb_key: req.body.omdb_key,
|
||||
twitch_client_id: req.body.twitch_client_id,
|
||||
twitch_client_secret: req.body.twitch_client_secret,
|
||||
mac_address: req.body.mac_address,
|
||||
});
|
||||
|
||||
await user.save();
|
||||
return res.redirect('/list');
|
||||
}
|
||||
catch (err) {
|
||||
return res.status(500).json({ message: 'Error when creating user', error: err });
|
||||
}
|
||||
render: async function (req: Request, res: Response) {
|
||||
res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
},
|
||||
|
||||
remove: async function (req: Request, res: Response) {
|
||||
try {
|
||||
await UserModel.deleteMany();
|
||||
return res.status(204).json({ message: 'User deleted' });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: 'Error when deleting the user', error: err });
|
||||
create: async function (req: Request, res: Response) {
|
||||
|
||||
const reqPassword: string = req.body.reqPassword;
|
||||
if (!reqPassword) return res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
|
||||
const password = await UserModel.getValue(values.pass);
|
||||
|
||||
// if no password in db save reqPassword
|
||||
if (!password) {
|
||||
const affectedRows = await UserModel.updateValue("pass", reqPassword);
|
||||
if (affectedRows > 0) {
|
||||
return res.redirect('/list');
|
||||
}
|
||||
return res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
}
|
||||
// check if passwords equal
|
||||
if (password != reqPassword) {
|
||||
return res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
}
|
||||
|
||||
// update
|
||||
const name: string = req.body.name;
|
||||
const value: string = req.body.value;
|
||||
|
||||
if (!name || !value) {
|
||||
return res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
}
|
||||
|
||||
const affectedRows = await UserModel.updateValue(name, value);
|
||||
if (affectedRows == 0) {
|
||||
return res.render('user', { title: 'Register', keys: UserModel.namesOfValues });
|
||||
}
|
||||
|
||||
return res.redirect('/list');
|
||||
},
|
||||
|
||||
get: async function (req: Request, res: Response) {
|
||||
try {
|
||||
const usersFound = await UserModel.find();
|
||||
|
||||
return res.status(200).json(usersFound);
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: 'Error when getting the user', error: err });
|
||||
}
|
||||
const usersFound = await UserModel.getAll();
|
||||
return res.status(200).json(usersFound);
|
||||
},
|
||||
};
|
@ -1,20 +1,15 @@
|
||||
import { type NextFunction, type Request, type Response } from "express";
|
||||
import UserModel from '../models/userModel';
|
||||
|
||||
import userModel, { values } from '../models/userModel';
|
||||
|
||||
async function checkAuthenticated(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const password = req.body.pass;
|
||||
const userFound = await UserModel.findOne({ pass: password });
|
||||
if (!userFound) {
|
||||
return res.status(404).json({ message: 'Wrong password' });
|
||||
const pass = req.body.pass;
|
||||
const password = await userModel.getValue(values.pass);
|
||||
if (pass && password) {
|
||||
if (pass == password) {
|
||||
return next();
|
||||
}
|
||||
req.user = userFound;
|
||||
return next();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return res.status(500).json({ message: 'Error when getting transactions.' });
|
||||
}
|
||||
return res.status(500).json({ message: 'Error when getting transactions.' });
|
||||
}
|
||||
|
||||
export default checkAuthenticated;
|
13
miscellaneous/custom.d.ts
vendored
13
miscellaneous/custom.d.ts
vendored
@ -1,13 +0,0 @@
|
||||
|
||||
|
||||
declare namespace Express {
|
||||
export interface Request {
|
||||
user?: Document<unknown, {}, {
|
||||
pass?: string | undefined;
|
||||
omdb_key?: string | undefined;
|
||||
twitch_client_id?: string | undefined;
|
||||
twitch_client_secret?: string | undefined;
|
||||
mac_address?: string | undefined;
|
||||
}>
|
||||
}
|
||||
}
|
11
miscellaneous/db.ts
Normal file
11
miscellaneous/db.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import mysql from 'mysql2/promise'
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: '192.168.0.11',
|
||||
port: 3306,
|
||||
user: 'myUsage',
|
||||
password: 'vEj8lFj22srB_(VG',
|
||||
database: 'my_general_db'
|
||||
});
|
||||
|
||||
export default pool;
|
@ -1,13 +1,58 @@
|
||||
import mongoose, { Schema } from 'mongoose';
|
||||
import { type ResultSetHeader, type RowDataPacket } from "mysql2"
|
||||
import pool from '../miscellaneous/db'
|
||||
|
||||
const userSchema = new Schema({
|
||||
'pass': String,
|
||||
'omdb_key': String,
|
||||
'twitch_client_id': String,
|
||||
'twitch_client_secret': String,
|
||||
'mac_address': String,
|
||||
});
|
||||
interface UserD extends RowDataPacket {
|
||||
name?: string;
|
||||
value?: string;
|
||||
}
|
||||
|
||||
const User = mongoose.model('user', userSchema);
|
||||
export enum values {
|
||||
pass = 1,
|
||||
omdb_key,
|
||||
twitch_client_id,
|
||||
twitch_client_secret,
|
||||
mac_address
|
||||
}
|
||||
|
||||
export default User;
|
||||
const namesOfValues: string[] = ["", "pass", "omdb_key", "twitch_client_id", "twitch_client_secret", "mac_address"];
|
||||
|
||||
async function getValue(name: values): Promise<string | undefined> {
|
||||
try {
|
||||
const [rows, fields] = await pool.query<UserD[]>("SELECT name, value FROM userData where id = ?;", [name]);
|
||||
if (rows.length > 0)
|
||||
return rows[0].value;
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async function updateValue(name: string, value: string): Promise<number> {
|
||||
try {
|
||||
const [result, fields] = await pool.query<ResultSetHeader>("UPDATE userData SET value = ? WHERE name = ?", [value, name]);
|
||||
return result.affectedRows;
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function getAll(): Promise<UserD[]> {
|
||||
try {
|
||||
const [rows, fields] = await pool.query<UserD[]>("SELECT name, value FROM userData;");
|
||||
return rows;
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export default {
|
||||
getValue,
|
||||
updateValue,
|
||||
getAll,
|
||||
namesOfValues
|
||||
};
|
||||
|
@ -27,7 +27,7 @@ router.get('/cash', function (req: Request, res: Response) {
|
||||
res.render('cash', { title: 'Cash' });
|
||||
});
|
||||
|
||||
import userRouter from './user';
|
||||
router.use('/user', userRouter);
|
||||
//import userRouter from './user';
|
||||
//router.use('/user', userRouter);
|
||||
|
||||
export default router;
|
@ -5,14 +5,10 @@ import checkAuthenticated from '../miscellaneous/checkAuthenticated';
|
||||
const router = express.Router();
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req: Request, res: Response) {
|
||||
res.render('user', { title: 'Register' });
|
||||
});
|
||||
router.get('/', userController.render);
|
||||
|
||||
router.post('/', userController.create);
|
||||
|
||||
router.delete('/', checkAuthenticated, userController.remove);
|
||||
|
||||
router.put('/', checkAuthenticated, userController.get);
|
||||
|
||||
export default router;
|
60
sql/base.sql
Normal file
60
sql/base.sql
Normal file
@ -0,0 +1,60 @@
|
||||
CREATE TABLE series (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
code INT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
released TEXT NOT NULL,
|
||||
webImg TEXT NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE code (code)
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
CREATE TABLE movies (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
code INT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
released TEXT NOT NULL,
|
||||
webImg TEXT NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE code (code)
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
CREATE TABLE games (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
code INT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
released TEXT NOT NULL,
|
||||
webImg TEXT NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE code (code)
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
CREATE TABLE userData (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
INSERT INTO userData (name, value) VALUES ("pass", "");
|
||||
|
||||
INSERT INTO userData (name, value) VALUES ("omdb_key", "");
|
||||
|
||||
INSERT INTO userData (name, value) VALUES ("twitch_client_id", "");
|
||||
|
||||
INSERT INTO userData (name, value) VALUES ("twitch_client_secret", "");
|
||||
|
||||
INSERT INTO userData (name, value) VALUES ("mac_address", "");
|
||||
|
||||
CREATE TABLE bankCardTransaction (
|
||||
id INT NOT NULL AUTO_INCREMENT,
|
||||
day INT NOT NULL,
|
||||
month INT NOT NULL,
|
||||
year INT NOT NULL,
|
||||
amount INT NOT NULL,
|
||||
type INT NOT NULL,
|
||||
raw TEXT NOT NULL,
|
||||
company TEXT NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
INDEX date(day, month, year),
|
||||
INDEX type (type)
|
||||
) ENGINE = InnoDB;
|
@ -1,50 +1,33 @@
|
||||
<div class="container">
|
||||
<form action="/user" method="post">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">omdb_key</label>
|
||||
<input type="text" class="form-control" name="omdb_key">
|
||||
<label class="form-label">Name</label>
|
||||
<select name="name" class="form-select">
|
||||
{{#each keys}}
|
||||
<option value="{{this}}">{{this}}</option>
|
||||
{{/each}}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">twitch_client_id</label>
|
||||
<input type="text" class="form-control" name="twitch_client_id">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">twitch_client_secret</label>
|
||||
<input type="text" class="form-control" name="twitch_client_secret">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">mac_address</label>
|
||||
<input type="text" class="form-control" name="mac_address">
|
||||
<label class="form-label">Value</label>
|
||||
<input type="text" class="form-control" name="value">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<input type="password" class="form-control" name="pass" id="password">
|
||||
<input type="password" class="form-control" name="reqPassword" id="password">
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Add</button>
|
||||
</form>
|
||||
<br>
|
||||
<div class="btn btn-primary" id="delete" onclick="deleteUser()">Delete</div>
|
||||
<br>
|
||||
<div class="btn btn-primary" id="get" onclick="getUser()"> Get</div>
|
||||
<br>
|
||||
<div id="text"></div>
|
||||
|
||||
<script>
|
||||
|
||||
async function deleteUser() {
|
||||
const password = document.getElementById('password').value;
|
||||
const response = await fetch('/user', {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ "pass": password }),
|
||||
})
|
||||
const data = await response.json();
|
||||
document.getElementById('text').innerHTML = data.message;
|
||||
}
|
||||
|
||||
async function getUser() {
|
||||
const password = document.getElementById('password').value;
|
||||
const response = await fetch('/user', {
|
||||
|
Loading…
x
Reference in New Issue
Block a user