From 75360486b16d63aa4e9110c13c1573cc35063224 Mon Sep 17 00:00:00 2001 From: Uttkarsh24 Date: Tue, 18 Feb 2025 18:31:58 +0530 Subject: [PATCH] Refactor game ID handling in controller and validation schemas --- Backend/src/controllers/game.controller.js | 33 ++++++++++--------- .../utils/zodSchema/gameValidatorSchema.js | 10 +++--- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Backend/src/controllers/game.controller.js b/Backend/src/controllers/game.controller.js index daba653..e7347b0 100644 --- a/Backend/src/controllers/game.controller.js +++ b/Backend/src/controllers/game.controller.js @@ -4,15 +4,16 @@ import ApiResponse from "../utils/ApiResponse.js"; import { Game } from "../models/gameId.models.js"; const addBgmiId = asyncHandler(async (req, res) => { - const { bgmiId } = req.body; + const { gameID } = req.body; + const user = req.user; - if (!bgmiId) { + if (!gameID) { throw new ApiError(400, "All Fields Are Required."); } try { await Game.findOneAndUpdate( { owner: user._id }, - { bgmiId: bgmiId }, + { bgmiId: gameID }, { new: true } ); return res @@ -24,15 +25,15 @@ const addBgmiId = asyncHandler(async (req, res) => { }); const addFreeFireId = asyncHandler(async (req, res) => { - const { freeFireId } = req.body; + const { gameID } = req.body; const user = req.user; - if (!freeFireId) { + if (!gameID) { throw new ApiError(400, "All Fields Are Required."); } try { await Game.findOneAndUpdate( { owner: user._id }, - { freefireId: freeFireId }, + { freefireId: gameID }, { new: true } ); return res @@ -44,9 +45,9 @@ const addFreeFireId = asyncHandler(async (req, res) => { }); const addValorantId = asyncHandler(async (req, res) => { - const { valorantId } = req.body; + const { gameID } = req.body; const user = req.user; - if (!valorantId.tagline || !valorantId.riotId) { + if (!gameID.tagline || !gameID.riotId) { throw new ApiError(400, "All Fields Are Required."); } try { @@ -54,8 +55,8 @@ const addValorantId = asyncHandler(async (req, res) => { { owner: user._id }, { valorantId: { - riotId: valorantId.riotId, - tagline: valorantId.tagline + riotId: gameID.riotId, + tagline: gameID.tagline } }, { new: true } @@ -69,15 +70,15 @@ const addValorantId = asyncHandler(async (req, res) => { }); const addCodmId = asyncHandler(async (req, res) => { - const { codmId } = req.body; + const { gameID } = req.body; const user = req.user; - if (!codmId) { + if (!gameID) { throw new ApiError(400, "All Fields Are Required."); } try { await Game.findOneAndUpdate( { owner: user._id }, - { codmId: codmId }, + { codmId: gameID }, { new: true } ); return res @@ -89,15 +90,15 @@ const addCodmId = asyncHandler(async (req, res) => { }); const addAsphaltId = asyncHandler(async (req, res) => { - const { asphaltId } = req.body; + const { gameID } = req.body; const user = req.user; - if (!asphaltId) { + if (!gameID) { throw new ApiError(400, "All Fields Are Required."); } try { await Game.findOneAndUpdate( { owner: user._id }, - { asphaltId: asphaltId }, + { asphaltId: gameID }, { new: true } ); return res diff --git a/Backend/src/utils/zodSchema/gameValidatorSchema.js b/Backend/src/utils/zodSchema/gameValidatorSchema.js index 0bc4edb..7614152 100644 --- a/Backend/src/utils/zodSchema/gameValidatorSchema.js +++ b/Backend/src/utils/zodSchema/gameValidatorSchema.js @@ -1,26 +1,26 @@ import { z } from "zod"; const bgmiIdSchema = z.object({ - bgmiId: z.string().min(1, { message: "Bgmi Id is required" }), + gameID: z.string().min(1, { message: "Bgmi Id is required" }), }); const freeFireIdSchema = z.object({ - freeFireId: z.string().min(1, { message: "FreeFire Id is required" }), + gameID: z.string().min(1, { message: "FreeFire Id is required" }), }); const valorantIdSchema = z.object({ - valorantId: z.object({ + gameID: z.object({ riotId: z.string().min(1, { message: "Riot Id is required" }), tagline: z.string().min(1, { message: "Tagline is required" }), }), }); const codmIdSchema = z.object({ - codmId: z.string().min(1, { message: "Codm Id is required" }), + gameID: z.string().min(1, { message: "Codm Id is required" }), }); const asphaltIdSchema = z.object({ - asphaltId: z.string().min(1, { message: "Asphalt Id is required" }), + gameID: z.string().min(1, { message: "Asphalt Id is required" }), }); export {