todo list

This commit is contained in:
Primakov Alexandr Alexandrovich
2024-12-05 19:44:54 +03:00
parent ed9eb93013
commit 9723c825f7
11 changed files with 397 additions and 7 deletions

29
src/connect.ts Normal file
View File

@@ -0,0 +1,29 @@
import mongoose from "mongoose";
import ItemListModel from "./model/todo-list";
import ItemModel from "./model/todo-item";
export const connect = async () => {
await mongoose.connect(process.env.MONGO_CONNECT_URL!);
console.log("Connected to database");
const lists = await ItemListModel.find();
console.log(JSON.stringify(lists, null, 4))
if (lists.length === 0) {
await ItemListModel.create({
name: "Test List",
});
const item = await ItemModel.create({
title: "Test Item",
});
lists.forEach(async (list) => {
await (list as unknown as any).addItem(item._id);
})
}
console.log("Database initiated");
};

View File

@@ -1,8 +1,9 @@
import express, { json } from 'express';
import { handleError } from './utils/errorHandler'
import 'dotenv/config'
import { router } from './routes'
import { handleError } from './utils/errorHandler'
import { connect } from './connect';
const app = express();
@@ -13,6 +14,14 @@ app.use(json({ limit: '100kb' }))
app.use(router)
app.use(handleError)
app.listen(port, () => {
console.log(`App is running on port http://localhost:${port}`);
})
const start = async () => {
console.log('starting...')
await connect()
app.listen(port, () => {
console.log(`App is running on port http://localhost:${port}`);
})
}
start()

24
src/model/todo-item.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Schema, model } from 'mongoose'
const schema = new Schema({
title: {type: String, required: true },
description: String,
created: { type: Date, default: () => new Date().toISOString() },
done: { type: Boolean, default: false },
// createdBy: { type: Schema.Types.ObjectId, required: true, ref: 'User' }
deleted: { type: Boolean, default: false }
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: (doc, ret) => {
delete ret._id
}
})
export default model('Item', schema)

34
src/model/todo-list.ts Normal file
View File

@@ -0,0 +1,34 @@
import { Schema, model } from 'mongoose'
const schema = new Schema({
name: String,
items: [{ type: Schema.Types.ObjectId, required: true, ref: 'Item' }],
created: { type: Date, default: () => new Date().toISOString() },
// createdBy: { type: Schema.Types.ObjectId, required: true, ref: 'User' }
deleted: { type: Boolean, default: false }
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: (doc, ret) => {
delete ret._id
}
})
schema.method('addItem', async function(item) {
this.items.push(item._id)
return await this.save()
})
schema.method('removeItem', async function(item) {
this.items = this.items.filter(i => i !== item._id)
return await this.save()
})
export default model('TodoList', schema)

View File

@@ -3,9 +3,11 @@ import { Router } from "express";
import pkg from '../../package.json'
import { router as usersRouter } from './users'
import { router as todoRouter } from './todo'
export const router = Router();
router.get('/healthcheck', (req, res) => void res.send({ ok: true, version: pkg.version }));
router.use('/users', usersRouter)
router.use('/users', usersRouter)
router.use('/todo', todoRouter)

66
src/routes/todo/index.ts Normal file
View File

@@ -0,0 +1,66 @@
import { Router } from "express";
import ListsModel from '../../model/todo-list';
import ItemModel from '../../model/todo-item';
export const router = Router();
router.get('/lists', async (req, res) => {
const lists = await ListsModel.find({});
res.json(lists);
})
router.post('/list', async (req, res) => {
const { name } = req.body
const list = await ListsModel.create({
name
});
res.json(list);
})
router.delete('/item/:itemId', async (req, res) => {
const { itemId } = req.params;
const item = await ItemModel.findById(itemId);
if (!item) throw new Error('Item not found');
await ItemModel.findByIdAndDelete(itemId);
res.send(item);
})
router.get('/list/:listId', async (req, res) => {
const { listId } = req.params;
const list = await ListsModel
.findById(listId)
.populate('items')
.exec();
if (!list) throw new Error('List not found');
res.json(list);
})
router.post('/:listId/item', async (req, res) => {
const { listId } = req.params
const { title, description = '' } = req.body
const list = await ListsModel.findById(listId);
if (!list) {
throw new Error('List not found');
}
const item = await ItemModel.create({
title,
description
});
await (list as any).addItem(item._id);
res.send(item)
})

View File

@@ -1,7 +1,6 @@
import { Router } from "express";
import { Validator } from "express-json-validator-middleware";
import bkfd2Password from "pbkdf2-password";
import { promisify } from 'node:util'
import jwt from 'jsonwebtoken'