Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

assessment service :

import { Injectable } from '@nestjs/common';


import { InjectRepository } from '@nestjs/typeorm';
import { ResponsesService } from 'src/responses/responses.service';
import { Repository, In } from 'typeorm';
import Assessment from './assessment.entity';

@Injectable()
export class AssessmentsService {
constructor(
@InjectRepository(Assessment)
private assessmentsRepository: Repository<Assessment>,
private readonly responsesService: ResponsesService
) {}

async create(assessment: Assessment) {

const newAssessment = this.assessmentsRepository.create(assessment);


const createdAssessment = await this.assessmentsRepository.save(newAssessment);

for(let i=0; i<newAssessment.responses.length; i++) {

const newResponse = await


this.responsesService.create(newAssessment.responses[i])
await this.responsesService.updateAssessmentId(newResponse.id,
createdAssessment)
}

return createdAssessment;
}
}

update assessment :

import { Injectable } from '@nestjs/common';


import { InjectRepository } from '@nestjs/typeorm';
import Assessment from 'src/assessments/assessment.entity';
import { Repository, In } from 'typeorm';
import Response from './response.entity';

@Injectable()
export class ResponsesService {
constructor(
@InjectRepository(Response)
private responsesRepository: Repository<Response>,
) {}

async create(response: Response) {

const newResponse = await this.responsesRepository.create(response);


await this.responsesRepository.save(newResponse);
return newResponse;
}

async updateAssessmentId(responseId: number, assessment: Assessment) {


await this.responsesRepository.update(responseId, {
assessment,
});
}
}

You might also like