File

src/module/short-test/short-test.service.ts

Index

Methods

Constructor

constructor(repository: EntityRepository<ShortTest>, em: EntityManager)
Parameters :
Name Type Optional
repository EntityRepository<ShortTest> No
em EntityManager No

Methods

Async create
create(dto: ShortTestDTO)
Parameters :
Name Type Optional
dto ShortTestDTO No
Async delete
delete(id: string)
Parameters :
Name Type Optional
id string No
Returns : Promise<void>
Async findAll
findAll()
Async findById
findById(id: string)
Parameters :
Name Type Optional
id string No
Returns : Promise<ShortTest>
Async findMany
findMany(ids: string[])

Retrieves multiple ShortTest entities by their IDs.

Parameters :
Name Type Optional Description
ids string[] No
  • Array of ShortTest IDs to retrieve.

A list of ShortTest entities.

Async update
update(id: string, dto: ShortTestDTO)
Parameters :
Name Type Optional
id string No
dto ShortTestDTO No
import { EntityRepository } from '@mikro-orm/core';
import { EntityManager } from '@mikro-orm/mysql';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { IShortTest } from 'shared/model/ShortTest';
import { ShortTest } from '../../database/entities/shorttest.entity';
import { CRUDService } from '../../helpers/CRUDService';
import { ShortTestDTO } from '../scheinexam/scheinexam.dto';

@Injectable()
export class ShortTestService implements CRUDService<IShortTest, ShortTestDTO, ShortTest> {
    constructor(
        @InjectRepository(ShortTest)
        private readonly repository: EntityRepository<ShortTest>,
        @Inject(EntityManager)
        private readonly em: EntityManager
    ) {}

    async findAll(): Promise<ShortTest[]> {
        return this.repository.findAll();
    }

    async findById(id: string): Promise<ShortTest> {
        const shortTest = await this.repository.findOne({ id });

        if (!shortTest) {
            throw new NotFoundException(
                `Short test document with the ID "${id}" could not be found.`
            );
        }

        return shortTest;
    }

    /**
     * Retrieves multiple `ShortTest` entities by their IDs.
     *
     * @param ids - Array of ShortTest IDs to retrieve.
     * @returns A list of `ShortTest` entities.
     * @throws `NotFoundException` - If any of the requested ShortTests are not found.
     */
    async findMany(ids: string[]): Promise<ShortTest[]> {
        const shortTests = await this.repository.find({ id: { $in: ids } });

        if (shortTests.length !== ids.length) {
            const foundIds = new Set(shortTests.map((st) => st.id));
            const missingIds = ids.filter((id) => !foundIds.has(id));
            throw new NotFoundException(
                `The following Short Test IDs could not be found: [${missingIds.join(', ')}]`
            );
        }

        return shortTests;
    }

    async create(dto: ShortTestDTO): Promise<IShortTest> {
        const shortTest = ShortTest.fromDTO(dto);
        await this.em.persistAndFlush(shortTest);
        return shortTest.toDTO();
    }

    async update(id: string, dto: ShortTestDTO): Promise<IShortTest> {
        const shortTest = await this.findById(id);
        shortTest.updateFromDTO(dto);
        await this.em.persistAndFlush(shortTest);
        return shortTest.toDTO();
    }

    async delete(id: string): Promise<void> {
        const shortTest = await this.findById(id);
        await this.em.removeAndFlush(shortTest);
    }
}

results matching ""

    No results matching ""