add smart random

This commit is contained in:
2023-07-14 14:54:47 +02:00
parent 68942e904f
commit b14fb39009
25 changed files with 509 additions and 104 deletions

View File

@@ -2,6 +2,7 @@ import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common
import { PrismaService } from '../../../prisma.service'
import { GetAllDecksDto } from '../dto/get-all-decks.dto'
import { Pagination } from '../../../infrastructure/common/pagination/pagination.service'
import { createPrismaOrderBy } from '../../../infrastructure/common/helpers/get-order-by-object'
@Injectable()
export class DecksRepository {
@@ -48,13 +49,15 @@ export class DecksRepository {
itemsPerPage,
minCardsCount,
maxCardsCount,
orderBy,
}: GetAllDecksDto) {
console.log({ name, authorId, userId, currentPage, itemsPerPage, minCardsCount, maxCardsCount })
console.log(minCardsCount)
console.log(Number(minCardsCount))
try {
const where = {
cardsCount: {
gte: Number(minCardsCount) ?? undefined,
lte: Number(maxCardsCount) ?? undefined,
gte: minCardsCount ? Number(minCardsCount) : undefined,
lte: maxCardsCount ? Number(maxCardsCount) : undefined,
},
name: {
contains: name,
@@ -85,9 +88,7 @@ export class DecksRepository {
}),
this.prisma.deck.findMany({
where,
orderBy: {
created: 'desc',
},
orderBy: createPrismaOrderBy(orderBy),
include: {
author: {
select: {
@@ -124,6 +125,24 @@ export class DecksRepository {
throw new InternalServerErrorException(e?.message)
}
}
public async findDeckByCardId(cardId: string) {
try {
const card = await this.prisma.card.findUnique({
where: {
id: cardId,
},
})
return await this.prisma.deck.findUnique({
where: {
id: card.deckId,
},
})
} catch (e) {
this.logger.error(e?.message)
throw new InternalServerErrorException(e?.message)
}
}
public async deleteDeckById(id: string) {
try {

View File

@@ -0,0 +1,59 @@
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
import { PrismaService } from '../../../prisma.service'
@Injectable()
export class GradesRepository {
constructor(private prisma: PrismaService) {}
private readonly logger = new Logger(GradesRepository.name)
async createGrade({
cardId,
userId,
deckId,
grade,
}: {
cardId: string
userId: string
deckId: string
grade: number
}) {
try {
return await this.prisma.grade.upsert({
where: {
userId,
cardId,
deckId,
},
update: {
grade,
shots: {
increment: 1,
},
},
create: {
grade,
shots: 1,
user: {
connect: {
id: userId,
},
},
card: {
connect: {
id: cardId,
},
},
deck: {
connect: {
id: deckId,
},
},
},
})
} catch (e) {
this.logger.error(e?.message)
throw new InternalServerErrorException(e?.message)
}
}
}