From 472ed8eb70a00295397593a36420c2c760174750 Mon Sep 17 00:00:00 2001 From: MiguelMLorente Date: Sun, 23 Nov 2025 16:21:34 +0100 Subject: [PATCH] Fix controller input mappings --- src/access.controller.ts | 6 +++--- src/app.module.ts | 6 ++++-- src/buy.controller.ts | 19 ++++++++++--------- src/user.service.ts | 8 ++++++-- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/access.controller.ts b/src/access.controller.ts index 1d6ee2f..22f0fd1 100644 --- a/src/access.controller.ts +++ b/src/access.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Logger, Post, UnauthorizedException } from '@nestjs/common'; +import { Body, Controller, Logger, Post, UnauthorizedException } from '@nestjs/common'; import { UserService } from './user.service'; @Controller('/access') @@ -8,7 +8,7 @@ export class AccessController { constructor(private readonly userService: UserService) {} @Post('/login') - public login(name: string, password: string): {userId: number} { + public login(@Body("name") name: string, @Body("password") password: string): {userId: number} { this.logger.debug("Received login request"); const user = this.userService.getUserByName(name); if (user.password !== password) { @@ -20,7 +20,7 @@ export class AccessController { } @Post('/register') - public new(name: string, password: string): {userId: number} { + public new(@Body("name") name: string, @Body("password") password: string): {userId: number} { this.logger.debug("Received register request"); const user = this.userService.createUser(name, password); return { diff --git a/src/app.module.ts b/src/app.module.ts index 0866d48..224902f 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,12 @@ import { Module } from '@nestjs/common'; import { AccessController } from './access.controller'; import { UserService } from './user.service'; +import { BuyController } from './buy.controller'; +import { PurchaseService } from './purchase.service'; @Module({ imports: [], - controllers: [AccessController], - providers: [UserService], + controllers: [AccessController, BuyController], + providers: [UserService, PurchaseService], }) export class AppModule {} diff --git a/src/buy.controller.ts b/src/buy.controller.ts index 2e75bb6..e6aebe7 100644 --- a/src/buy.controller.ts +++ b/src/buy.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Redirect } from "@nestjs/common"; +import { Body, Controller, Get, Post, Redirect } from "@nestjs/common"; import Stripe from 'stripe' import { PurchaseService } from "./purchase.service"; import { UserService } from "./user.service"; @@ -11,11 +11,11 @@ export class BuyController { constructor(private readonly purchaseService: PurchaseService, private readonly userService: UserService) {} - @Post("start") + @Post("/start") @Redirect() - public async getStripeSessionUrl(userId: number) { + public async getStripeSessionUrl(@Body("userId") userId: number, @Body("quantity") quantity: number) { const user = this.userService.getUserById(userId) - const purchase = this.purchaseService.recordPurchaseStart(user, PurchaseItem.PAELLA, 1) + const purchase = this.purchaseService.recordPurchaseStart(user, PurchaseItem.PAELLA, quantity) const session = await this.stripe.checkout.sessions.create({ line_items: [ { @@ -24,20 +24,21 @@ export class BuyController { } ], mode: "payment", - success_url: "https://localhost:3000/buy/complete", - cancel_url: "https://localhost:3000/buy/cancel", - + success_url: "https://localhost:5173/buy/complete", + cancel_url: "https://localhost:5173/buy/cancel", + payment_method_types: ['card'], }) + console.log(session) return {url: session.url} } - @Get("complete") + @Get("/complete") public complete() { } - @Get("cancel") + @Get("/cancel") public cancel() { } diff --git a/src/user.service.ts b/src/user.service.ts index 1920663..8c070a5 100644 --- a/src/user.service.ts +++ b/src/user.service.ts @@ -1,13 +1,15 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { Injectable, Logger, NotFoundException } from '@nestjs/common'; import { User } from './dto/user'; @Injectable() export class UserService { private readonly users: User[] = []; + private readonly logger = new Logger(UserService.name) constructor() {} public getUserByName(name: string): User { + this.logger.debug(`Get user by name: ${name}`) const user = this.users.find((u) => u.name === name); if (!user) { throw new NotFoundException(); @@ -16,6 +18,7 @@ export class UserService { } public getUserById(userId: number): User { + this.logger.debug(`Get user by id: ${userId}`) const user = this.users.find((u) => u.id === userId); if (!user) { throw new NotFoundException(); @@ -30,7 +33,8 @@ export class UserService { } catch (e) { if (!(e instanceof NotFoundException)) throw e; } - + + this.logger.debug(`Creating user with name: ${name}`) const user: User = { id: this.users.length, name,