Classes and interfaces
Changed the topic model to use classes and interfaces instead of direct assignments.
This commit is contained in:
parent
8ef27f33d6
commit
f1170341fb
2 changed files with 54 additions and 27 deletions
|
@ -1,22 +1,12 @@
|
||||||
import { Request, Response, NextFunction } from 'express';
|
import { Request, Response, NextFunction } from 'express';
|
||||||
import { topics, Topic } from '../models/topic';
|
import { topics, Topic, TopicNode } from '../models/topic';
|
||||||
import { finder } from '../utils'
|
import { finder } from '../utils'
|
||||||
|
|
||||||
// Create a topic
|
// Create a topic
|
||||||
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
|
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const { name, content } = req.body;
|
const { name, content, parentTopicId } = req.body;
|
||||||
|
const newTopic = new TopicNode(name, content, null, null, null, parentTopicId)
|
||||||
const newTopic: Topic = {
|
|
||||||
id: Date.now(),
|
|
||||||
name,
|
|
||||||
content,
|
|
||||||
createdAt: new Date().toString(),
|
|
||||||
updatedAt: new Date().toString(),
|
|
||||||
version: 1,
|
|
||||||
parentTopicId: req.body.parentTopicId ?? undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
topics.push(newTopic);
|
topics.push(newTopic);
|
||||||
|
|
||||||
res.status(201).json(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) => {
|
export const updateTopic = (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const id = parseInt(req.params.id, 10);
|
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)
|
const oldTopicArray = topics.filter(t => t.id === id)
|
||||||
|
|
||||||
if (oldTopicArray.length === 0) {
|
if (oldTopicArray.length === 0) {
|
||||||
|
@ -96,18 +85,7 @@ export const updateTopic = (req: Request, res: Response, next: NextFunction) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldTopic = oldTopicArray[oldTopicArray.length - 1]
|
const oldTopic = oldTopicArray[oldTopicArray.length - 1]
|
||||||
|
const newTopic = oldTopic.update(name, content, parentTopicId)
|
||||||
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)
|
|
||||||
|
|
||||||
res.status(201).json(newTopic)
|
res.status(201).json(newTopic)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -6,6 +6,55 @@ export interface Topic {
|
||||||
updatedAt: string
|
updatedAt: string
|
||||||
version: number
|
version: number
|
||||||
parentTopicId?: 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[] = [];
|
export const topics: Topic[] = [];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue