-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (112 loc) · 4.82 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const moment = require('moment');
// const aws = require('aws-sdk');
const S3FS = require('s3fs');
const fs = require('fs');
const api = require('./apiKeys');
const S3FSImplementation = new S3FS('3x3macedonia', api);
const multiparty = require('connect-multiparty');
const urlExists = require('url-exists');
const posts = require('./controllers/posts/posts');
const ads = require('./controllers/ads/ads');
const sponsors = require('./controllers/sponsors/sponsors');
const db = knex({
client: 'pg',//deka koristi PostgreSQL
connection: {
connectionString: process.env.DATABASE_URL,
ssl: true,
}
});
const app = express();
const multipartyMiddleware = multiparty();
//anythight that should go to S3 it is getting processed
// to multipartmiddleware, intercepts the file and saves it %temp% requiest.files.file obect,
// router.use(multipartMiddleware);
app.set('views', './views');
//For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
app.use(express.static('./public'));
//not needed?
app.engine('html', require('ejs').renderFile);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cors());
app.use(multiparty(multipartyMiddleware));//
console.log('port', Number(process.env.PORT));
console.log('THE BUCKET IS', process.env.S3_BUCKET);
app.get('/', (req, res) => {
//tuka treba da se renderira front-endot
res.json('Hello world');
});
app.get('/aboutus', (req, res) => {
db('aboutus')
.select('*')
.then(aboutusimages => {
res.status(200).json(aboutusimages);
}).catch(err => console.log(err));
});
app.get('/ads',(req,res)=>{ads.getAds(req,res,db);});
app.get('/ad/:id',(req,res)=>{ads.getAd(req,res,db,fs,S3FSImplementation)});
app.del('/ad/:id',(req,res)=>{ads.deleteAd(req, res, db, fs, S3FSImplementation);});
app.post('/uploadad',(req,res)=>{ads.uploadAd(req,res,db,urlExists,fs,S3FSImplementation);});//treba /ad da se vika
app.get('/posts',(req,res)=>{posts.getPosts(req,res,db)});
app.get('/post_image/:id/:m',(req,res)=>{posts.getImage(req,res,db,S3FSImplementation)});
app.post('/post',(req,res)=>{posts.uploadPost(req,res,db,moment,fs,S3FSImplementation)});
app.patch('/post/',(req,res)=>{posts.editPost(req,res,db,fs,S3FSImplementation)});
app.del('/post_image/:id',(req,res)=>{posts.deletePostImage(req,res,db,S3FSImplementation)});
//next DELETE POST AND THEN FIX THE FRONTEND U FUXTARD
app.del('/post/:post_id',(req,res)=>{posts.deletePost(req,res,db,fs,S3FSImplementation)});
app.get('/sponsors',(req,res)=>{sponsors.getSponsors(req, res, db);});
app.get('/sponsor/:id',(req,res)=>{sponsors.getSponsor(req, res, db, fs, S3FSImplementation);});
app.del('/sponsor/:id', (req, res) => {sponsors.deleteSponsor(req, res, db, S3FSImplementation);});
app.post('/uploadsponsor',(req,res)=>{sponsors.uploadSponsor(req, res, db, urlExists, fs, S3FSImplementation);})
app.post('/signin', (req, res) => {
const {username, password} = req.body;
db.select('hash', 'username')
.where({username: username})
.from('login')
.then(data => { //data sto se vratilo od prethodnoto( email, hash )
if (bcrypt.compareSync(password, data[0].hash)) {
return db.select('*')
.from('users')
.where('username', '=', username)
.then(user => {
res.json(user[0]);
})
.catch(err => res.status(400).json('Unable to get user'))
}
res.status(400).json('Wrong Credentials');
})
.catch(err => res.status(400).json('Wrong Credentials'));
});
//username, password
app.post('/register', (req, res) => {
const {username, password} = req.body;
const hash = bcrypt.hashSync(password);
db.transaction(trx => {
trx.insert({
hash: hash,
username: username
})
.into('login')
.returning('username')
.then(loginUsername => {
return trx('users')
.returning('*')
.insert({
username: loginUsername[0]
})
.then(response => {
res.json(response[0]);
});
})
.then(trx.commit)
.catch(trx.rollback);
}).catch(err => res.status(400).json('Bad Request'));
});
app.listen(process.env.PORT || 3001, () => {
console.log(`app is running on port ${process.env.PORT}`);
});