Merge pull request 'qr genereta' (#9) from mike into develop
Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
+43
-22
@@ -1,36 +1,57 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { QrService } from './qr.service';
|
||||
import { Qr } from './qr.entity';
|
||||
import { CreateQrDto } from './dto/create-qr.dto';
|
||||
import { UpdateQrDto } from './dto/update.qr.dto';
|
||||
import { ApiOperation, ApiQuery } from '@nestjs/swagger';
|
||||
|
||||
@Controller('qr')
|
||||
export class QrController {
|
||||
constructor(private qrService: QrService) {}
|
||||
|
||||
constructor(private qrService: QrService) {}
|
||||
@Get('generate')
|
||||
@ApiOperation({ summary: 'Genera un código QR a partir de un texto' })
|
||||
@ApiQuery({
|
||||
name: 'text',
|
||||
required: true,
|
||||
description: 'Texto a codificar en el QR',
|
||||
})
|
||||
async generateQRCode(@Query('text') text: string): Promise<string> {
|
||||
return this.qrService.generateQRCode(text);
|
||||
}
|
||||
|
||||
@Get()
|
||||
getQrs(): Promise<Qr[]> {
|
||||
return this.qrService.getQrs();
|
||||
}
|
||||
@Get()
|
||||
getQrs(): Promise<Qr[]> {
|
||||
return this.qrService.getQrs();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getQr(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.qrService.getQr(id);
|
||||
}
|
||||
@Get(':id')
|
||||
getQr(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.qrService.getQr(id);
|
||||
}
|
||||
|
||||
@Post() //en el body ValidationPipe
|
||||
createQr(@Body() newQr: CreateQrDto) {
|
||||
return this.qrService.createQr(newQr)
|
||||
}
|
||||
@Post() //en el body ValidationPipe
|
||||
createQr(@Body() newQr: CreateQrDto) {
|
||||
return this.qrService.createQr(newQr);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
deleteQr(@Param('id', ParseIntPipe) id:number) {
|
||||
return this.qrService.deleteQr(id);
|
||||
}
|
||||
@Delete(':id')
|
||||
deleteQr(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.qrService.deleteQr(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
updateQr(@Param('id', ParseIntPipe) id: number, @Body() qr: UpdateQrDto) {
|
||||
return this.qrService.updateQr(id, qr)
|
||||
}
|
||||
@Patch(':id')
|
||||
updateQr(@Param('id', ParseIntPipe) id: number, @Body() qr: UpdateQrDto) {
|
||||
return this.qrService.updateQr(id, qr);
|
||||
}
|
||||
}
|
||||
|
||||
+61
-44
@@ -4,70 +4,87 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateQrDto } from './dto/create-qr.dto';
|
||||
import { UpdateQrDto } from './dto/update.qr.dto';
|
||||
import * as QRCode from 'qrcode';
|
||||
|
||||
@Injectable()
|
||||
export class QrService {
|
||||
constructor(@InjectRepository(Qr) private qrRepository: Repository<Qr>) {}
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Qr) private qrRepository: Repository<Qr>
|
||||
) {}
|
||||
async generateQRCode(text: string): Promise<string> {
|
||||
return await QRCode.toDataURL(text);
|
||||
}
|
||||
|
||||
async createQr(qr: CreateQrDto) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr: qr.id_participante_evento
|
||||
}
|
||||
})
|
||||
async generateBuffer(text: string): Promise<Buffer> {
|
||||
return await QRCode.toBuffer(text); // Devuelve un buffer para adjuntar en email o stream
|
||||
}
|
||||
|
||||
if (qrFound) {
|
||||
return new HttpException('Qr already exists', HttpStatus.CONFLICT)
|
||||
}
|
||||
// Para enviar el QR por email
|
||||
/* const qrBuffer = await this.qrService.generateBuffer('Texto a codificar');
|
||||
await transporter.sendMail({
|
||||
to: 'destinatario@example.com',
|
||||
subject: 'Tu código QR',
|
||||
html: '<p>Escanea el siguiente código:</p><img src="cid:qrcode"/>',
|
||||
attachments: [{
|
||||
filename: 'qrcode.png',
|
||||
content: qrBuffer,
|
||||
cid: 'qrcode',
|
||||
}],
|
||||
}); */
|
||||
|
||||
return this.qrRepository.save(qr)
|
||||
async createQr(qr: CreateQrDto) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr: qr.id_participante_evento,
|
||||
},
|
||||
});
|
||||
|
||||
if (qrFound) {
|
||||
return new HttpException('Qr already exists', HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
getQrs() {
|
||||
return this.qrRepository.find({})
|
||||
}
|
||||
return this.qrRepository.save(qr);
|
||||
}
|
||||
|
||||
async getQr(id_qr) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr
|
||||
}
|
||||
})
|
||||
getQrs() {
|
||||
return this.qrRepository.find({});
|
||||
}
|
||||
|
||||
if (!qrFound) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
async getQr(id_qr) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr,
|
||||
},
|
||||
});
|
||||
|
||||
return qrFound
|
||||
if (!qrFound) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
async deleteQr(id_qr: number) {
|
||||
const result = await this.qrRepository.delete({ id_qr })
|
||||
return qrFound;
|
||||
}
|
||||
|
||||
if (result.affected === 0) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
async deleteQr(id_qr: number) {
|
||||
const result = await this.qrRepository.delete({ id_qr });
|
||||
|
||||
return result
|
||||
if (result.affected === 0) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
async updateQr(id_qr: number, qr: UpdateQrDto) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr
|
||||
}
|
||||
})
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!qrFound) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND)
|
||||
}
|
||||
async updateQr(id_qr: number, qr: UpdateQrDto) {
|
||||
const qrFound = await this.qrRepository.findOne({
|
||||
where: {
|
||||
id_qr,
|
||||
},
|
||||
});
|
||||
|
||||
const updateQr = Object.assign(qrFound, qr)
|
||||
return this.qrRepository.save(updateQr)
|
||||
if (!qrFound) {
|
||||
return new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
|
||||
const updateQr = Object.assign(qrFound, qr);
|
||||
return this.qrRepository.save(updateQr);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user