-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from makeopensource/154-sticky-notes-endpoints
154 sticky notes endpoints
- Loading branch information
Showing
15 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {NextFunction, Request, Response} from 'express' | ||
|
||
import StickyNoteService from './stickyNote.service' | ||
|
||
import {NotFound, Updated} from '../../utils/apiResponse.utils' | ||
|
||
import {serialize} from './stickyNote.serializer' | ||
|
||
export async function retrieve(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const id = parseInt(req.params.id) | ||
const stickyNote = await StickyNoteService.retrieve(id) | ||
|
||
if (!stickyNote) return res.status(404).json(NotFound) | ||
|
||
const response = serialize(stickyNote) | ||
|
||
res.status(200).json(response) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
export async function post(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const reqStickyNote = req.body | ||
const stickyNote = await StickyNoteService.create(reqStickyNote) | ||
const response = serialize(stickyNote) | ||
|
||
res.status(201).json(response) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
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(id, reqStickyNote) | ||
|
||
if (!stickyNote.affected) return res.status(404).json(NotFound) | ||
|
||
res.status(200).json(Updated) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
export async function remove(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const id = parseInt(req.params.id) | ||
await StickyNoteService._delete(id) | ||
|
||
res.status(204).send() | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
export async function listBySubmission(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const submissionId = parseInt(req.params.submissionId) | ||
const stickyNotes = await StickyNoteService.listBySubmission(submissionId) | ||
const response = stickyNotes.map(serialize) | ||
|
||
res.status(200).json(response) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
export default { | ||
retrieve, | ||
post, | ||
put, | ||
remove, | ||
listBySubmission, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
JoinColumn, | ||
ManyToOne, | ||
Entity, | ||
Column, | ||
DeleteDateColumn, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm' | ||
|
||
import SubmissionModel from '../submission/submission.model' | ||
|
||
@Entity('sticky_notes') | ||
export default class StickyNotesModel { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column({ name: 'submissionId' }) | ||
@JoinColumn({ name: 'submissionId' }) | ||
@ManyToOne(() => SubmissionModel) | ||
submissionId: number | ||
|
||
@Column({ name: 'content' }) | ||
content: string | ||
|
||
@DeleteDateColumn({ name: 'deleted_at' }) | ||
deletedAt?: Date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import express from 'express' | ||
|
||
// Middleware | ||
import validator from './stickyNote.validator' | ||
import { isAuthorized } from '../../authorization/authorization.middleware' | ||
import { asInt } from '../../middleware/validator/generic.validator' | ||
|
||
// Controller | ||
import StickyNoteController from './stickyNote.controller' | ||
|
||
const Router = express.Router({ mergeParams: true }) | ||
|
||
Router.get('/all', isAuthorized("stickyNoteViewAll"), validator , StickyNoteController.listBySubmission) | ||
|
||
Router.get('/:id' ,isAuthorized("stickyNoteViewAll") , asInt("id"), validator , StickyNoteController.retrieve) | ||
|
||
Router.post('/',isAuthorized("stickyNoteEditAll") ,validator, StickyNoteController.post) | ||
|
||
Router.put('/:id',isAuthorized("stickyNoteEditAll") , validator, StickyNoteController.put) | ||
|
||
Router.delete('/:id',isAuthorized("stickyNoteEditAll") , asInt("id"), validator, StickyNoteController.remove) | ||
|
||
export default Router | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {StickyNote} from 'devu-shared-modules' | ||
|
||
import StickyNoteModel from './stickyNote.model' | ||
|
||
export function serialize(stickyNote: StickyNoteModel): StickyNote { | ||
return { | ||
id: stickyNote.id, | ||
submissionId: stickyNote.submissionId, | ||
content: stickyNote.content, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {IsNull} from 'typeorm' | ||
import {dataSource} from '../../database' | ||
|
||
import StickyNotesModel from './stickyNote.model' | ||
import {StickyNote} from 'devu-shared-modules' | ||
|
||
const StickyNoteConn = () => dataSource.getRepository(StickyNotesModel) | ||
|
||
export async function create(stickyNote: StickyNote) { | ||
return await StickyNoteConn().save(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()}) | ||
} | ||
|
||
export async function retrieve(id: number) { | ||
return await StickyNoteConn().findOneBy({id, deletedAt: IsNull()}) | ||
} | ||
|
||
export async function listBySubmission(submissionId: number) { | ||
return await StickyNoteConn().findBy({submissionId: submissionId , deletedAt: IsNull()}) | ||
} | ||
|
||
export default { | ||
create, | ||
update, | ||
_delete, | ||
retrieve, | ||
listBySubmission, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {check} from 'express-validator' | ||
|
||
import validate from '../../middleware/validator/generic.validator' | ||
|
||
const submissionId = check('submissionId').isNumeric() | ||
const content = check('content').isString() | ||
|
||
const validator = [submissionId, content, validate] | ||
|
||
export default validator |
14 changes: 14 additions & 0 deletions
14
devU-api/src/migration/1731427638811-add-sticky-note-endpoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
devU-api/src/migration/1731433278166-updatedRolesWithStickyNotes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class UpdatedRolesWithStickyNotes1731433278166 implements MigrationInterface { | ||
name = 'UpdatedRolesWithStickyNotes1731433278166' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "role" ADD "sticky_note_view_all" boolean NOT NULL`); | ||
await queryRunner.query(`ALTER TABLE "role" ADD "sticky_note_edit_all" boolean NOT NULL`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "role" DROP COLUMN "sticky_note_edit_all"`); | ||
await queryRunner.query(`ALTER TABLE "role" DROP COLUMN "sticky_note_view_all"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type StickyNote = { | ||
id?: number | ||
submissionId: number | ||
content: string | ||
} |