PrivateAPI: Add history controller

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-03-03 15:25:11 +01:00 committed by David Mehren
parent b872694158
commit 29dbf0b657
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
4 changed files with 225 additions and 2 deletions

View file

@ -0,0 +1,78 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Test, TestingModule } from '@nestjs/testing';
import { HistoryController } from './history.controller';
import { LoggerModule } from '../../../../logger/logger.module';
import { UsersModule } from '../../../../users/users.module';
import { HistoryModule } from '../../../../history/history.module';
import { NotesModule } from '../../../../notes/notes.module';
import { getRepositoryToken } from '@nestjs/typeorm';
import { User } from '../../../../users/user.entity';
import { Note } from '../../../../notes/note.entity';
import { AuthToken } from '../../../../auth/auth-token.entity';
import { Identity } from '../../../../users/identity.entity';
import { AuthorColor } from '../../../../notes/author-color.entity';
import { Authorship } from '../../../../revisions/authorship.entity';
import { Revision } from '../../../../revisions/revision.entity';
import { Tag } from '../../../../notes/tag.entity';
import { HistoryEntry } from '../../../../history/history-entry.entity';
import { NoteGroupPermission } from '../../../../permissions/note-group-permission.entity';
import { NoteUserPermission } from '../../../../permissions/note-user-permission.entity';
import { Group } from '../../../../groups/group.entity';
import { ConfigModule } from '@nestjs/config';
import appConfigMock from '../../../../config/app.config.mock';
describe('HistoryController', () => {
let controller: HistoryController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HistoryController],
imports: [
UsersModule,
HistoryModule,
NotesModule,
LoggerModule,
ConfigModule.forRoot({
isGlobal: true,
load: [appConfigMock],
}),
],
})
.overrideProvider(getRepositoryToken(User))
.useValue({})
.overrideProvider(getRepositoryToken(Note))
.useValue({})
.overrideProvider(getRepositoryToken(AuthToken))
.useValue({})
.overrideProvider(getRepositoryToken(Identity))
.useValue({})
.overrideProvider(getRepositoryToken(AuthorColor))
.useValue({})
.overrideProvider(getRepositoryToken(Authorship))
.useValue({})
.overrideProvider(getRepositoryToken(Revision))
.useValue({})
.overrideProvider(getRepositoryToken(Tag))
.useValue({})
.overrideProvider(getRepositoryToken(HistoryEntry))
.useValue({})
.overrideProvider(getRepositoryToken(NoteGroupPermission))
.useValue({})
.overrideProvider(getRepositoryToken(NoteUserPermission))
.useValue({})
.overrideProvider(getRepositoryToken(Group))
.useValue({})
.compile();
controller = module.get<HistoryController>(HistoryController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View file

@ -0,0 +1,127 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
Body,
Controller,
Delete,
Get,
NotFoundException,
Param,
Post,
Put,
} 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 { HistoryEntryCreationDto } from '../../../../history/history-entry-creation.dto';
import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto';
import { ConsoleLoggerService } from '../../../../logger/console-logger.service';
import { HistoryService } from '../../../../history/history.service';
@ApiTags('history')
@Controller('/me/history')
export class HistoryController {
constructor(
private readonly logger: ConsoleLoggerService,
private historyService: HistoryService,
private userService: UsersService,
private noteService: NotesService,
) {
this.logger.setContext(HistoryController.name);
}
@Get()
async getHistory(): Promise<HistoryEntryDto[]> {
// ToDo: use actual user here
try {
const user = await this.userService.getUserByUsername('hardcoded');
const foundEntries = await this.historyService.getEntriesByUser(user);
return foundEntries.map((entry) =>
this.historyService.toHistoryEntryDto(entry),
);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
}
@Post()
async setHistory(
@Body('history') history: HistoryEntryCreationDto[],
): Promise<void> {
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);
}
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
}
@Delete()
async deleteHistory(): Promise<void> {
try {
// ToDo: use actual user here
const user = await this.userService.getUserByUsername('hardcoded');
await this.historyService.deleteHistory(user);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
}
@Put(':note')
async updateHistoryEntry(
@Param('note') noteId: string,
@Body() entryUpdateDto: HistoryEntryUpdateDto,
): Promise<HistoryEntryDto> {
try {
// ToDo: use actual user here
const user = await this.userService.getUserByUsername('hardcoded');
const newEntry = await this.historyService.updateHistoryEntry(
noteId,
user,
entryUpdateDto,
);
return this.historyService.toHistoryEntryDto(newEntry);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
}
@Delete(':note')
async deleteHistoryEntry(@Param('note') noteId: string): Promise<void> {
try {
// ToDo: use actual user here
const user = await this.userService.getUserByUsername('hardcoded');
await this.historyService.deleteHistoryEntry(noteId, user);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
}
}

View file

@ -9,9 +9,12 @@ import { TokensController } from './tokens/tokens.controller';
import { LoggerModule } from '../../logger/logger.module';
import { UsersModule } from '../../users/users.module';
import { AuthModule } from '../../auth/auth.module';
import { HistoryController } from './me/history/history.controller';
import { HistoryModule } from '../../history/history.module';
import { NotesModule } from '../../notes/notes.module';
@Module({
imports: [LoggerModule, UsersModule, AuthModule],
controllers: [TokensController],
imports: [LoggerModule, UsersModule, AuthModule, HistoryModule, NotesModule],
controllers: [TokensController, HistoryController],
})
export class PrivateApiModule {}

View file

@ -0,0 +1,15 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { IsString } from 'class-validator';
export class HistoryEntryCreationDto {
/**
* ID or Alias of the note
*/
@IsString()
note: string;
}