From 650b2987675a1dc6464bb524533a6316e8d367fa Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Mon, 9 Jan 2023 08:53:27 +0100 Subject: [PATCH] add fonction notifFonction.js --- Site Web/client/src/actions/user.actions.js | 4 +++- Site Web/client/src/components/Notif.js | 5 +++-- Site Web/controllers/notifFonction.js | 12 +++++++++++ Site Web/controllers/post.controller.js | 23 +++++++++++++++++++++ Site Web/controllers/user.controller.js | 2 +- Site Web/models/post.model.js | 2 +- Site Web/models/user.model.js | 22 +++++++++++++++++++- Site Web/routes/user.routes.js | 1 - 8 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 Site Web/controllers/notifFonction.js diff --git a/Site Web/client/src/actions/user.actions.js b/Site Web/client/src/actions/user.actions.js index 4e6c504e..35af099e 100644 --- a/Site Web/client/src/actions/user.actions.js +++ b/Site Web/client/src/actions/user.actions.js @@ -58,4 +58,6 @@ export const unFollowUser = (followerId, idToUnFollow) => { }) .catch((err) => console.log(err)); }; -}; \ No newline at end of file +}; + + diff --git a/Site Web/client/src/components/Notif.js b/Site Web/client/src/components/Notif.js index 8fbd7a0b..65815e3e 100644 --- a/Site Web/client/src/components/Notif.js +++ b/Site Web/client/src/components/Notif.js @@ -1,10 +1,11 @@ import React, {useEffect} from 'react'; import {isEmpty} from "./Utils"; import {useSelector} from "react-redux"; +import axios from "axios"; -const Notif = ({message} ) => { - const usersData = useSelector((state) => state.users.users); +import { useDispatch } from 'react-redux'; +const Notif = ({message} ) => { return (
diff --git a/Site Web/controllers/notifFonction.js b/Site Web/controllers/notifFonction.js new file mode 100644 index 00000000..993622ac --- /dev/null +++ b/Site Web/controllers/notifFonction.js @@ -0,0 +1,12 @@ +const UserModel = require("../models/user.model"); + +module.exports.addNotification = (userId, notification) => { + UserModel.findById(userId, (err, user) => { + if (err) { + console.log(err); + } else { + user.notif.push(notification); + user.save(); + } + }); +}; \ No newline at end of file diff --git a/Site Web/controllers/post.controller.js b/Site Web/controllers/post.controller.js index 7d4a6b1e..8cd65255 100644 --- a/Site Web/controllers/post.controller.js +++ b/Site Web/controllers/post.controller.js @@ -7,6 +7,21 @@ const ObjectID = require("mongoose").Types.ObjectId; const fs = require("fs"); const { promisify } = require("util"); +//import { addNotification } from "./notifFonction"; +const {addNotification} = require("./notifFonction"); +//const UserModel = require("../models/user.model"); + +/*const addNotification = (userId, notification) => { + UserModel.findById(userId, (err, user) => { + if (err) { + console.log(err); + } else { + user.notif.push(notification); + user.save(); + } + }); +};*/ + module.exports.readPost = (req, res) => { PostModel.find((err, docs) => { if (!err) res.send(docs); @@ -60,7 +75,15 @@ module.exports.deletePost = (req, res) => { }); }; + module.exports.likePost = async (req, res) => { + addNotification("63b835de3f9be509b614df36", { + typeNotif: "like", + id_user: "5f1a32c2f9f9ab74a1b7a6c8", + id_post1: "5f1a32c2f9f9ab74a1b7a6c8", + id_post2: "" + }); + if (!ObjectID.isValid(req.params.id)) return res.status(400).send("ID unknown : " + req.params.id); diff --git a/Site Web/controllers/user.controller.js b/Site Web/controllers/user.controller.js index 678a5928..d96fb2ad 100644 --- a/Site Web/controllers/user.controller.js +++ b/Site Web/controllers/user.controller.js @@ -113,4 +113,4 @@ module.exports.unfollow = async (req, res) => { } catch (err) { return res.status(401).send(err); } -} \ No newline at end of file +} diff --git a/Site Web/models/post.model.js b/Site Web/models/post.model.js index 1b2ad8db..4083f14c 100644 --- a/Site Web/models/post.model.js +++ b/Site Web/models/post.model.js @@ -1,4 +1,4 @@ -const mongoose = require('mongoose'); + const mongoose = require('mongoose'); //trim pour supprimer les espaces const postSchema = new mongoose.Schema( diff --git a/Site Web/models/user.model.js b/Site Web/models/user.model.js index 4f3e2ac0..15873c16 100644 --- a/Site Web/models/user.model.js +++ b/Site Web/models/user.model.js @@ -5,6 +5,25 @@ const { isEmail } = require('validator'); const bcrypt = require('bcrypt'); +const NotifSchema = new mongoose.Schema({ + typeNotif: { + type: String, + required: true + }, + id_user: { + type: String, + required: true + }, + id_post1: { + type: String, + required: true + }, + id_post2: { + type: String, + required: true + } +}); + //trim pour supprimer les espaces const userSchema = new mongoose.Schema( { @@ -46,7 +65,8 @@ const userSchema = new mongoose.Schema( }, likes: { type: [String] - } + }, + notif: [NotifSchema] }, { timestamps: true, diff --git a/Site Web/routes/user.routes.js b/Site Web/routes/user.routes.js index de10ce63..859a50fc 100644 --- a/Site Web/routes/user.routes.js +++ b/Site Web/routes/user.routes.js @@ -21,7 +21,6 @@ router.delete('/:id', userController.deleteUser); router.patch('/follow/:id', userController.follow); router.patch('/unfollow/:id', userController.unfollow); - //upload pb avec postman router.post("/upload", upload.single('file'), uploadController.uploadProfil);