Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

리소스 room 추가 #4 #5

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions module/room/room.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { roomModel } from "./room.model";
import { StatusCodes } from "http-status-codes";

const roomController = {};

roomController.create = async (req, res) => {
try {
const pin = (Math.floor(Math.random() * 1000000) + 1000000)
.toString()
.substring(1);
const dup = await roomModel.findOne({ pin: pin });
if (dup) throw new Error("Failed to generate PIN Code");
const room = await roomModel.create({
pin: pin,
gameId: req.body.gameId,
gamers: [],
});
return res.json(room);
} catch (error) {
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ error: error.toString() });
}
};

roomController.delete = async (req, res) => {
try {
const room = await roomModel.findByIdAndRemove(req.params.id);
if (!room) {
return res
.status(StatusCodes.BAD_REQUEST)
.json({ message: "room not found" });
}
return res.json({ message: "room deleted successfully" });
} catch (error) {
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ error: error.toString() });
}
};

export default roomController;
22 changes: 22 additions & 0 deletions module/room/room.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const roomSchema = new Schema({
pin: {
type: String,
required: true,
},
gameId: {
type: String,
required: true,
},
gamers: {
r4k0nb4k0n marked this conversation as resolved.
Show resolved Hide resolved
type: Array,
required: true,
default: [],
},
});

const roomModel = mongoose.model("room", roomSchema);
export { roomModel };
10 changes: 10 additions & 0 deletions module/room/room.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from "express";
import roomController from "./room.controller";
import { asyncWrapper } from "../../utils/asyncWrapper";

const roomRoutes = express.Router();

roomRoutes.post("/", asyncWrapper(roomController.create));
roomRoutes.delete("/:id", asyncWrapper(roomController.delete));

export { roomRoutes };
2 changes: 2 additions & 0 deletions routes/api/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import express from "express";
import { todoRoutes } from "../../module/todo/todo.routes";
import { roomRoutes } from "../../module/room/room.routes";

const apiRoutes = express.Router();

apiRoutes.use("/todos", todoRoutes);
apiRoutes.use("/room", roomRoutes);

export default apiRoutes;