HistoryService: Add setHistory method

This is the transactional reimplementation of the business logic of the history controllers setHistory method (of the private api). This should prevent the problem that the history gets deleted, but a later error in the handling of the list of HistoryEntryImportDto let's the call fail.

See also: https://docs.nestjs.com/techniques/database#transactions

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-11 21:54:50 +02:00 committed by David Mehren
parent 7c648e7056
commit 524ad658d8
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 58 additions and 19 deletions

View file

@ -5,6 +5,7 @@
*/
import {
BadRequestException,
Body,
Controller,
Delete,
@ -16,9 +17,8 @@ import {
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { UsersService } from '../../../../users/users.service';
import { NotesService } from '../../../../notes/notes.service';
import { HistoryEntryDto } from '../../../../history/history-entry.dto';
import { NotInDBError } from '../../../../errors/errors';
import { ForbiddenIdError, NotInDBError } from '../../../../errors/errors';
import { HistoryEntryImportDto } from '../../../../history/history-entry-import.dto';
import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto';
import { ConsoleLoggerService } from '../../../../logger/console-logger.service';
@ -31,7 +31,6 @@ export class HistoryController {
private readonly logger: ConsoleLoggerService,
private historyService: HistoryService,
private userService: UsersService,
private noteService: NotesService,
) {
this.logger.setContext(HistoryController.name);
}
@ -60,21 +59,10 @@ export class HistoryController {
try {
// ToDo: use actual user here
const user = await this.userService.getUserByUsername('hardcoded');
await this.historyService.deleteHistory(user);
for (const historyEntry of history) {
const note = await this.noteService.getNoteByIdOrAlias(
historyEntry.note,
);
await this.historyService.createOrUpdateHistoryEntry(
note,
user,
historyEntry.pinStatus,
historyEntry.lastVisited,
);
}
await this.historyService.setHistory(user, history);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
if (e instanceof NotInDBError || e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}