-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
178 lines (148 loc) · 5.57 KB
/
content.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* express, REST API */
const express = require('express');
const app = express(); /* funktioner: app.get(), app.post(), app.put(), app.delete() */
/* tillåter Cross-Origin Resource Sharing, alltså att andra webbsidor kommer åt API:n */
const cors = require('cors');
app.use(cors());
// enable files upload
const fileUpload = require('express-fileupload');
const fs = require("fs");
app.use(fileUpload({
createParentPath: true
}));
app.use(express.static("./uploads")); // Gör uploads offentligt för åtkomst, utanför express
// middleware, for app.get()
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
/* importera databas funktioner som används av api:n */
const database = require("./database");
const {
response,
static
} = require('express');
/* starta api */
function start() {
/*********** GET ***********/
/* startsidan för API */
app.get('/', (req, res) => {
/* kommer åt webbsidan */
res.send('Welcome to our super API!');
});
/* hämtar listdata med alla riksintressen*/
app.get('/api/riksintressen/list', async (req, res) => {
const data = await database.getRiksintressenList();
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämtar listdata med alla raderade riksintressen*/
app.get('/api/riksintressen/list/deleted', async (req, res) => {
const data = await database.getRiksintressenListDeleted();
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämta specifikt riksintresse utifrån ID */
app.get('/api/riksintressen/:id', async (req, res) => {
const data = await database.getRiksintresse(req.params.id);
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämtar alla kommuner */
app.get('/api/kommuner', async (req, res) => {
const data = await database.getKommuner();
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämtar alla lan */
app.get('/api/lan', async (req, res) => {
const data = await database.getLan();
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämtar alla kommuner */
app.get('/api/kulturmiljotyper', async (req, res) => {
const data = await database.getKulturmiljotyp();
res.setHeader("content-type", "application/json");
res.send(JSON.stringify(data));
});
/* hämta ett riksintresses bilder/dokument */
app.get('/api/files/:id', async (req, res) => {
try {
const directoryPath = "uploads/" + req.params.id;
fs.readdir(directoryPath, function (err, files) {
let fileInfos = [];
if (files != undefined) { // kontrollera att filer finns, loopa då igenom de
files.forEach((file) => {
fileInfos.push({
name: file,
url: req.params.id + "/" + file,
});
});
res.send(JSON.stringify(fileInfos));
} else if (err) { // ifall ett riksintresse inte har filer, informera om det.
res.send({
message: "There's no files associated with this riksintresse!",
});
}
});
} catch (err) {
res.send(err);
}
});
/*********** POST ***********/
/* ladda upp dokument/bilder */
app.post('/api/upload', async (req, res) => {
try {
if (!req.files) {
res.send({
status: false,
message: 'No file uploaded'
});
} else {
let file = req.files.file;
//Use the mv() method to place the file in upload directory (i.e. "uploads")
console.log("File: " + file.name + " uploaded for id " + req.body.id);
file.mv('uploads/' + req.body.id + '/' + file.name);
//send response
res.send({
status: true,
message: 'File is uploaded',
data: {
name: file.name,
mimetype: file.mimetype,
size: file.size
}
});
}
} catch (err) {
res.status(500).send(err);
}
});
/* uppdatera ett existerande riksintresse */
app.post('/api/update/riksintresse', async (req, res) => {
let message;
try {
message = await database.updateRiksintresse(req.body); // uppdatera objekt
} catch (exception) {
message = "Could not update riksintresse, exception: " + exception;
}
console.log(message);
res.send(JSON.stringify(message));
});
/* uppdatera ett existerande riksintresse */
app.post('/api/create/riksintresse', async (req, res) => {
let message;
try {
message = await database.createRiksintresse(req.body); // uppdatera objekt
} catch (exception) {
message = "Could not create riksintresse, exception: " + exception;
}
console.log(message.message + " " + message.id);
res.send(message);
});
app.listen(3000, () => console.log('Server started on port 3000')); // starta servern
}
module.exports = {
start: start
}