Modificacion y refactorizacion del codigo
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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' };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user