File: /var/dev/shahnamag/back-end/src/triplet/triplet.service.ts
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateTripletDto } from './dto/create-triplet.dto';
import { UpdateTripletDto } from './dto/update-triplet.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Triplet } from './entities/triplet.entity';
import { Repository } from 'typeorm';
import { Person } from 'src/person/entities/person.entity';
import { Predicate } from 'src/predicate/entities/predicate.entity';
import { Verse } from 'src/verse/entities/verse.entity';
@Injectable()
export class TripletService {
constructor(
@InjectRepository(Predicate) private readonly predicateRepo: Repository<Predicate>,
@InjectRepository(Person) private readonly personRepo: Repository<Person>,
@InjectRepository(Verse) private readonly verseRepo: Repository<Verse>,
@InjectRepository(Triplet) private readonly tripletRepo: Repository<Triplet>,
) {}
async create(tripletDto: CreateTripletDto) {
const subject = await this.personRepo.findOneBy({id: tripletDto.subjectId});
if (!subject)
throw new NotFoundException(`subject with ID ${tripletDto.subjectId} not found`);
const predicate = await this.predicateRepo.findOneBy({id: tripletDto.predicateId});
if (!predicate)
throw new NotFoundException(`Predicate with ID ${tripletDto.predicateId} not found`);
const object = await this.personRepo.findOneBy({id: tripletDto.objectId});
if (!object)
throw new NotFoundException(`object with ID ${tripletDto.objectId} not found`);
const verse = await this.verseRepo.findOneBy({id: tripletDto.verseId});
if (!verse)
throw new NotFoundException(`verse with ID ${tripletDto.verseId} not found`);
let triplet = this.tripletRepo.create({subject, predicate, object, verse});
return this.tripletRepo.save(triplet);
}
findAll() {
return this.tripletRepo.find();
}
findOne(id: number) {
return `This action returns a #${id} triplet`;
}
update(id: number, updateTripletDto: UpdateTripletDto) {
return `This action updates a #${id} triplet`;
}
async remove(id: number) {
const result = await this.tripletRepo.delete(id);
if (result.affected === 0)
throw new NotFoundException(`Triplet with ID ${id} not found`);
}
}