skip to Main Content

There are three files profiles.model.js, profiles.controller.js, and profiles.router.js. I want to implement the PUT method in these three file. First, profiles.model.js here needs some conditions or something like this. If I want to save or insert I use profilesDatabase.create but if I want to update then What do I need to do here?

profiles.model.js

const profilesDatabase = require("./profiles.mongo");

const getLatestProfile = async () => {
  const latestProfile = await profilesDatabase.findOne().sort("-id");
  if (!latestProfile) {
    return 0;
  }

  return latestProfile.id;
};

const getAllProfiles = async () => {
  return await profilesDatabase.find({}, { _id: 0, __v: 0 });
};

// Please ignore this saveProfile below, I want to implement the PUT method here.

// async function saveProfile(newProfile) {
//   await profilesDatabase.create(newProfile);
// }

// async function saveNewProfile(profile) {
//   const newProfile = Object.assign(profile);
//   await saveProfile(newProfile);
// }

async function getProfileById(id) {
  const profile = await profilesDatabase.findOne(
    {
      id: id,
    },
    { _id: 0, __v: 0 }
  );
  return profile;
}

module.exports = {
  getLatestProfile,
  getAllProfiles,
  // saveNewProfile,
  getProfileById,
};

profiles.controller.js

const {
  getLatestProfile,
  getAllProfiles,
  saveNewProfile,
  getProfileById,
} = require("../../models/profiles.model");

async function httpGetAllProfiles(req, res) {
  return res.status(200).json(await getAllProfiles());
}

// In this function below need to implement the PUT method code but I don't know how...!!!
async function httpUpdateProfile(req, res) {
  const id = Number(req.params.id);
  const profile = req.body;
}

async function httpGetProfileById(req, res) {
  const id = Number(req.params.id);

  const profile = await getProfileById(id);
  if (!profile) {
    return res.status(400).json({
      error: "Profile not found!",
    });
  }
  return res.status(200).json(profile);
}

module.exports = {
  httpGetAllProfiles,
  httpUpdateProfile,
  httpGetProfileById,
};

profiles.router.js

const express = require("express");

const {
  httpGetAllProfiles,
  httpSaveNewProfile,
  httpGetProfileById,
} = require("./profiles.controller");

const profileRouter = express.Router();

profileRouter.get("/", httpGetAllProfiles);

// And here, What is the correct way for put method...!!!
profileRouter.put("/save", httpSaveNewProfile);

profileRouter.get("/:id", httpGetProfileById);

module.exports = profileRouter;

2

Answers


  1. Modify:

    profileRouter.put("/save/:id", httpSaveNewProfile);
    

    In profiles.controller.js: use getProfileById(id) to check if the profile’s already existed.
    In profiles.model.js: use findOneAndUpdate(...) to update your profile.

    Login or Signup to reply.
  2. In profiles.model.js:

    async function saveNewProfile(id, body) {
      return await profilesDatabase.findByIdAndUpdate(id, body, { new: true });
    }
    

    In profiles.controller.js:

    async function httpSaveNewProfile(req, res) {
      try {
        const updatedProfile = await saveNewProfile(req.params.id, req.body);
        if (!updatedProfile) { return res.status(400).json({ error: 'Profile not found' });
        return res.status(200).json({ profile: updatedProfile });
      } catch (e) {
        return res.status(500).json({ error: 'Server error' })
      }
    }
    

    Finally, declare your route as:

    profileRouter.put("/save/:id", httpSaveNewProfile);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search