mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-25 12:33:48 +00:00
move to storage service
This commit is contained in:
@@ -25,10 +25,7 @@ export class UpdateUserHandler implements ICommandHandler<UpdateUserCommand> {
|
||||
let avatar: string | null
|
||||
|
||||
if (command.avatar) {
|
||||
const addAvatarImagePromise = this.fileUploadService.uploadFile(
|
||||
command.avatar?.buffer,
|
||||
command.avatar?.originalname
|
||||
)
|
||||
const addAvatarImagePromise = this.fileUploadService.uploadFile(command.avatar)
|
||||
|
||||
const result = await addAvatarImagePromise
|
||||
|
||||
|
||||
@@ -35,32 +35,20 @@ export class UpdateCardHandler implements ICommandHandler<UpdateCardCommand> {
|
||||
let questionImg, answerImg
|
||||
|
||||
if (command.questionImg && command.answerImg) {
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||
command.questionImg?.buffer,
|
||||
command.questionImg?.originalname
|
||||
)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(
|
||||
command.answerImg?.buffer,
|
||||
command.answerImg?.originalname
|
||||
)
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(command.questionImg)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(command.answerImg)
|
||||
|
||||
const result = await Promise.all([addQuestionImagePromise, addAnswerImagePromise])
|
||||
|
||||
questionImg = result[0].fileUrl
|
||||
answerImg = result[1].fileUrl
|
||||
} else if (command.answerImg) {
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(
|
||||
command.answerImg?.buffer,
|
||||
command.answerImg?.originalname
|
||||
)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(command.answerImg)
|
||||
const result = await addAnswerImagePromise
|
||||
|
||||
answerImg = result.fileUrl
|
||||
} else if (command.questionImg) {
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||
command.questionImg?.buffer,
|
||||
command.questionImg?.originalname
|
||||
)
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(command.questionImg)
|
||||
const result = await addQuestionImagePromise
|
||||
|
||||
questionImg = result.fileUrl
|
||||
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
PaginatedDecksWithMaxCardsCount,
|
||||
} from './entities/deck.entity'
|
||||
import { MinMaxCards } from './entities/min-max-cards.entity'
|
||||
import { DecksRepository } from './infrastructure/decks.repository'
|
||||
import {
|
||||
CreateCardCommand,
|
||||
CreateDeckCommand,
|
||||
@@ -60,7 +61,10 @@ import {
|
||||
@ApiTags('Decks')
|
||||
@Controller('decks')
|
||||
export class DecksController {
|
||||
constructor(private commandBus: CommandBus) {}
|
||||
constructor(
|
||||
private commandBus: CommandBus,
|
||||
private decksRepository: DecksRepository
|
||||
) {}
|
||||
|
||||
@HttpCode(HttpStatus.PARTIAL_CONTENT)
|
||||
@ApiOperation({
|
||||
@@ -89,6 +93,25 @@ export class DecksController {
|
||||
return this.commandBus.execute(new GetAllDecksV2Command({ ...finalQuery, userId: req.user.id }))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.PARTIAL_CONTENT)
|
||||
@ApiOperation({ description: 'Retrieve paginated decks list.', summary: 'Paginated decks list' })
|
||||
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Version('2')
|
||||
@Get('empty')
|
||||
async findAllEmpty(@Query() query: GetAllDecksDto, @Req() req) {
|
||||
const result: PaginatedDecks = await this.commandBus.execute(
|
||||
new GetAllDecksV2Command({
|
||||
itemsPerPage: 5000,
|
||||
minCardsCount: 0,
|
||||
maxCardsCount: 0,
|
||||
userId: req.user.id,
|
||||
})
|
||||
)
|
||||
|
||||
return this.decksRepository.deleteManyById(result.items.map(({ id }) => id))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({
|
||||
description: 'Retrieve the minimum and maximum amount of cards in a deck.',
|
||||
|
||||
@@ -294,6 +294,21 @@ LIMIT $${conditions.length + havingConditions.length + 1} OFFSET $${
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteManyById(id: string[]) {
|
||||
try {
|
||||
return await this.prisma.deck.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: id,
|
||||
},
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
this.logger.error(e?.message)
|
||||
throw new InternalServerErrorException(e?.message)
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteDeckById(id: string) {
|
||||
try {
|
||||
return await this.prisma.deck.delete({
|
||||
|
||||
@@ -38,32 +38,20 @@ export class CreateCardHandler implements ICommandHandler<CreateCardCommand> {
|
||||
}
|
||||
|
||||
if (command.questionImg && command.answerImg) {
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||
command.questionImg?.buffer,
|
||||
command.questionImg?.originalname
|
||||
)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(
|
||||
command.answerImg?.buffer,
|
||||
command.answerImg?.originalname
|
||||
)
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(command.questionImg)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(command.answerImg)
|
||||
|
||||
const result = await Promise.all([addQuestionImagePromise, addAnswerImagePromise])
|
||||
|
||||
questionImg = result[0].fileUrl
|
||||
answerImg = result[1].fileUrl
|
||||
} else if (command.answerImg) {
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(
|
||||
command.answerImg?.buffer,
|
||||
command.answerImg?.originalname
|
||||
)
|
||||
const addAnswerImagePromise = this.fileUploadService.uploadFile(command.answerImg)
|
||||
const result = await addAnswerImagePromise
|
||||
|
||||
answerImg = result.fileUrl
|
||||
} else if (command.questionImg) {
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(
|
||||
command.questionImg?.buffer,
|
||||
command.questionImg?.originalname
|
||||
)
|
||||
const addQuestionImagePromise = this.fileUploadService.uploadFile(command.questionImg)
|
||||
const result = await addQuestionImagePromise
|
||||
|
||||
questionImg = result.fileUrl
|
||||
|
||||
@@ -23,10 +23,7 @@ export class CreateDeckHandler implements ICommandHandler<CreateDeckCommand> {
|
||||
let cover
|
||||
|
||||
if (command.cover) {
|
||||
const result = await this.fileUploadService.uploadFile(
|
||||
command.cover.buffer,
|
||||
command.cover.originalname
|
||||
)
|
||||
const result = await this.fileUploadService.uploadFile(command.cover)
|
||||
|
||||
cover = result.fileUrl
|
||||
}
|
||||
|
||||
@@ -35,10 +35,7 @@ export class UpdateDeckHandler implements ICommandHandler<UpdateDeckCommand> {
|
||||
let cover
|
||||
|
||||
if (command.cover) {
|
||||
const result = await this.fileUploadService.uploadFile(
|
||||
command.cover.buffer,
|
||||
command.cover.originalname
|
||||
)
|
||||
const result = await this.fileUploadService.uploadFile(command.cover)
|
||||
|
||||
cover = result.fileUrl
|
||||
} else if (command.deck.cover === '') {
|
||||
|
||||
Reference in New Issue
Block a user