node-mongo-demo

node.js and mongodb demo

git clone https://9o.is/git/node-mongo-demo.git

commit a4c6edd672406f72e0029d1f0e8214ec33e1acac
parent 33383588211fe32ec6a91d8b4491cb8e63fe1d88
Author: Jul <jul@9o.is>
Date:   Sun, 26 Jan 2025 08:10:50 -0500

add POST /api/lucky7/session endpoint

Diffstat:
Mbackend/index.js | 2++
Abackend/src/api/lucky7-sessions-create.js | 40++++++++++++++++++++++++++++++++++++++++
Abackend/src/api/lucky7.js | 9+++++++++
Abackend/src/models/lucky7-game.js | 12++++++++++++
Abackend/src/models/lucky7-session.js | 10++++++++++
5 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/backend/index.js b/backend/index.js @@ -4,6 +4,7 @@ import bodyParser from "body-parser"; import cors from "cors"; import dotenv from 'dotenv'; import userRouter from "./src/api/user.js"; +import lucky7Router from "./src/api/lucky7.js"; dotenv.config(); @@ -13,6 +14,7 @@ app.use(bodyParser.urlencoded({ limit: "5mb", extended: true })); app.use(cors()); app.use("/api/user", userRouter); +app.use("/api/lucky7", lucky7Router); const PORT = process.env.PORT || 5000; diff --git a/backend/src/api/lucky7-sessions-create.js b/backend/src/api/lucky7-sessions-create.js @@ -0,0 +1,40 @@ +import Lucky7Session from "../models/lucky7-session.js"; + +const dateDifferenceInSeconds = (date1, date2) => { + return Math.floor((date2 - date1) / 1000) + 1; +}; + +const nextGameTime = (startDateTime) => { + const currentSeconds = Math.ceil(new Date() / 1000); + const startSeconds = Math.floor(startDateTime.getTime() / 1000); + const difference = currentSeconds - startSeconds; + + const nextChunk = Math.ceil(difference / 15) * 15; + return new Date((startSeconds + nextChunk) * 1000); +}; + +const lucky7SessionsCreate = async (req, res) => { + const { userId } = req; + + try { + const session = await Lucky7Session.findOneAndUpdate({ + userId, + }, {}, { + upsert: true, + new: true, + }); + + const { _id, createdAt } = session.toObject(); + + res.status(200).json({ + id: _id, + createdAt, + nextGameInSeconds: dateDifferenceInSeconds(new Date(), nextGameTime(createdAt)), + }); + } catch (error) { + console.error(error); + res.status(500).json({ message: "Something went wrong" }); + } +}; + +export default lucky7SessionsCreate; diff --git a/backend/src/api/lucky7.js b/backend/src/api/lucky7.js @@ -0,0 +1,9 @@ +import express from "express"; +import lucky7SessionsCreate from "./lucky7-sessions-create.js"; +import auth from "../utils/auth.js"; + +const router = express.Router(); + +router.post("/sessions", auth, lucky7SessionsCreate); + +export default router; diff --git a/backend/src/models/lucky7-game.js b/backend/src/models/lucky7-game.js @@ -0,0 +1,12 @@ +import mongoose from "mongoose"; + +const lucky7GameSchema = mongoose.Schema({ + userId: { type: 'ObjectId', ref: 'User', required: true } + rollAt: { type: Date, required: true }, + lucky: { type: Boolean, required: true }, + roll: { type: [Number], required: true }, + win: { type: Boolean, required: true }, +}); + +export default mongoose.model("Lucky7Game", lucky7GameSchema); + diff --git a/backend/src/models/lucky7-session.js b/backend/src/models/lucky7-session.js @@ -0,0 +1,10 @@ +import mongoose from "mongoose"; + +const EXPIRES_IN_SECONDS = 86400; // 1 day + +const lucky7SessionSchema = mongoose.Schema({ + userId: { type: 'ObjectId', ref: 'User', required: true, unique: true }, + createdAt: { type: Date, expires: EXPIRES_IN_SECONDS, default: Date.now }, +}); + +export default mongoose.model("Lucky7Session", lucky7SessionSchema);