Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var User = require('../Model/User.js');
- var fs = require('fs');
- var ErrorConstants1 = require('../Errors.js');
- var MessageConstants = require('../message.js');
- function DataBaseConnection() {
- this.connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: '',
- database: 'SPN',
- port: 3306
- });
- }
- DataBaseConnection.prototype.connect = function () {
- this.connection.connect(function (error) {
- if (error) {
- throw error;
- } else {
- console.log('Successful Connection');
- }
- });
- }
- DataBaseConnection.prototype.insertUser = function (user, callback) {
- var db = this;
- this.connection.beginTransaction(function (error) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- }
- db.connection.query(
- "INSERT INTO person VALUES(" + user.personId + ",'" + user.name + "','USER')", function (error, result) {
- if (error) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.person_exists, null);
- });
- } else {
- db.connection.query(
- "INSERT INTO user VALUES('" + user.username + "','./DAO/Image/Profile/" + user.username + user.avatar.extension + "','" + user.password + "','" + user.email + "'," + user.personId + ")", function (error, result) {
- if (error) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.user_exists, null);
- });
- } else {
- var path = "./DAO/Image/Profile/" + user.username + user.avatar.extension;
- fs.writeFile(path, user.avatar.file, 'base64', function (error1) {
- if (error1) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.image_creation_error, null);
- });
- } else {
- db.connection.commit(function (err) {
- if (err) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.data_base_error, null);
- });
- }
- db.connection.end();
- callback(null, MessageConstants.default.user_created);
- });
- }
- });
- }
- });
- }
- });
- });
- }
- DataBaseConnection.prototype.logIn = function (username, password, callback) {
- function log(username, password, db, callback1) {
- db.connection.query("SELECT * FROM user, person WHERE user.person = person.personId AND user.username = '" + username + "'", function (error, result, fields) {
- if (error) {
- callback1(ErrorConstants1.default.data_base_error, null);
- } else {
- if (result.length > 0) {
- if (result[0].password == password) {
- callback1(null, result[0]);
- } else {
- callback1(ErrorConstants1.default.password_incorrect, null);
- }
- } else {
- callback1(ErrorConstants1.default.username_does_not_exists, null);
- }
- }
- });
- }
- log(username, password, this, function (error, result) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, result);
- }
- });
- };
- DataBaseConnection.prototype.createSession = function (session, callback) {
- function create(session, db, callback1) {
- db.connection.query("INSERT INTO session VALUES('" + session.sessionId + "', '" + session.status + "', '" + session.IP + "', '" + session.user.personId + "')", function (error, result, fields) {
- if (error) {
- callback1(ErrorConstants1.default.session_exists, null);
- } else {
- callback1(null, MessageConstants.default.session_created);
- }
- });
- }
- create(session, this, function (error, result) {
- if (error) {
- callback(error, null);
- } else {
- callback(null, result);
- }
- });
- }
- DataBaseConnection.prototype.getSession = function (sessionId, callback) {
- this.connection.query("SELECT * FROM user, session, person WHERE user.person = session.user AND user.person = person.personId AND session.sessionId = '" + sessionId + "'", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- if (result.length > 0) {
- callback(null, result[0]);
- } else {
- callback(ErrorConstants1.default.session_does_not_exists, null);
- }
- }
- });
- }
- DataBaseConnection.prototype.getLastAlbumId = function (callback) {
- this.connection.query("SELECT MAX(albumId) as 'albumId' FROM album", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- callback(null, result[0].albumId);
- }
- });
- }
- DataBaseConnection.prototype.getLastIdImage = function (callback) {
- this.connection.query("SELECT MAX(idImage) as 'idImage' FROM image", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- callback(null, result[0].idImage);
- }
- });
- }
- DataBaseConnection.prototype.getUser = function (username, callback) {
- this.connection.query("SELECT * FROM user,person WHERE person.personId = user.person AND username = '" + username + "'", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- if (result.length > 0) {
- callback(null, result[0]);
- } else {
- callback(ErrorConstants1.default.username_does_not_exists, null);
- }
- }
- });
- }
- DataBaseConnection.prototype.insertAlbum = function (album, callback) {
- this.connection.query("INSERT INTO album VALUES(" + album.albumId + ",'" + album.name + "','" + album.description + "','" + album.user.personId + "')", function (error, result) {
- if (error) {
- callback(ErrorConstants1.default.album_exists, null);
- } else {
- callback(null, MessageConstants.default.album_created);
- }
- });
- }
- DataBaseConnection.prototype.insertImage = function (image, callback) {
- var db = this;
- db.connection.beginTransaction(function (error) {
- if (error) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.data_base_error, null);
- });
- }
- db.connection.query("INSERT INTO image VALUES('" + image.idImage + "','./DAO/Image/Album/" + image.idImage + image.photo.extension + "','" + image.description + "','" + image.title + "','" + image.comment + "','" + image.user.personId + "')", function (error, result) {
- if (error) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.image_exists, null);
- });
- } else {
- var path = "./DAO/Image/Album/" + image.idImage + image.photo.extension;
- fs.writeFile(path, image.photo.file, 'base64', function (error1) {
- if (error1) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.image_creation_error, null);
- });
- } else {
- db.connection.commit(function (err) {
- if (err) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.data_base_error, null);
- });
- }
- db.connection.end();
- callback(null, MessageConstants.default.image_created);
- });
- }
- });
- }
- });
- });
- /*
- this.connection.query("INSERT INTO image VALUES('" + image.idImage + "','./DAO/Image/Album/" + image.idImage + image.photo.extension + "','" + image.description + "','" + image.title + "','" + image.comment + "','" + image.user.personId + "')", function (error, result) {
- if (error) {
- callback(ErrorConstants1.default.image_exists, null);
- } else {
- var path = "./DAO/Image/Album/" + image.idImage + image.photo.extension;
- fs.writeFile(path, image.photo.file, 'base64', function (error1) {
- if (error1) {
- callback(ErrorConstants1.default.image_creation_error, null);
- } else {
- callback(null, MessageConstants.default.image_created);
- }
- });
- }
- });
- */
- }
- DataBaseConnection.prototype.getLastOrderNumber = function (album, callback) {
- this.connection.query("SELECT MAX(orderNumber) as orderNumber FROM albumximage WHERE album = '" + album + "'", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- if (result[0].orderNumber != null) {
- callback(null, parseInt(result[0].orderNumber) + 1);
- } else {
- callback(null, 1);
- }
- }
- });
- }
- DataBaseConnection.prototype.insertAlbumxImage = function (albumxImage, callback) {
- this.connection.query("INSERT INTO albumximage VALUES('" + albumxImage.orderNumber + "','" + albumxImage.image + "','" + albumxImage.album + "')", function (error, result) {
- if (error) {
- callback(ErrorConstants1.default.albumximage_exists, null);
- } else {
- callback(null, MessageConstants.default.albumximage_created);
- }
- });
- }
- DataBaseConnection.prototype.getImage = function (image, callback) {
- this.connection.query("SELECT * FROM image WHERE idImage = '" + image + "'", function (error, result, fields) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- } else {
- if (result.length > 0) {
- callback(null, result[0]);
- } else {
- callback(ErrorConstants1.default.image_does_not_exists, null);
- }
- }
- });
- }
- DataBaseConnection.prototype.updateImage = function (image, callback) {
- var db = this;
- db.connection.beginTransaction(function (error) {
- if (error) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.data_base_error, null);
- });
- }
- else {
- db.connection.query("UPDATE image SET photo = './DAO/Image/Album/" + image.idImage + image.photo.extension + "', description = '" + image.description + "', title ='" + image.title + "', comment = '" + image.comment + "' WHERE idImage = '" + image.idImage + "'", function (error, result) {
- if (error) {
- callback(ErrorConstants1.default.data_base_error, null);
- }
- else {
- var path = "./DAO/Image/Album/" + image.idImage + image.photo.extension;
- fs.writeFile(path, image.photo.file, 'base64', function (error1) {
- if (error1) {
- callback(ErrorConstants1.default.image_updating_error, null);
- } else {
- db.connection.commit(function (err) {
- if (err) {
- db.connection.rollback(function () {
- callback(ErrorConstants1.default.data_base_error, null);
- });
- }
- db.connection.end();
- callback(null, MessageConstants.default.image_updated);
- });
- }
- });
- }
- });
- }
- });
- }
- module.exports = DataBaseConnection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement