Added modules for books & series.
This commit is contained in:
94
backend/nestjs-seshat-api/src/books/books.controller.ts
Normal file
94
backend/nestjs-seshat-api/src/books/books.controller.ts
Normal file
@ -0,0 +1,94 @@
|
||||
import { Body, Controller, Get, Post, Put, Request, Res, UseGuards } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { JwtAccessGuard } from 'src/auth/guards/jwt-access.guard';
|
||||
import { BooksService } from './books.service';
|
||||
import { BookSearchResultDto } from 'src/providers/dto/book-search-result.dto';
|
||||
import { UpdateBookOriginDto } from './dto/update-book-origin.dto';
|
||||
import { UpdateBookDto } from './dto/update-book.dto';
|
||||
import { PinoLogger } from 'nestjs-pino';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
|
||||
@UseGuards(JwtAccessGuard)
|
||||
@Controller('books')
|
||||
export class BooksController {
|
||||
constructor(
|
||||
private books: BooksService,
|
||||
private logger: PinoLogger,
|
||||
) { }
|
||||
|
||||
|
||||
@Post('')
|
||||
async CreateBook(
|
||||
@Request() req,
|
||||
@Body() body: BookSearchResultDto,
|
||||
@Res({ passthrough: true }) response: Response,
|
||||
) {
|
||||
try {
|
||||
return {
|
||||
success: true,
|
||||
data: await this.books.createBook(body),
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof QueryFailedError) {
|
||||
if (err.driverError.code == '23505') {
|
||||
// Book exists already.
|
||||
response.statusCode = 400;
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'The book has already been added previously.',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.error({
|
||||
class: BooksController.name,
|
||||
method: this.CreateBook.name,
|
||||
user: req.user,
|
||||
msg: 'Failed to create book.',
|
||||
error: err,
|
||||
});
|
||||
|
||||
response.statusCode = 500;
|
||||
return {
|
||||
success: false,
|
||||
error_message: 'Something went wrong while adding the book.',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Get('')
|
||||
async GetBooksFromUser(
|
||||
@Request() req,
|
||||
) {
|
||||
return {
|
||||
success: true,
|
||||
data: await this.books.findBookStatusesTrackedBy(req.user.userId),
|
||||
};
|
||||
}
|
||||
|
||||
@Put('')
|
||||
async UpdateBook(
|
||||
@Body() body: UpdateBookDto,
|
||||
) {
|
||||
const data = { ...body };
|
||||
delete data['bookId'];
|
||||
|
||||
const result = await this.books.updateBook(body.bookId, data);
|
||||
return {
|
||||
success: result?.affected == 1,
|
||||
};
|
||||
}
|
||||
|
||||
@Put('origins')
|
||||
async UpdateBookOrigin(
|
||||
@Body() body: UpdateBookOriginDto,
|
||||
) {
|
||||
const data = { ...body };
|
||||
delete data['bookOriginId'];
|
||||
|
||||
const result = await this.books.updateBookOrigin(body.bookOriginId, data);
|
||||
return {
|
||||
success: result?.affected == 1,
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user