Skip to content

Commit

Permalink
0.2.0 배포
Browse files Browse the repository at this point in the history
  • Loading branch information
doputer authored Dec 1, 2022
2 parents 0f582eb + 2121cee commit a063065
Show file tree
Hide file tree
Showing 78 changed files with 1,646 additions and 343 deletions.
28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/pull_request_template.md

This file was deleted.

68 changes: 68 additions & 0 deletions .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Backend CI

on:
pull_request:
branches: [main, develop]
paths:
- 'backend/**'

jobs:
lint:
name: Lint CI

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Cache Dependencies
uses: actions/cache@v3
id: backend-cache
with:
path: backend/node_modules
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-build-
${{ runner.OS }}-
- if: ${{ steps.backend-cache.outputs.cache-hit != 'true' }}
name: Install Dependencies
run: npm install
working-directory: backend

- name: Check Lint
run: npm run lint
env:
CI: true
working-directory: backend

build:
name: Build CI

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Cache Dependencies
uses: actions/cache@v3
id: backend-cache
with:
path: backend/node_modules
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-build-
${{ runner.OS }}-
- if: ${{ steps.backend-cache.outputs.cache-hit != 'true' }}
name: Install Dependencies
run: npm install
working-directory: backend

- name: Build
run: npm run build
env:
CI: true
working-directory: backend
2 changes: 1 addition & 1 deletion backend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: ['.eslintrc.js', 'dist/**'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"scripts": {
"dev": "cross-env NODE_ENV=dev tsc-watch --onSuccess \"ts-node -r tsconfig-paths/register dist/index.js\"",
"build": "tsc",
"start": "NODE_ENV=prod node -r ./tsconfig-paths.js dist/index.js"
"start": "NODE_ENV=prod node -r ./tsconfig-paths.js dist/index.js",
"lint": "eslint ."
},
"dependencies": {
"@prisma/client": "^4.7.0",
Expand Down
2 changes: 2 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ model Article {
book_id Int
scraps Scrap[]
@@fulltext([content])
@@fulltext([title])
@@fulltext([content, title])
}

Expand Down
39 changes: 24 additions & 15 deletions backend/src/apis/articles/articles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Request, Response } from 'express';

import { SearchArticles } from '@apis/articles/articles.interface';
import articlesService from '@apis/articles/articles.service';
import { IScrap } from '@apis/scraps/scraps.interface';
import scrapsService from '@apis/scraps/scraps.service';

const searchArticles = async (req: Request, res: Response) => {
const { query, page, take, userId } = req.query as unknown as SearchArticles;

const articles = await articlesService.searchArticles({ query, page, take, userId });
const searchResult = await articlesService.searchArticles({ query, page, take: +take, userId });

res.status(200).send(articles);
res.status(200).send(searchResult);
};

const getArticle = async (req: Request, res: Response) => {
Expand All @@ -20,22 +21,30 @@ const getArticle = async (req: Request, res: Response) => {
};

const createArticle = async (req: Request, res: Response) => {
const { title, content, book_id, order } = req.body;
const { article, scraps } = req.body;

const article = await articlesService.createArticle({
title,
content,
book_id,
const createdArticle = await articlesService.createArticle({
title: article.title,
content: article.content,
book_id: article.book_id,
});

const scrap = await scrapsService.createScrap({
order,
is_original: true,
book_id,
article_id: article.id,
// forEach와 async,await을 같이사용하는 것이 맞나? 다른방법은 없나?
const result: any[] = [];
scraps.forEach(async (scrap: IScrap) => {
if (scrap.id === 0) {
result.push(
await scrapsService.createScrap({
order: scrap.order,
is_original: true,
book_id: article.book_id,
article_id: createdArticle.id,
})
);
} else {
result.push(await scrapsService.updateScraps(scrap));
}
});

res.status(201).send({ article, scrap });
res.status(201).send({ createdArticle, result });
};

const deleteArticle = async (req: Request, res: Response) => {
Expand Down
10 changes: 6 additions & 4 deletions backend/src/apis/articles/articles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const searchArticles = async (searchArticles: SearchArticles) => {

const skip = (page - 1) * take;

const matchUserCondition = userId
const matchUserCondition = Number(userId)
? {
book: {
user: {
Expand All @@ -28,6 +28,7 @@ const searchArticles = async (searchArticles: SearchArticles) => {
created_at: true,
book: {
select: {
id: true,
user: {
select: {
id: true,
Expand All @@ -52,7 +53,10 @@ const searchArticles = async (searchArticles: SearchArticles) => {
skip,
});

return articles;
return {
data: articles,
hasNextPage: articles.length === take,
};
};

const getArticle = async (articleId: number) => {
Expand Down Expand Up @@ -85,7 +89,6 @@ const getArticle = async (articleId: number) => {

const createArticle = async (dto: CreateArticle) => {
const { title, content, book_id } = dto;

const article = await prisma.article.create({
data: {
title,
Expand All @@ -97,7 +100,6 @@ const createArticle = async (dto: CreateArticle) => {
},
},
});

return article;
};

Expand Down
43 changes: 38 additions & 5 deletions backend/src/apis/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Request, Response } from 'express';

import { FindBooks, SearchBooks } from '@apis/books/books.interface';
import booksService from '@apis/books/books.service';
import { IScrap } from '@apis/scraps/scraps.interface';
import scrapsService from '@apis/scraps/scraps.service';

const getBook = async (req: Request, res: Response) => {
const { bookId } = req.params;
Expand All @@ -16,23 +18,23 @@ const getBook = async (req: Request, res: Response) => {
};

const getBooks = async (req: Request, res: Response) => {
const { order, take, editor } = req.query as unknown as FindBooks;
const { order, take, editor, type } = req.query as unknown as FindBooks;

let userId = res.locals.user?.id;

if (!userId) userId = 0;

const books = await booksService.findBooks({ order, take: +take, userId, editor });
const books = await booksService.findBooks({ order, take: +take, userId, editor, type });

res.status(200).send(books);
};

const searchBooks = async (req: Request, res: Response) => {
const { query, page, take, userId } = req.query as unknown as SearchBooks;

const books = await booksService.searchBooks({ query, userId, take, page });
const searchResult = await booksService.searchBooks({ query, userId, take: +take, page });

res.status(200).send(books);
res.status(200).send(searchResult);
};

const createBook = async (req: Request, res: Response) => {
Expand All @@ -42,12 +44,43 @@ const createBook = async (req: Request, res: Response) => {

const book = await booksService.createBook({ title, userId });

res.status(201).send(book);
const bookData = await booksService.findBook(book.id, userId);

res.status(201).send(bookData);
};

const editBook = async (req: Request, res: Response) => {
const { id, title, thumbnail_image, scraps } = req.body;

const userId = res.locals.user.id;

const book = await booksService.editBook({ id, title, thumbnail_image });

const result: any[] = [];
scraps.forEach(async (scrap: IScrap) => {
result.push(await scrapsService.updateScraps(scrap));
});

const bookData = await booksService.findBook(book.id, userId);

res.status(200).send(bookData);
};

const deleteBook = async (req: Request, res: Response) => {
const bookId = Number(req.params.bookId);

const userId = res.locals.user.id;

const book = await booksService.deleteBook(bookId, userId);

res.status(200).send(book);
};

export default {
getBook,
getBooks,
searchBooks,
createBook,
editBook,
deleteBook,
};
1 change: 1 addition & 0 deletions backend/src/apis/books/books.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface FindBooks {
take: number;
userId?: number;
editor?: string;
type?: 'bookmark';
}

export interface CreateBook {
Expand Down
Loading

0 comments on commit a063065

Please sign in to comment.