mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-23 10:16:32 -05:00
UsersService: Add methods to find, create and delete users
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
ec8cf6d33e
commit
7997a0955a
3 changed files with 49 additions and 13 deletions
|
@ -29,8 +29,10 @@ export class MeController {
|
|||
}
|
||||
|
||||
@Get()
|
||||
getMe(): UserInfoDto {
|
||||
return this.usersService.getUserInfo();
|
||||
async getMe(): Promise<UserInfoDto> {
|
||||
return this.usersService.toUserDto(
|
||||
await this.usersService.getUserByUsername('hardcoded'),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('history')
|
||||
|
|
|
@ -48,4 +48,20 @@ export class User {
|
|||
identity => identity.user,
|
||||
)
|
||||
identities: Identity[];
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
|
||||
public static create(
|
||||
userName: string,
|
||||
displayName: string,
|
||||
): Pick<
|
||||
User,
|
||||
'userName' | 'displayName' | 'ownedNotes' | 'authToken' | 'identities'
|
||||
> {
|
||||
const newUser = new User();
|
||||
newUser.userName = userName;
|
||||
newUser.displayName = displayName;
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,44 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { NotInDBError } from '../errors/errors';
|
||||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||
import { UserInfoDto } from './user-info.dto';
|
||||
import { User } from './user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly logger: ConsoleLoggerService) {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
@InjectRepository(User) private userRepository: Repository<User>,
|
||||
) {
|
||||
this.logger.setContext(UsersService.name);
|
||||
}
|
||||
|
||||
getUserInfo(): UserInfoDto {
|
||||
//TODO: Use the database
|
||||
this.logger.warn('Using hardcoded data!');
|
||||
return {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
};
|
||||
createUser(userName: string, displayName: string): Promise<User> {
|
||||
const user = User.create(userName, displayName);
|
||||
return this.userRepository.save(user);
|
||||
}
|
||||
|
||||
getPhotoUrl(user: User) {
|
||||
async deleteUser(userName: string) {
|
||||
//TOOD: Handle owned notes and edits
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { userName: userName },
|
||||
});
|
||||
await this.userRepository.delete(user);
|
||||
}
|
||||
|
||||
async getUserByUsername(userName: string): Promise<User> {
|
||||
const user = this.userRepository.findOne({
|
||||
where: { userName: userName },
|
||||
});
|
||||
if (user === undefined) {
|
||||
throw new NotInDBError(`User with username '${userName}' not found`);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
getPhotoUrl(user: User): string {
|
||||
if (user.photo) {
|
||||
return user.photo;
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue