-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
165 lines (165 loc) · 5.94 KB
/
api.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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const port = 3000;
const cors = require("cors");
const multer = require("multer");
const path = require("path");
var bodyParser = require('body-parser');
mongoose.connect("mongodb+srv://Lenilk:[email protected]/Refrigurator");
const databased = mongoose.connection;
const InRefrigurator = require("./model/inRefriguratorModel");
const Want = require("./model/wantModel");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
var formidable = require('formidable');
var fs = require('fs');
databased.on('error', (error) => {
console.log(error);
});
databased.once('connected', () => {
console.log('Database Connected');
});
app.get("/getRf", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const data = yield InRefrigurator.find();
res.json(data);
}
catch (error) {
res.status(500).json({ message: error.message });
}
}));
app.get("/getWant", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const data = yield Want.find();
res.json(data);
}
catch (error) {
res.status(500).json({ message: error.message });
}
}));
app.get("/version", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
yield fs.readFile("appversion.txt", 'utf-8', (err, data) => __awaiter(void 0, void 0, void 0, function* () {
let json = yield JSON.stringify({ "version": data });
res.json(json);
}));
}));
app.get("/updatePage", (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<form action="updateApp" method="post" enctype="multipart/form-data">');
res.write('<input type="text" name="version" placeholder="version"><br>');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
});
app.get("/downloadapp", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
res.download(path.join(__dirname + "/App/app-release.apk"));
}));
app.post("/updateApp", (req, res) => {
var form = formidable({ multiples: true });
console.log(form);
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.filepath;
var version = fields["version"];
var newpath = path.join(__dirname + "/App/" + files.filetoupload.originalFilename);
fs.rename(oldpath, newpath, function (err) {
if (err)
throw err;
res.write('File uploaded and moved!');
res.end();
});
fs.writeFile('appversion.txt', String(version), function (err) {
if (err)
throw err;
console.log('Saved!');
});
});
});
app.post("/postRf", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const data = InRefrigurator({
name: req.body.name,
Amount: req.body.Amount
});
try {
const dataToSave = yield data.save();
res.status(200).json(dataToSave);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.post("/postWant", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const data = Want({
name: req.body.name,
Amount: req.body.Amount,
Comment: req.body.Comment
});
try {
const dataToSave = yield data.save();
res.status(200).json(dataToSave);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.delete("/deleteRf/:id", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const id = req.params.id;
const data = yield InRefrigurator.findByIdAndDelete(id);
res.send(`Document with ${data.name} has been deleted..`);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.delete("/deleteWant/:id", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const id = req.params.id;
const data = yield Want.findByIdAndDelete(id);
res.send(`Document with ${data.name} has been deleted..`);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.patch('/updateRf/:id', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const id = req.params.id;
const updatedData = req.body;
const options = { new: true };
const result = yield InRefrigurator.findByIdAndUpdate(id, updatedData, options);
res.send(result);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.patch('/updateWant/:id', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const id = req.params.id;
const updatedData = req.body;
const options = { new: true };
const result = yield Want.findByIdAndUpdate(id, updatedData, options);
res.send(result);
}
catch (error) {
res.status(400).json({ message: error.message });
}
}));
app.listen(port, () => {
console.log(`start in ${port}`);
});