66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Comentarios } from './entity/comentarios.entity';
|
|
import { Repository } from 'typeorm';
|
|
import { ComentarioDto } from './comentario.dto';
|
|
|
|
@Injectable()
|
|
export class ComentariosService {
|
|
constructor(
|
|
@InjectRepository(Comentarios)
|
|
private comentariosRepository: Repository<Comentarios>,
|
|
) {}
|
|
|
|
postComentario(comentario: ComentarioDto) {
|
|
if (comentario.from !== 'CEDETEC' && comentario.from !== 'PCPUMA') {
|
|
throw new BadRequestException('Mensaje no autorizado');
|
|
}
|
|
|
|
const nuevoComentario = this.comentariosRepository.create(comentario);
|
|
return this.comentariosRepository.save(nuevoComentario);
|
|
}
|
|
|
|
getComentarios() {
|
|
return this.comentariosRepository.find();
|
|
}
|
|
|
|
async getComentariosByDate(
|
|
before?: string,
|
|
after?: string,
|
|
from?: string,
|
|
to?: string,
|
|
) {
|
|
const query = this.comentariosRepository.createQueryBuilder('comentarios');
|
|
|
|
if (before) {
|
|
query.andWhere('comentarios.fecha_registro < :before', {
|
|
before: new Date(before),
|
|
});
|
|
}
|
|
|
|
if (after) {
|
|
query.andWhere('comentarios.fecha_registro > :after', {
|
|
after: new Date(after),
|
|
});
|
|
}
|
|
|
|
if (from) {
|
|
query.andWhere('comentarios.fecha_registro >= :from', {
|
|
from: new Date(from),
|
|
});
|
|
}
|
|
|
|
if (to) {
|
|
query.andWhere('comentarios.fecha_registro <= :to', { to: new Date(to) });
|
|
}
|
|
|
|
query.orderBy('comentarios.fecha_registro', 'DESC');
|
|
|
|
return await query.getMany();
|
|
}
|
|
|
|
deleteComentario(id: number) {
|
|
return this.comentariosRepository.delete({ id_comentario: id });
|
|
}
|
|
}
|