Skip to content

Commit

Permalink
Merge branch 'main' of github.com:codex-team/notes.api into feat/team…
Browse files Browse the repository at this point in the history
…s-base
  • Loading branch information
slaveeks committed Oct 22, 2023
2 parents e906afe + c4993b8 commit 4dfb4c2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export default class NoteService {
});
}

/**
* Deletes note by id
*
* @param id - note internal id
*/
public async deleteNoteById(id: NoteInternalId): Promise<boolean> {
return await this.repository.deleteNoteById(id);
}

/**
* Updates a note
*
Expand Down
37 changes: 37 additions & 0 deletions src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don
return reply.send(note);
});

/**
* Deletes note by id
*/
fastify.delete<{
Params: {
notePublicId : NotePublicId;
},
Reply: {
isDeleted : boolean
},
}>('/:notePublicId', {
schema: {
params: {
notePublicId: {
$ref: 'NoteSchema#/properties/id',
},
},
},
config: {
policy: [
'authRequired',
'userInTeam',
],
},
preHandler: [
noteResolver,
],
}, async (request, reply) => {
const noteId = request.note?.id as number;
const isDeleted = await noteService.deleteNoteById(noteId);

/**
* Check if note does not exist
*/
return reply.send({ isDeleted : isDeleted });
});

/**
* Adds a new note.
* Responses with note public id.
Expand Down
11 changes: 10 additions & 1 deletion src/repository/note.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class NoteRepository {
}

/**
* Gets note by id
* Gets note by internal id
*
* @param id - note id
* @returns { Promise<Note | null> } found note
Expand All @@ -51,6 +51,15 @@ export default class NoteRepository {
return await this.storage.getNoteById(id);
}

/**
* Deletes note by id
*
* @param id - note id
*/
public async deleteNoteById(id: NoteInternalId): Promise<boolean> {
return await this.storage.deleteNoteById(id);
}

/**
* Gets note by hostname
*
Expand Down
18 changes: 18 additions & 0 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ export default class NoteSequelizeStorage {
return note;
}

/**
* Deletes note by id
*
* @param id - internal id
*/
public async deleteNoteById(id: NoteInternalId): Promise<boolean> {
const affectedRows = await this.model.destroy({
where:{
id,
},
});

/**
* If note not found return false
*/
return affectedRows > 0;
}

/**
* Gets note list by creator id
*
Expand Down

0 comments on commit 4dfb4c2

Please sign in to comment.