-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
386 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IsString, IsNumber, IsPositive } from 'class-validator'; | ||
|
||
export class CreatePowerUpDto { | ||
@IsString() | ||
name: string; | ||
|
||
@IsString() | ||
description: string; | ||
|
||
@IsNumber() | ||
@IsPositive() | ||
duration: number; | ||
|
||
@IsNumber() | ||
@IsPositive() | ||
price: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsNumber, IsPositive } from 'class-validator'; | ||
|
||
export class PurchasePowerUpDto { | ||
@IsNumber() | ||
@IsPositive() | ||
powerUpId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IsString, IsNumber, IsPositive, IsOptional } from 'class-validator'; | ||
|
||
export class UpdatePowerUpDto { | ||
@IsOptional() | ||
@IsString() | ||
name?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
description?: string; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
@IsPositive() | ||
duration?: number; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
@IsPositive() | ||
price?: number; | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/power-ups/entities/power-up-purchase.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; | ||
import { PowerUp } from './power-up.entity'; | ||
import { User } from '../users/user.entity'; | ||
|
||
@Entity() | ||
export class PowerUpPurchase { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@ManyToOne(() => PowerUp) | ||
powerUp: PowerUp; | ||
|
||
@ManyToOne(() => User) | ||
user: User; | ||
|
||
@Column() | ||
purchaseDate: Date; | ||
|
||
@Column() | ||
expirationDate: Date; | ||
|
||
@Column({ default: false }) | ||
isUsed: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
@Entity() | ||
export class PowerUp { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
description: string; | ||
|
||
@Column() | ||
duration: number; | ||
|
||
@Column() | ||
price: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Injectable, type NestMiddleware } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { PowerUpService } from './power-up.service'; | ||
|
||
@Injectable() | ||
export class PowerUpValidationMiddleware implements NestMiddleware { | ||
constructor(private readonly powerUpService: PowerUpService) {} | ||
|
||
async use(req: Request, res: Response, next: NextFunction) { | ||
const user = req.user; | ||
const activePowerUps = await this.powerUpService.getActivePowerUps(user); | ||
|
||
// Add active power-ups to the request object for use in controllers | ||
req['activePowerUps'] = activePowerUps; | ||
|
||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Controller, Post, Body, Get, UseGuards } from '@nestjs/common'; | ||
import { PowerUpService } from './power-up.service'; | ||
import { CreatePowerUpDto } from './dto/create-power-up.dto'; | ||
import { PurchasePowerUpDto } from './dto/purchase-power-up.dto'; | ||
import { JwtAuthGuard } from '../auth/jwt-auth.guard'; | ||
import { User } from '../users/user.decorator'; | ||
|
||
@Controller('power-ups') | ||
export class PowerUpController { | ||
constructor(private readonly powerUpService: PowerUpService) {} | ||
|
||
@Post() | ||
@UseGuards(JwtAuthGuard) | ||
async createPowerUp(@Body() createPowerUpDto: CreatePowerUpDto) { | ||
return this.powerUpService.createPowerUp(createPowerUpDto); | ||
} | ||
|
||
@Post('purchase') | ||
@UseGuards(JwtAuthGuard) | ||
async purchasePowerUp(@User() user, @Body() purchaseDto: PurchasePowerUpDto) { | ||
return this.powerUpService.purchasePowerUp(user, purchaseDto); | ||
} | ||
|
||
@Get('active') | ||
@UseGuards(JwtAuthGuard) | ||
async getActivePowerUps(@User() user) { | ||
return this.powerUpService.getActivePowerUps(user); | ||
} | ||
|
||
@Post('use') | ||
@UseGuards(JwtAuthGuard) | ||
async usePowerUp(@User() user, @Body('powerUpId') powerUpId: number) { | ||
await this.powerUpService.usePowerUp(user, powerUpId); | ||
return { message: 'Power-up used successfully' }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { PowerUpService } from './power-up.service'; | ||
import { PowerUpController } from './power-up.controller'; | ||
import { PowerUp } from './entities/power-up.entity'; | ||
import { PowerUpPurchase } from './entities/power-up-purchase.entity'; | ||
import { PowerUpValidationMiddleware } from './power-up-validation.middleware'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([PowerUp, PowerUpPurchase])], | ||
providers: [PowerUpService, PowerUpValidationMiddleware], | ||
controllers: [PowerUpController], | ||
exports: [PowerUpService, PowerUpValidationMiddleware], | ||
}) | ||
export class PowerUpModule {} |
Oops, something went wrong.