mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 17:56:30 -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
1f22f47327
commit
219a3bcb5f
3 changed files with 49 additions and 13 deletions
|
@ -29,8 +29,10 @@ export class MeController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getMe(): UserInfoDto {
|
async getMe(): Promise<UserInfoDto> {
|
||||||
return this.usersService.getUserInfo();
|
return this.usersService.toUserDto(
|
||||||
|
await this.usersService.getUserByUsername('hardcoded'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('history')
|
@Get('history')
|
||||||
|
|
|
@ -48,4 +48,20 @@ export class User {
|
||||||
identity => identity.user,
|
identity => identity.user,
|
||||||
)
|
)
|
||||||
identities: Identity[];
|
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 { 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 { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||||
import { UserInfoDto } from './user-info.dto';
|
import { UserInfoDto } from './user-info.dto';
|
||||||
import { User } from './user.entity';
|
import { User } from './user.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
constructor(private readonly logger: ConsoleLoggerService) {
|
constructor(
|
||||||
|
private readonly logger: ConsoleLoggerService,
|
||||||
|
@InjectRepository(User) private userRepository: Repository<User>,
|
||||||
|
) {
|
||||||
this.logger.setContext(UsersService.name);
|
this.logger.setContext(UsersService.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
getUserInfo(): UserInfoDto {
|
createUser(userName: string, displayName: string): Promise<User> {
|
||||||
//TODO: Use the database
|
const user = User.create(userName, displayName);
|
||||||
this.logger.warn('Using hardcoded data!');
|
return this.userRepository.save(user);
|
||||||
return {
|
|
||||||
displayName: 'foo',
|
|
||||||
userName: 'fooUser',
|
|
||||||
email: 'foo@example.com',
|
|
||||||
photo: '',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
if (user.photo) {
|
||||||
return user.photo;
|
return user.photo;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue