mirror of
https://github.com/Kevsl/crypto-exchange-api.git
synced 2026-02-08 19:16:13 +01:00
testing all features
This commit is contained in:
1
src/offer/dto/index.ts
Normal file
1
src/offer/dto/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './offer.dto';
|
||||
20
src/offer/dto/offer.dto.ts
Normal file
20
src/offer/dto/offer.dto.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNumber, IsString } from 'class-validator';
|
||||
export class OfferDto {
|
||||
@ApiProperty({
|
||||
type: String,
|
||||
description: 'Cryptocurrency UUID',
|
||||
example: '12121-DSZD-E221212-2121221',
|
||||
})
|
||||
@IsString()
|
||||
id_crypto: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'number',
|
||||
|
||||
description: 'Amount traded ',
|
||||
example: 21,
|
||||
})
|
||||
@IsNumber()
|
||||
amount: number;
|
||||
}
|
||||
59
src/offer/offer.controller.ts
Normal file
59
src/offer/offer.controller.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/* eslint-disable prettier/prettier */
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
// UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { GetUser } from '../auth/decorator';
|
||||
// import { JwtGuard } from '../auth/guard';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { User } from '@prisma/client';
|
||||
import { JwtGuard } from 'src/auth/guard';
|
||||
import { OfferService } from './offer.service';
|
||||
import { OfferDto } from './dto';
|
||||
|
||||
@UseGuards(JwtGuard)
|
||||
@ApiTags('offer')
|
||||
@Controller('offer')
|
||||
export class OfferController {
|
||||
constructor(private offerService: OfferService) {}
|
||||
|
||||
@Get('/all')
|
||||
getAllRoles(@GetUser() user: User) {
|
||||
return this.offerService.getOffers(user.id);
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@Post('/create')
|
||||
createRole(
|
||||
@Body()
|
||||
dto: OfferDto,
|
||||
@GetUser() user: User
|
||||
) {
|
||||
return this.offerService.createOffer(user.id, dto);
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Patch('/update/:id')
|
||||
editOfferById(
|
||||
@Param('id') offerId: string,
|
||||
@Body() dto: OfferDto,
|
||||
@GetUser() user: User
|
||||
) {
|
||||
return this.offerService.editOfferById(user.id, offerId, dto);
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Delete('/delete/:id')
|
||||
deleteOfferById(@Param('id') roleId: string, @GetUser() user: User) {
|
||||
return this.offerService.deleteOfferById(user.id, roleId);
|
||||
}
|
||||
}
|
||||
9
src/offer/offer.module.ts
Normal file
9
src/offer/offer.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OfferService } from './offer.service';
|
||||
import { OfferController } from './offer.controller';
|
||||
|
||||
@Module({
|
||||
providers: [OfferService],
|
||||
controllers: [OfferController],
|
||||
})
|
||||
export class OfferModule {}
|
||||
76
src/offer/offer.service.ts
Normal file
76
src/offer/offer.service.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { checkUserHasAccount, checkuserIsAdmin } from 'src/utils/checkUser';
|
||||
import { OfferDto } from './dto';
|
||||
// import { checkRoleLevel, checkUserIsStaff } from 'src/utils/checkUser';
|
||||
@Injectable()
|
||||
export class OfferService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async getOffers(userId: string) {
|
||||
await checkUserHasAccount(userId);
|
||||
return this.prisma.offer.findMany({
|
||||
orderBy: {
|
||||
created_at: 'desc',
|
||||
},
|
||||
select: {
|
||||
amount: true,
|
||||
created_at: true,
|
||||
id_user: true,
|
||||
Crypto: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async createOffer(userId: string, dto: OfferDto) {
|
||||
await checkUserHasAccount(userId);
|
||||
const offer = await this.prisma.offer.create({
|
||||
data: {
|
||||
id_crypto: dto.id_crypto,
|
||||
id_user: userId,
|
||||
amount: dto.amount,
|
||||
},
|
||||
});
|
||||
|
||||
return offer;
|
||||
}
|
||||
async editOfferById(userId: string, offerId: string, dto: OfferDto) {
|
||||
await checkUserHasAccount(userId);
|
||||
|
||||
const offer = await this.prisma.offer.findUnique({
|
||||
where: {
|
||||
id: offerId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!offer || offer.id !== offerId)
|
||||
throw new ForbiddenException('Access to resources denied');
|
||||
|
||||
return this.prisma.role.update({
|
||||
where: {
|
||||
id: offerId,
|
||||
},
|
||||
data: {
|
||||
...dto,
|
||||
},
|
||||
});
|
||||
}
|
||||
async deleteOfferById(userId: string, id: string) {
|
||||
await checkuserIsAdmin(userId);
|
||||
|
||||
const offer = await this.prisma.offer.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!offer || offer.id !== id)
|
||||
throw new ForbiddenException('Access to resources denied');
|
||||
|
||||
await this.prisma.offer.delete({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user