Skip to content
Snippets Groups Projects
Commit 14c2ad98 authored by Talty-Kerr, Patrick (UG - Comp Sci & Elec Eng)'s avatar Talty-Kerr, Patrick (UG - Comp Sci & Elec Eng)
Browse files

basic routes,controllers,models added.not complete

parent 954ae1e2
No related branches found
No related tags found
1 merge request!13Create Comment service requirements
import { NextFunction, Request, Response } from "express";
import mongoose from 'mongoose';
import Comment from "../models/Comment";
const createComment = (req: Request, res: Response, next: NextFunction) => {
const { body } = req.body;
const comment = new Comment({
_id: new mongoose.Types.ObjectId(),
body
});
return comment
.save()
.then((comment) => res.status(201).json({ comment }))
.catch((error) => res.status(500).json({ error }));
};
/*
const readComment = (req: Request, res: Response, next: NextFunction) => {
const commentId = req.params.commentId;
return Comment.findById(commentId)
.then((comment) => ( comment ? res.status(200).json({ comment }) : res.status(404).json({ message: ' Not Found'})))
.catch((error) => res.status(500).json({ error }));
}
*/
const readAllComments = (req: Request, res: Response, next: NextFunction) => {
return Comment.find()
.then((comments) => res.status(200).json({ comments }))
.catch((error) => res.status(500).json({ error }));
}
export default { createComment, readAllComments}
//const createComment = (req: Request, res: Response, next: NextFunction) => {}
//const createComment = (req: Request, res: Response, next: NextFunction) => {}
\ No newline at end of file
import mongoose, {Document, Schema} from 'mongoose';
export interface IComment {
user_id: number;
post_id: number;
body: string;
}
export interface ICommentModel extends IComment, Document{}
const CommentSchema: Schema = new Schema(
{ //Commented Out for testing
//user_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
//post_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'Post', required: true},
body: { type: String, required: true },
},
{
timestamps: true,
}
)
export default mongoose.model<ICommentModel>('Comments', CommentSchema)
import express from "express";
import controller from '../controllers/Comment';
const router = express.Router();
router.post('/create', controller.createComment);
router.get('/get/', controller.readAllComments);
export = router;
...@@ -3,7 +3,7 @@ import http from 'http'; ...@@ -3,7 +3,7 @@ import http from 'http';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { config } from './config/config'; import { config } from './config/config';
import Logging from './library/logging'; import Logging from './library/logging';
import commentRoutes from './routes/Comment'
const router = express(); const router = express();
//Connect to Mongo //Connect to Mongo
...@@ -49,6 +49,7 @@ const StartServer = () => { ...@@ -49,6 +49,7 @@ const StartServer = () => {
}); });
/**ROUTES */ /**ROUTES */
router.use('/comments', commentRoutes);
/**HEALTHCHECK */ /**HEALTHCHECK */
/** Healthcheck */ /** Healthcheck */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment