multy-stub/server/routers/epja-2023-2/pen-plotter/routes/sub-routes/users.js

65 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-11-25 11:56:14 +03:00
"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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const paths_1 = require("../../paths");
const express_1 = __importDefault(require("express"));
const fs_1 = __importDefault(require("fs"));
const router = express_1.default.Router();
router.get("/", (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
try {
// Get all the users from the profiles directory
const users = yield fs_1.default.promises.readdir(paths_1.PROFILES_PATH);
// For each user, read the meta.json file and add the createdAt property
let response = [];
for (let i = 0; i < users.length; i++) {
// Get the last modification time
const timestamp = yield fs_1.default.statSync(`${paths_1.PROFILES_PATH}/${users[i]}`).mtime;
response.push({ name: users[i], createdAt: timestamp.toISOString() });
}
res.json(response);
}
catch (err) {
console.error(err);
res.status(500).json({ error: "Unable to read the directory" });
}
}));
router.post("/", (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: "Missing name" });
}
try {
yield fs_1.default.promises.mkdir(`${paths_1.PROFILES_PATH}/${name}`);
res.json({ success: true });
}
catch (err) {
console.error(err);
res.status(500).json({ error: "Unable to create the directory" });
}
}));
router.delete("/", (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
const { name } = req.body;
try {
yield fs_1.default.promises.rm(`${paths_1.PROFILES_PATH}/${name}`, {
recursive: true,
});
res.json({ success: true });
}
catch (err) {
console.error(err);
res.status(500).json({ error: "Unable to delete the directory" });
}
}));
exports.default = router;