/
Cooolprogrammers
/
web-nodejs-labs
Обзор
Документация
Войти
/
Cooolprogrammers
/
web-nodejs-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
backend/src/services/lab8/user.service.js
99 строк
3 KB
darkvoid6@mail.ru
ЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫ
18 дек 2025, 07:59
18 дек 2025, 07:59
f2c4f06
Код
Авторство
О чём код?
import fs from 'fs/promises'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Пути к файлам данных const usersFilePath = path.join(__dirname, '..', '..', '..', 'data', 'lab8-users.json'); const task1FilePath = path.join(__dirname, '..', '..', '..', 'data', 'lab8', 'task1.txt'); class UserService { // ========== МЕТОДЫ ДЛЯ ПРИМЕРА (пользователи) ========== async readUsers() { try { const data = await fs.readFile(usersFilePath, 'utf8'); return JSON.parse(data); } catch (error) { if (error.code === 'ENOENT') { return []; } throw error; } } async writeUsers(users) { await fs.writeFile(usersFilePath, JSON.stringify(users, null, 2), 'utf8'); } async getAll() { return await this.readUsers(); } async create(userData) { const users = await this.readUsers(); const nextId = users.length > 0 ? Math.max(...users.map(u => u.id)) + 1 : 1; const newUser = { id: nextId, ...userData }; users.push(newUser); await this.writeUsers(users); return newUser; } // ========== МЕТОДЫ ДЛЯ ЗАДАНИЯ 1 ========== async readTask1Records() { try { const data = await fs.readFile(task1FilePath, 'utf8'); return JSON.parse(data); } catch (error) { if (error.code === 'ENOENT') { return []; } throw error; } } async writeTask1Records(records) { // Создаем директорию, если она не существует await fs.mkdir(path.dirname(task1FilePath), { recursive: true }); await fs.writeFile(task1FilePath, JSON.stringify(records, null, 2), 'utf8'); } async getTask1Records() { return await this.readTask1Records(); } async saveTask1Record(recordData) { const records = await this.readTask1Records(); const nextId = records.length > 0 ? Math.max(...records.map(r => r.id)) + 1 : 1; const newRecord = { id: nextId, ...recordData, timestamp: new Date().toISOString() }; records.push(newRecord); await this.writeTask1Records(records); return newRecord; } makeInitials(lastname, firstname, surname) { return { lastname, firstnameLetter: firstname.charAt(0).toUpperCase(), surnameLetter: surname.charAt(0).toUpperCase() }; } makeProjectStatus(lastname, firstname, surname, projectParticipant) { const initials = this.makeInitials(lastname, firstname, surname); return { ...initials, projectParticipant: projectParticipant ? 'Участвует' : 'Не участвует' }; } } export default new UserService();