Skip to content

Commit

Permalink
Fixed up CRUD endpoints for sticky notes
Browse files Browse the repository at this point in the history
Fixed up CRUD endpoints for sticky notes so that they function as intended.
  • Loading branch information
kevinzhong930 committed Nov 12, 2024
1 parent 0f0db5b commit a674275
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
4 changes: 3 additions & 1 deletion devU-api/src/entities/stickyNote/stickyNote.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export async function post(req: Request, res: Response, next: NextFunction) {

export async function put(req: Request, res: Response, next: NextFunction) {
try {
const id = parseInt(req.params.id)
const reqStickyNote = req.body
const stickyNote = await StickyNoteService.update(reqStickyNote)
// const stickyNote = await StickyNoteService.update(reqStickyNote)
const stickyNote = await StickyNoteService.update(id, reqStickyNote)

if (!stickyNote.affected) return res.status(404).json(NotFound)

Expand Down
4 changes: 4 additions & 0 deletions devU-api/src/entities/stickyNote/stickyNote.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ManyToOne,
Entity,
Column,
DeleteDateColumn,
PrimaryGeneratedColumn,
} from 'typeorm'

Expand All @@ -21,4 +22,7 @@ export default class StickyNotesModel {

@Column({ name: 'content' })
content: string

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt?: Date
}
7 changes: 3 additions & 4 deletions devU-api/src/entities/stickyNote/stickyNote.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import StickyNoteController from './stickyNote.controller'

const Router = express.Router({ mergeParams: true })

Router.get('/all',validator , StickyNoteController.listBySubmission)

Router.get('/:id' , asInt(), validator , StickyNoteController.retrieve)

Router.post('/', validator, StickyNoteController.post)

Router.put('/', validator, StickyNoteController.put)
Router.put('/:id', validator, StickyNoteController.put)

Router.delete('/:id', asInt(), validator, StickyNoteController.remove)

Router.get('/all',validator , StickyNoteController.listBySubmission)


export default Router


8 changes: 4 additions & 4 deletions devU-api/src/entities/stickyNote/stickyNote.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import {IsNull} from 'typeorm'
import {IsNull} from 'typeorm'
import {dataSource} from '../../database'

import StickyNotesModel from './stickyNote.model'
Expand All @@ -10,15 +10,15 @@ export async function create(stickyNote: StickyNote) {
return await StickyNoteConn().save(stickyNote)
}

export async function update(stickyNote: StickyNote) {
const {id, submissionId, content} = stickyNote
export async function update(id : number,stickyNote: StickyNote) {
const {submissionId, content} = stickyNote
if (!id) throw new Error('Missing Id')
return await StickyNoteConn().update(id, {submissionId, content})
}

export async function _delete(id: number) {
// return await StickyNoteConn().softDelete({id, deletedAt: IsNull()})
return await StickyNoteConn().softDelete({id})
return await StickyNoteConn().softDelete({id, deletedAt: IsNull()})
}

export async function retrieve(id: number) {
Expand Down
16 changes: 0 additions & 16 deletions devU-api/src/migration/1731177885475-add-sticky-notes.ts

This file was deleted.

14 changes: 14 additions & 0 deletions devU-api/src/migration/1731427638811-add-sticky-note-endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddStickyNoteEndpoints1731427638811 implements MigrationInterface {
name = 'AddStickyNoteEndpoints1731427638811'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "sticky_notes" ADD "deleted_at" TIMESTAMP`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "sticky_notes" DROP COLUMN "deleted_at"`);
}

}

0 comments on commit a674275

Please sign in to comment.