mirror of https://github.com/ghostfolio/ghostfolio
9 changed files with 195 additions and 16 deletions
@ -0,0 +1,13 @@ |
|||||
|
import { IsOptional, IsString } from 'class-validator'; |
||||
|
|
||||
|
export class CreatePlatformDto { |
||||
|
@IsOptional() |
||||
|
@IsString() |
||||
|
id?: string; |
||||
|
|
||||
|
@IsString() |
||||
|
name: string; |
||||
|
|
||||
|
@IsString() |
||||
|
url: string; |
||||
|
} |
@ -0,0 +1,113 @@ |
|||||
|
import { |
||||
|
Body, |
||||
|
Controller, |
||||
|
Delete, |
||||
|
Get, |
||||
|
HttpException, |
||||
|
Inject, |
||||
|
Param, |
||||
|
Post, |
||||
|
Put, |
||||
|
UseGuards |
||||
|
} from '@nestjs/common'; |
||||
|
import { PlatformService } from './platform.service'; |
||||
|
import { AuthGuard } from '@nestjs/passport'; |
||||
|
import { Platform } from '@prisma/client'; |
||||
|
import { CreatePlatformDto } from './create-platform.dto'; |
||||
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; |
||||
|
import { RequestWithUser } from '@ghostfolio/common/types'; |
||||
|
import { REQUEST } from '@nestjs/core'; |
||||
|
import { getReasonPhrase, StatusCodes } from 'http-status-codes'; |
||||
|
import { UpdatePlatformDto } from './update-platform.dto'; |
||||
|
|
||||
|
@Controller('platform') |
||||
|
export class PlatformController { |
||||
|
public constructor( |
||||
|
private platformService: PlatformService, |
||||
|
@Inject(REQUEST) private readonly request: RequestWithUser |
||||
|
) {} |
||||
|
|
||||
|
@Get() |
||||
|
@UseGuards(AuthGuard('jwt')) |
||||
|
public async getPlatforms(): Promise<Platform[]> { |
||||
|
return this.platformService.getPlatforms(); |
||||
|
} |
||||
|
|
||||
|
@Post() |
||||
|
@UseGuards(AuthGuard('jwt')) |
||||
|
public async createPlatform( |
||||
|
@Body() data: CreatePlatformDto |
||||
|
): Promise<Platform> { |
||||
|
if ( |
||||
|
!hasPermission(this.request.user.permissions, permissions.createPlatform) |
||||
|
) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
return this.platformService.createPlatform(data); |
||||
|
} |
||||
|
|
||||
|
@Put(':id') |
||||
|
@UseGuards(AuthGuard('jwt')) |
||||
|
public async updatePlatform( |
||||
|
@Param(':id') id: string, |
||||
|
@Body() data: UpdatePlatformDto |
||||
|
) { |
||||
|
if ( |
||||
|
!hasPermission(this.request.user.permissions, permissions.updatePlatform) |
||||
|
) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
const originalPlatform = await this.platformService.getPlatform({ |
||||
|
id |
||||
|
}); |
||||
|
|
||||
|
if (!originalPlatform) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
return this.platformService.updatePlatform({ |
||||
|
data: { |
||||
|
...data |
||||
|
}, |
||||
|
where: { |
||||
|
id |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Delete(':id') |
||||
|
@UseGuards(AuthGuard('jwt')) |
||||
|
public async deletePlatform(@Param(':id') id: string) { |
||||
|
if ( |
||||
|
!hasPermission(this.request.user.permissions, permissions.deletePlatform) |
||||
|
) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
const originalPlatform = await this.platformService.getPlatform({ |
||||
|
id |
||||
|
}); |
||||
|
|
||||
|
if (!originalPlatform) { |
||||
|
throw new HttpException( |
||||
|
getReasonPhrase(StatusCodes.FORBIDDEN), |
||||
|
StatusCodes.FORBIDDEN |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
return this.platformService.deletePlatform({ id }); |
||||
|
} |
||||
|
} |
@ -1,9 +1,11 @@ |
|||||
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; |
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; |
||||
import { Module } from '@nestjs/common'; |
import { Module } from '@nestjs/common'; |
||||
|
|
||||
|
import { PlatformController } from './platform.controller'; |
||||
import { PlatformService } from './platform.service'; |
import { PlatformService } from './platform.service'; |
||||
|
|
||||
@Module({ |
@Module({ |
||||
|
controllers: [PlatformController], |
||||
exports: [PlatformService], |
exports: [PlatformService], |
||||
imports: [PrismaModule], |
imports: [PrismaModule], |
||||
providers: [PlatformService] |
providers: [PlatformService] |
@ -0,0 +1,43 @@ |
|||||
|
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; |
||||
|
import { Injectable } from '@nestjs/common'; |
||||
|
import { Platform, Prisma } from '@prisma/client'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class PlatformService { |
||||
|
public constructor(private readonly prismaService: PrismaService) {} |
||||
|
|
||||
|
public async getPlatforms(): Promise<Platform[]> { |
||||
|
return this.prismaService.platform.findMany(); |
||||
|
} |
||||
|
|
||||
|
public async getPlatform( |
||||
|
platformWhereUniqueInput: Prisma.PlatformWhereUniqueInput |
||||
|
): Promise<Platform> { |
||||
|
return this.prismaService.platform.findUnique({ |
||||
|
where: platformWhereUniqueInput |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public async createPlatform(data: Prisma.PlatformCreateInput) { |
||||
|
return this.prismaService.platform.create({ |
||||
|
data |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public async updatePlatform(params: { |
||||
|
where: Prisma.PlatformWhereUniqueInput; |
||||
|
data: Prisma.PlatformUpdateInput; |
||||
|
}): Promise<Platform> { |
||||
|
const { data, where } = params; |
||||
|
return this.prismaService.platform.update({ |
||||
|
data, |
||||
|
where |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public async deletePlatform( |
||||
|
where: Prisma.PlatformWhereUniqueInput |
||||
|
): Promise<Platform> { |
||||
|
return this.prismaService.platform.delete({ where }); |
||||
|
} |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
import { IsOptional, IsString } from 'class-validator'; |
||||
|
|
||||
|
export class UpdatePlatformDto { |
||||
|
@IsOptional() |
||||
|
@IsString() |
||||
|
id?: string; |
||||
|
|
||||
|
@IsString() |
||||
|
name: string; |
||||
|
|
||||
|
@IsString() |
||||
|
url: string; |
||||
|
} |
@ -1,11 +0,0 @@ |
|||||
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; |
|
||||
import { Injectable } from '@nestjs/common'; |
|
||||
|
|
||||
@Injectable() |
|
||||
export class PlatformService { |
|
||||
public constructor(private readonly prismaService: PrismaService) {} |
|
||||
|
|
||||
public async get() { |
|
||||
return this.prismaService.platform.findMany(); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue