Archived
41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { ConflictException, Injectable } from '@nestjs/common';
|
|
import { CreateSolutionDto } from './dto/create-solution.dto';
|
|
import { IssuesService } from 'src/issues/issues.service';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Solution } from './entities/solution.entity';
|
|
import { Repository } from 'typeorm';
|
|
|
|
@Injectable()
|
|
export class SolutionsService {
|
|
constructor(
|
|
@InjectRepository(Solution)
|
|
private readonly repository: Repository<Solution>,
|
|
private readonly issues: IssuesService,
|
|
) {}
|
|
|
|
async create(issueId: string, createSolutionDto: CreateSolutionDto) {
|
|
if ((await this.getForIssue(issueId)) !== null)
|
|
throw new ConflictException('This issue already has a solution');
|
|
const solution = this.repository.create({ ...createSolutionDto });
|
|
const issue = await this.issues.findOne(issueId);
|
|
solution.issue = Promise.resolve(issue);
|
|
return this.repository.save(solution);
|
|
}
|
|
|
|
async getForIssue(issueId: string): Promise<Solution | null> {
|
|
return this.repository.findOneBy({ issue: { id: issueId } });
|
|
}
|
|
|
|
async like(issueId: string, remove?: boolean) {
|
|
const ent = await this.getForIssue(issueId);
|
|
ent.likes = remove ? ent.likes - 1 : ent.likes + 1;
|
|
return this.repository.save(ent);
|
|
}
|
|
|
|
async dislike(issueId: string, remove?: boolean) {
|
|
const ent = await this.getForIssue(issueId);
|
|
ent.dislikes = remove ? ent.dislikes - 1 : ent.dislikes + 1;
|
|
return this.repository.save(ent);
|
|
}
|
|
}
|