From f1170341fb7543d08500a293a531bd347ecce909 Mon Sep 17 00:00:00 2001 From: Rodrigo Pinto Date: Sat, 5 Apr 2025 01:15:42 -0300 Subject: [PATCH] Classes and interfaces Changed the topic model to use classes and interfaces instead of direct assignments. --- src/controllers/topicController.ts | 32 +++---------------- src/models/topic.ts | 49 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/controllers/topicController.ts b/src/controllers/topicController.ts index d361f45..cb6964f 100644 --- a/src/controllers/topicController.ts +++ b/src/controllers/topicController.ts @@ -1,22 +1,12 @@ import { Request, Response, NextFunction } from 'express'; -import { topics, Topic } from '../models/topic'; +import { topics, Topic, TopicNode } from '../models/topic'; import { finder } from '../utils' // Create a topic export const createTopic = (req: Request, res: Response, next: NextFunction) => { try { - const { name, content } = req.body; - - const newTopic: Topic = { - id: Date.now(), - name, - content, - createdAt: new Date().toString(), - updatedAt: new Date().toString(), - version: 1, - parentTopicId: req.body.parentTopicId ?? undefined - }; - + const { name, content, parentTopicId } = req.body; + const newTopic = new TopicNode(name, content, null, null, null, parentTopicId) topics.push(newTopic); res.status(201).json(newTopic); @@ -86,8 +76,7 @@ export const getTopicByIdRecursive = (req: Request, res: Response, next: NextFun export const updateTopic = (req: Request, res: Response, next: NextFunction) => { try { const id = parseInt(req.params.id, 10); - const { name } = req.body; - + const { name, content, parentTopicId } = req.body; const oldTopicArray = topics.filter(t => t.id === id) if (oldTopicArray.length === 0) { @@ -96,18 +85,7 @@ export const updateTopic = (req: Request, res: Response, next: NextFunction) => } const oldTopic = oldTopicArray[oldTopicArray.length - 1] - - const newTopic: Topic = { - id: oldTopic.id, - name: oldTopic.name, - content: oldTopic.content, - createdAt: oldTopic.createdAt, - updatedAt: new Date().toString(), - version: oldTopic.version + 1, - parentTopicId: oldTopic.parentTopicId ?? undefined - }; - - topics.push(newTopic) + const newTopic = oldTopic.update(name, content, parentTopicId) res.status(201).json(newTopic) } catch (error) { diff --git a/src/models/topic.ts b/src/models/topic.ts index d6bb766..2070945 100644 --- a/src/models/topic.ts +++ b/src/models/topic.ts @@ -6,6 +6,55 @@ export interface Topic { updatedAt: string version: number parentTopicId?: number + + update(name:string, content: string, parentTopicId: number): Topic +} + +export class TopicNode implements Topic { + id: number + name: string + content: string + createdAt: string + updatedAt: string + version: number + parentTopicId?: number + + constructor( + name: string, + content: string, + id?: number, + createdAt?: string, + version?: number, + parentTopicId?: number + ) { + this.id = id ?? Date.now() + this.name = name + this.content = content + this.parentTopicId = parentTopicId ?? undefined + this.createdAt = createdAt ?? new Date().toString() + this.updatedAt = new Date().toString() + this.version = version ? version++ : 1 + } + + update(name: string, content: string, parentTopicId: number) { + const newName = name ?? this.name + const newContent = content ?? this.content + const newVersion = this.version + 1 + const newParentTopicId = parentTopicId ?? this.parentTopicId + + const newTopic = new TopicNode( + newName, + newContent, + this.id, + this.createdAt, + newVersion, + newParentTopicId + ) + + topics.push(newTopic) + + return (newTopic) + } } export const topics: Topic[] = [];