Skip to content

Commit

Permalink
fix(*): add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Feb 12, 2024
1 parent 15ce8bb commit f07b7f3
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 10 deletions.
23 changes: 13 additions & 10 deletions controllers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ async function getComment(req, res) {

async function createComment(req, res) {
try {
let { comment_id, postId, name, email, comment, user_id, avatar } = req.body;
if (!name) name = 'Anonymous'
if (!email) email = '[email protected]'
let date = (new Date()).toLocaleDateString

const { comment_id, name, email, comment, markdown, user_id, avatar } = req.body;
console.log(req.body)
const com = await Comments.findOne({ comment_id });

// const com = await Comments.findOne({ comment_id });

if (com) {
logger.error("Comment already exist.");
return res.status(400).json({ msg: "This comment already exists." })
}
// if (com) {
// logger.error("Comment already exist.");
// return res.status(400).json({ msg: "This comment already exists." })
// }

const newComment = new Comments({
comment_id, name, email,
comment, markdown, user_id,
blog: req.params.id, avatar
name, email, comment, postId,
blog: postId, date
})

await newComment.save()
Expand All @@ -62,7 +65,7 @@ async function createComment(req, res) {

res.json({ msg: "Created a new comment" });
} catch (err) {

console.log(err)
logger.error(err)

return res.status(500).json({ msg: err.message })
Expand Down
112 changes: 112 additions & 0 deletions models/caseStudy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import mongoose from 'mongoose';

const caseStudySchema = new mongoose.Schema({
headerImg: {
type: Object,
required: true,
},
name: {
type: String,
required: true,
},
headline: {
type: String,
required: true,
},
role: {
type: String,
required: true,
},
context: {
type: String,
required: true,
},
img: {
type: Object,
required: true,
},
title: {
type: String,
required: true,
},
background: {
type: String,
required: true,
},
objectives: {
type: String,
required: true,
},
subHeading: {
type: String,
required: true,
},
prototype: {
type: String,
required: true,
},
source: {
type: [String],
required: true,
},
websites: {
type: [String],
required: true,
},
app: {
type: [String],
required: true,
},
design: {
type: String,
required: true,
},
designImg: {
type: String,
required: true,
},
designColor: {
type: Object,
required: true,
},
typography: {
type: Object,
required: true,
},
uiDesignImg: {
type: [String],
required: true,
},
userFlows: {
type: String,
required: true,
},
mainFunctions: {
type: [String],
required: true,
},
goal: {
type: String,
required: true,
},
version: {
type: String,
required: true,
},
tags: {
type: [String]
},
date: {
type: Date,
},
createdAt: {
type: Date,
default: Date.now
}
}, {
timestamps: true
})

const CaseStudy = mongoose.model('CaseStudy', caseStudySchema);

export default CaseStudy;
11 changes: 11 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ const commentSchema = new mongoose.Schema({
type:String,
required:"this filed is required"
},
postId: {
type: String,
required: true
},
date: {
type: String,
required: true
},
comments: {
type: String,
},
blog:{
type:mongoose.Schema.Types.ObjectId,
ref: 'Articles'
Expand Down
27 changes: 27 additions & 0 deletions src/API/CommentsAPI.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from 'react';
import axios from 'axios'

function CommentsAPI(id) {
const [comments, setComments] = useState([])
const [result, setResult] = useState(0)
const [callback, setCallback] = useState(false)

useEffect(() => {
if (id) {
const getComments = async () => {
const res = await axios.get(`/api/articles/${id}/comments`)
setComments(res.data.comments)
setResult(res.data.result)
}
getComments()
}
}, [callback, id])

return {
comments: [comments, setComments],
result: [result, setResult],
callback: [callback, setCallback],
}
}

export default CommentsAPI;

0 comments on commit f07b7f3

Please sign in to comment.