Modificacion y refactorizacion del codigo

This commit is contained in:
santiago
2025-08-27 12:44:22 -06:00
parent aa8efa74d9
commit a41b30b94a
2 changed files with 11 additions and 18 deletions
@@ -1,26 +1,15 @@
import { Controller, Post, Get, Body, HttpException, HttpStatus } from '@nestjs/common';
import { Controller, Post, Get, Body } from '@nestjs/common';
import { AlmacenamientoService } from './almacenamiento.service';
@Controller('almacenamiento')
export class AlmacenamientoController {
constructor(private readonly almacenamientoService: AlmacenamientoService) {}
// almacenamiento.controller.ts
@Post()
async create(@Body() body: { correo: string }) {
try {
const resultado = await this.almacenamientoService.create(body.correo);
return resultado; // { mensaje: 'Usuario verificado correctamente' }
} catch (error: any) {
// Aquí devolvemos un JSON con status 404 si no se encontró usuario
throw new HttpException(
{ error: error.message },
HttpStatus.NOT_FOUND
);
}
async create(@Body() body: { correo: string; agree: boolean }) {
return this.almacenamientoService.create(body.correo);
}
@Get()
async findAll() {
const data = await this.almacenamientoService.findAll();
+8 -4
View File
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Almacenamiento } from './entity/almacenamiento.entity';
@@ -15,9 +15,13 @@ export class AlmacenamientoService {
console.log(correo)
const existente = await this.almacenamientoRepository.findOne({ where:{correo} });
console.log(existente)
if (!existente) {
throw new Error("Este usuario no tiene acceso")
}
if (!existente) {
// Maneja el error aquí mismo
throw new HttpException(
{ error: 'Este usuario no tiene acceso' },
HttpStatus.NOT_FOUND
);
}
return { mensaje: 'Usuario verificado correctamente' };
}