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';
@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 {

View File

@ -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 {}

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 { 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() {
}

View File

@ -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();
@ -31,6 +34,7 @@ export class UserService {
if (!(e instanceof NotFoundException)) throw e;
}
this.logger.debug(`Creating user with name: ${name}`)
const user: User = {
id: this.users.length,
name,