Skip to content

Commit

Permalink
feat: 기부자 수정 api (#593)
Browse files Browse the repository at this point in the history
* feat: update donator api create

* feat: patch donator fix

add authValidate, modify api endpoint, build test

---------

Co-authored-by: Jihwan <[email protected]>
Co-authored-by: scarf <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2023
1 parent 80f6953 commit 2e4b534
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
60 changes: 60 additions & 0 deletions backend/src/v1/books/books.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-unresolved */
import {
NextFunction, Request, RequestHandler, Response,
} from 'express';
Expand All @@ -9,9 +10,13 @@ import isNullish from '~/v1/utils/isNullish';
import * as BooksService from './books.service';
import * as types from './books.type';
import LikesService from './likes.service';
import { searchSchema } from '../users/users.types';
import { User } from '../DTO/users.model';
import UsersService from '../users/users.service';
import * as parseCheck from '~/v1/utils/parseCheck';

const likesService = new LikesService();
const usersService = new UsersService();

const pubdateFormatValidator = (pubdate: string | Date) => {
const regexConditon = /^[0-9]{8}$/;
Expand Down Expand Up @@ -402,3 +407,58 @@ export const updateBookInfo = async (
}
return 0;
};

export const updateBookDonator = async (
req: Request,
res: Response,
next: NextFunction,
) => {
const parsed = searchSchema.safeParse(req.body);
if (!parsed.success) {
return next(new ErrorResponse(errorCode.INVALID_INPUT, status.BAD_REQUEST));
}
const {
nicknameOrEmail, page, limit,
} = parsed.data;
let items;
let user;
try {
if (nicknameOrEmail) {
items = JSON.parse(JSON.stringify(
await usersService.searchUserBynicknameOrEmail(nicknameOrEmail, limit, page),
));
}
if (items) {
items.items = await Promise.all(items.items.map(async (data: User) => ({
...data,
})));
}
if (items.items[0]) {
user = items.items[0];
} else {
return next(new ErrorResponse(errorCode.INVALID_INPUT, status.BAD_REQUEST));
}
const bookDonator: types.UpdateBookDonator = {
id: req.body.id,
donatorId: user.id,
donator: user.nickname,
};

if (bookDonator.id <= 0 || Number.isNaN(bookDonator.id)) {
return next(new ErrorResponse(errorCode.INVALID_INPUT, status.BAD_REQUEST));
}
if (bookDonator.id) { await BooksService.updateBookDonator(bookDonator); }

return res.status(status.NO_CONTENT).send();
} catch (error: any) {
const errorNumber = parseInt(error.message, 10);
if (errorNumber >= 300 && errorNumber < 400) {
next(new ErrorResponse(error.message, status.BAD_REQUEST));
} else if (error.message === 'DB error') {
next(new ErrorResponse(errorCode.QUERY_EXECUTION_FAILED, status.INTERNAL_SERVER_ERROR));
}
logger.error(error);
next(new ErrorResponse(errorCode.FAIL_PATCH_BOOK_BY_UNEXPECTED, status.INTERNAL_SERVER_ERROR));
}
return 0;
};
6 changes: 5 additions & 1 deletion backend/src/v1/books/books.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Book, BookInfo, User, Lending, Category, VSearchBook,
} from '~/entity/entities';
import {
CreateBookInfo, LendingBookList, UpdateBook, UpdateBookInfo,
CreateBookInfo, LendingBookList, UpdateBook, UpdateBookInfo, UpdateBookDonator,
} from './books.type';
import { number } from "zod";

Expand Down Expand Up @@ -187,6 +187,10 @@ class BooksRepository extends Repository<Book> {
await this.books.update(book.id, book as Book);
}

async updateBookDonator(bookDonator: UpdateBookDonator): Promise<void> {
await this.books.update(bookDonator.id, bookDonator as Book);
}

async createBookInfo(
target: CreateBookInfo,
): Promise<BookInfo> {
Expand Down
7 changes: 6 additions & 1 deletion backend/src/v1/books/books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as models from './books.model';
import BooksRepository from './books.repository';
import {
CreateBookInfo, LendingBookList, UpdateBook, UpdateBookInfo,
categoryIds,
categoryIds, UpdateBookDonator,
} from './books.type';
import { categoryWithBookCount } from '../DTO/common.interface';

Expand Down Expand Up @@ -421,3 +421,8 @@ export const updateBook = async (book: UpdateBook) => {
const booksRepository = new BooksRepository();
await booksRepository.updateBook(book);
};

export const updateBookDonator = async (bookDonator: UpdateBookDonator) => {
const booksRepository = new BooksRepository();
await booksRepository.updateBookDonator(bookDonator);
}
6 changes: 6 additions & 0 deletions backend/src/v1/books/books.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export type UpdateBook = {
status: number;
}

export type UpdateBookDonator = {
id: number;
donator: string;
donatorId: number;
}

export enum categoryIds{
'K' = 1,
'C',
Expand Down
5 changes: 4 additions & 1 deletion backend/src/v1/routes/books.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
deleteLike,
getLikeInfo,
updateBookInfo,
updateBookDonator,
} from '~/v1/books/books.controller';
import authValidate from '~/v1/auth/auth.validate';
import authValidateDefaultNullUser from '~/v1/auth/auth.validateDefaultNullUser';
Expand Down Expand Up @@ -1099,4 +1100,6 @@ router
* type: json
* example : { errorCode: 311 }
*/
.patch('/update', authValidate(roleSet.librarian), updateBookInfo);
.patch('/update', authValidate(roleSet.librarian), updateBookInfo)
.patch('/donator', authValidate(roleSet.librarian), updateBookDonator);

0 comments on commit 2e4b534

Please sign in to comment.