Skip to content

Commit

Permalink
3rd_Seminar Level1
Browse files Browse the repository at this point in the history
Related to: #5
  • Loading branch information
Crayon committed Apr 23, 2022
1 parent d861953 commit 3da9d97
Show file tree
Hide file tree
Showing 24 changed files with 607 additions and 0 deletions.
146 changes: 146 additions & 0 deletions 3rd_Seminar/node-typescript-init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
yarn.lock

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

# End of https://www.toptal.com/developers/gitignore/api/node
2 changes: 2 additions & 0 deletions 3rd_Seminar/node-typescript-init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# node-typescript-init
Node.js, Typescript, MongoDB (mongoose) Initial Boiler Plate
11 changes: 11 additions & 0 deletions 3rd_Seminar/node-typescript-init/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"watch": [
"src",
".env"
],
"ext": "js,ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node --transpile-only ./src/index.ts"
}
26 changes: 26 additions & 0 deletions 3rd_Seminar/node-typescript-init/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "node-typescript-init",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon",
"build": "tsc && node dist"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/mongoose": "^5.11.97",
"@types/node": "^17.0.25",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"dependencies": {
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-validator": "^6.14.0",
"mongoose": "^6.3.1"
}
}
23 changes: 23 additions & 0 deletions 3rd_Seminar/node-typescript-init/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import dotenv from "dotenv";

// Set the NODE_ENV to 'development' by default
process.env.NODE_ENV = process.env.NODE_ENV || "development";

const envFound = dotenv.config();
if (envFound.error) {
// This error should crash whole process

throw new Error("⚠️ Couldn't find .env file ⚠️");
}

export default {
/**
* Your favorite port
*/
port: parseInt(process.env.PORT as string, 10) as number,

/**
* MongoDB URI
*/
mongoURI: process.env.MONGODB_URI as string,
};
97 changes: 97 additions & 0 deletions 3rd_Seminar/node-typescript-init/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import express, { Request, Response } from "express";
import { UserCreateDto } from "../interfaces/user/UserCreateDto";
import statusCode from "../modules/statusCode";
import message from "../modules/responseMessage";
import util from "../modules/util";
import { UserService } from "../services";
import { UserUpdateDto } from "../interfaces/user/UserUpdateDto";

/**
* @route GET /user/:userId
* @desc Get User
* @access Public
* @param req
* @param res
*/
const findUserById = async (req: Request, res: Response) => {
const { userId } = req.params;

try {
const user = await UserService.findUserById(userId);

if (!user) {
return res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NOT_FOUND));
}

res.status(statusCode.OK).send(util.success(statusCode.OK, message.READ_USER_SUCCESS, user));
} catch (error) {
res.status(statusCode.INTERNAL_SERVER_ERROR).json(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
}


/**
* @route POST /user
* @desc Create User
* @access Public
* @param req
* @param res
*/
const createUser = async (req: Request, res: Response) => {
const userCreateDto: UserCreateDto = req.body; // User Create Dto로 req.body 값을 받아옴

try {
const data = await UserService.createUser(userCreateDto);

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_USER_SUCCESS, data));

} catch (error) {
console.log(error);
// 서버 내부에서 오류 발생
res.status(statusCode.INTERNAL_SERVER_ERROR).json(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
}

/**
* @route PUT /user/:userId
* @desc Update User
* @access Public
*/
const updateUser = async (req: Request, res: Response) => {
const userUpdateDto: UserUpdateDto = req.body;
const { userId } = req.params;

try {
await UserService.updateUser(userId, userUpdateDto);

res.status(statusCode.NO_CONTENT).send();
} catch (error) {
res.status(statusCode.INTERNAL_SERVER_ERROR).json(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
}

/**
* @route DELETE /user/:userId
* @desc Delete User
* @access Public
*/
const deleteUser = async (req: Request, res: Response) => {
const { userId } = req.params;

try {
await UserService.deleteUser(userId);

res.status(statusCode.NO_CONTENT).send();

} catch (error) {
res.status(statusCode.INTERNAL_SERVER_ERROR).json(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}

}

export default {
findUserById,
createUser,
updateUser,
deleteUser
}
6 changes: 6 additions & 0 deletions 3rd_Seminar/node-typescript-init/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import UserController from "./UserController"

// controller index file
export {
UserController
}
42 changes: 42 additions & 0 deletions 3rd_Seminar/node-typescript-init/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import express, { Request, Response, NextFunction } from "express";
import config from "./config";
const app = express();
import connectDB from "./loaders/db";
import routes from './routes';
require('dotenv').config();

connectDB();

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(routes); //라우터
// error handler

interface ErrorType {
message: string;
status: number;
}

app.use(function (err: ErrorType, req: Request, res: Response, next: NextFunction) {

res.locals.message = err.message;
res.locals.error = req.app.get("env") === "production" ? err : {};

// render the error page
res.status(err.status || 500);
res.render("error");
});

app
.listen(config.port, () => {
console.log(`
################################################
🛡️ Server listening on port 🛡️
################################################
`);
})
.on("error", (err) => {
console.error(err);
process.exit(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mongoose from "mongoose";

export interface PostBaseResponseDto {
_id: mongoose.Schema.Types.ObjectId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface SchoolInfo {
name: string;
major: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SchoolInfo } from "../school/SchoolInfo";

export interface UserCreateDto {
name: string;
phone: string;
email: string;
age?: number;
school?: SchoolInfo;
}
Loading

0 comments on commit 3da9d97

Please sign in to comment.