mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-16 08:04:45 -04:00
HistoryService: Refactor deleteHistoryEntry
The function now expects a `Note` object instead of a noteId to make it more consistent with other functions. Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
839877dbc5
commit
d2b60a316f
6 changed files with 13 additions and 13 deletions
|
@ -107,11 +107,13 @@ export class HistoryController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':note')
|
@Delete(':note')
|
||||||
async deleteHistoryEntry(@Param('note') noteId: string): Promise<void> {
|
async deleteHistoryEntry(
|
||||||
|
@Param('note', GetNotePipe) note: Note,
|
||||||
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// ToDo: use actual user here
|
// ToDo: use actual user here
|
||||||
const user = await this.userService.getUserByUsername('hardcoded');
|
const user = await this.userService.getUserByUsername('hardcoded');
|
||||||
await this.historyService.deleteHistoryEntry(noteId, user);
|
await this.historyService.deleteHistoryEntry(note, user);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof NotInDBError) {
|
if (e instanceof NotInDBError) {
|
||||||
throw new NotFoundException(e.message);
|
throw new NotFoundException(e.message);
|
||||||
|
|
|
@ -149,7 +149,7 @@ export class MeController {
|
||||||
@ApiNotFoundResponse({ description: notFoundDescription })
|
@ApiNotFoundResponse({ description: notFoundDescription })
|
||||||
async deleteHistoryEntry(
|
async deleteHistoryEntry(
|
||||||
@RequestUser() user: User,
|
@RequestUser() user: User,
|
||||||
@Param('note') note: string,
|
@Param('note', GetNotePipe) note: Note,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// ToDo: Check if user is allowed to delete note
|
// ToDo: Check if user is allowed to delete note
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -311,7 +311,7 @@ describe('HistoryService', () => {
|
||||||
return entry;
|
return entry;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
await service.deleteHistoryEntry(alias, user);
|
await service.deleteHistoryEntry(note, user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('fails', () => {
|
describe('fails', () => {
|
||||||
|
@ -321,7 +321,7 @@ describe('HistoryService', () => {
|
||||||
const note = Note.create(user, alias);
|
const note = Note.create(user, alias);
|
||||||
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
|
jest.spyOn(historyRepo, 'findOne').mockResolvedValueOnce(undefined);
|
||||||
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
|
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(note);
|
||||||
await expect(service.deleteHistoryEntry(alias, user)).rejects.toThrow(
|
await expect(service.deleteHistoryEntry(note, user)).rejects.toThrow(
|
||||||
NotInDBError,
|
NotInDBError,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -130,12 +130,12 @@ export class HistoryService {
|
||||||
/**
|
/**
|
||||||
* @async
|
* @async
|
||||||
* Delete the history entry identified by the user and a note id or alias
|
* Delete the history entry identified by the user and a note id or alias
|
||||||
* @param {string} noteIdOrAlias - the note that the history entry belongs to
|
* @param {Note} note - the note that the history entry belongs to
|
||||||
* @param {User} user - the user that the history entry belongs to
|
* @param {User} user - the user that the history entry belongs to
|
||||||
* @throws {NotInDBError} the specified history entry does not exist
|
* @throws {NotInDBError} the specified history entry does not exist
|
||||||
*/
|
*/
|
||||||
async deleteHistoryEntry(noteIdOrAlias: string, user: User): Promise<void> {
|
async deleteHistoryEntry(note: Note, user: User): Promise<void> {
|
||||||
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
const entry = await this.getEntryByNote(note, user);
|
||||||
await this.historyEntryRepository.remove(entry);
|
await this.historyEntryRepository.remove(entry);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,7 +191,7 @@ describe('History', () => {
|
||||||
const userEntries = await historyService.getEntriesByUser(user);
|
const userEntries = await historyService.getEntriesByUser(user);
|
||||||
expect(userEntries.length).toEqual(1);
|
expect(userEntries.length).toEqual(1);
|
||||||
expect(userEntries[0].pinStatus).toBeTruthy();
|
expect(userEntries[0].pinStatus).toBeTruthy();
|
||||||
await historyService.deleteHistoryEntry(note2.alias, user);
|
await historyService.deleteHistoryEntry(note2, user);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DELETE /me/history/:note', async () => {
|
it('DELETE /me/history/:note', async () => {
|
||||||
|
|
|
@ -96,10 +96,8 @@ describe('Me', () => {
|
||||||
it(`GET /me/history`, async () => {
|
it(`GET /me/history`, async () => {
|
||||||
const noteName = 'testGetNoteHistory1';
|
const noteName = 'testGetNoteHistory1';
|
||||||
const note = await notesService.createNote('', noteName);
|
const note = await notesService.createNote('', noteName);
|
||||||
const createdHistoryEntry = await historyService.updateHistoryEntryTimestamp(
|
const createdHistoryEntry =
|
||||||
note,
|
await historyService.updateHistoryEntryTimestamp(note, user);
|
||||||
user,
|
|
||||||
);
|
|
||||||
const response = await request(app.getHttpServer())
|
const response = await request(app.getHttpServer())
|
||||||
.get('/me/history')
|
.get('/me/history')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue