Fix controller input mappings

main
MiguelMLorente 2025-11-23 16:21:34 +01:00
parent c97e1b01a9
commit 472ed8eb70
4 changed files with 23 additions and 16 deletions

View File

@ -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'; import { UserService } from './user.service';
@Controller('/access') @Controller('/access')
@ -8,7 +8,7 @@ export class AccessController {
constructor(private readonly userService: UserService) {} constructor(private readonly userService: UserService) {}
@Post('/login') @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"); this.logger.debug("Received login request");
const user = this.userService.getUserByName(name); const user = this.userService.getUserByName(name);
if (user.password !== password) { if (user.password !== password) {
@ -20,7 +20,7 @@ export class AccessController {
} }
@Post('/register') @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"); this.logger.debug("Received register request");
const user = this.userService.createUser(name, password); const user = this.userService.createUser(name, password);
return { return {

View File

@ -1,10 +1,12 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AccessController } from './access.controller'; import { AccessController } from './access.controller';
import { UserService } from './user.service'; import { UserService } from './user.service';
import { BuyController } from './buy.controller';
import { PurchaseService } from './purchase.service';
@Module({ @Module({
imports: [], imports: [],
controllers: [AccessController], controllers: [AccessController, BuyController],
providers: [UserService], providers: [UserService, PurchaseService],
}) })
export class AppModule {} export class AppModule {}

View File

@ -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 Stripe from 'stripe'
import { PurchaseService } from "./purchase.service"; import { PurchaseService } from "./purchase.service";
import { UserService } from "./user.service"; import { UserService } from "./user.service";
@ -11,11 +11,11 @@ export class BuyController {
constructor(private readonly purchaseService: PurchaseService, private readonly userService: UserService) {} constructor(private readonly purchaseService: PurchaseService, private readonly userService: UserService) {}
@Post("start") @Post("/start")
@Redirect() @Redirect()
public async getStripeSessionUrl(userId: number) { public async getStripeSessionUrl(@Body("userId") userId: number, @Body("quantity") quantity: number) {
const user = this.userService.getUserById(userId) 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({ const session = await this.stripe.checkout.sessions.create({
line_items: [ line_items: [
{ {
@ -24,20 +24,21 @@ export class BuyController {
} }
], ],
mode: "payment", mode: "payment",
success_url: "https://localhost:3000/buy/complete", success_url: "https://localhost:5173/buy/complete",
cancel_url: "https://localhost:3000/buy/cancel", cancel_url: "https://localhost:5173/buy/cancel",
payment_method_types: ['card'],
}) })
console.log(session)
return {url: session.url} return {url: session.url}
} }
@Get("complete") @Get("/complete")
public complete() { public complete() {
} }
@Get("cancel") @Get("/cancel")
public cancel() { public cancel() {
} }

View File

@ -1,13 +1,15 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { User } from './dto/user'; import { User } from './dto/user';
@Injectable() @Injectable()
export class UserService { export class UserService {
private readonly users: User[] = []; private readonly users: User[] = [];
private readonly logger = new Logger(UserService.name)
constructor() {} constructor() {}
public getUserByName(name: string): User { public getUserByName(name: string): User {
this.logger.debug(`Get user by name: ${name}`)
const user = this.users.find((u) => u.name === name); const user = this.users.find((u) => u.name === name);
if (!user) { if (!user) {
throw new NotFoundException(); throw new NotFoundException();
@ -16,6 +18,7 @@ export class UserService {
} }
public getUserById(userId: number): User { public getUserById(userId: number): User {
this.logger.debug(`Get user by id: ${userId}`)
const user = this.users.find((u) => u.id === userId); const user = this.users.find((u) => u.id === userId);
if (!user) { if (!user) {
throw new NotFoundException(); throw new NotFoundException();
@ -30,7 +33,8 @@ export class UserService {
} catch (e) { } catch (e) {
if (!(e instanceof NotFoundException)) throw e; if (!(e instanceof NotFoundException)) throw e;
} }
this.logger.debug(`Creating user with name: ${name}`)
const user: User = { const user: User = {
id: this.users.length, id: this.users.length,
name, name,