Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ACCESS_TOKEN_SECRET=[YOUR_ACCESS_TOKEN_SECRET_HERE]
REFRESH_TOKEN_SECRET=[YOUR_REFRESH_TOKEN_SECRET_HERE]
MONGO_URL=[YOUR_MONGO_CONNECTION_STRING_HERE]
MONGO_URL=mongodb+srv://AndresAccenture:Bytheway77.77@clusteraccenturemfee.amprhsx.mongodb.net/?retryWrites=true&w=majority&appName=ClusterAccentureMFEE
130 changes: 130 additions & 0 deletions apps/api/src/controllers/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Post from '../models/post';
import Comment from '../models/comment';

const posts = [];

// Get all posts
export const getPosts = async (req, res) => {
try {
const posts = await Post.find();

res.status(200).json(posts);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Get posts by id
const getPost = async (req, res) => {
// Retrieve the id from the route params
const { id } = req.params;

try {
// Check if we have a post with that id
const post = await Post.findById(id);

if (!post) {
// If we don't find the post return a 404 status code with a message
return res.status(404).json({ message: 'Post not found!' });
}
// Return the post with a 200 status code
res.status(200).json(post);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Get post by category
const getPostsByCategory = async (req, res) => {
// Retrieve the id from the route params
const { category, id } = req.params;
try {
const postFound = await Post.findOne(category);

if (!postFound) {
// If we don't find the post return a 404 status code with a message
return res.status(404).json({ message: 'Post by Category not found' });
// Note: Remember that json method doesn't interrupt the workflow
// therefore is important to add a "return" to break the process
}

// Return the post with a 200 status code
res.status(200).json(postFound);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Create post
const createPost = async (req, res) => {
try {
const post = await Post.create(req.body);
// Return the created post with a 201 status code
res.status(201).json(post);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Create comment
export const createComment = async (req, res) => {
const { id } = req.params;

try {
const comment = await new Comment(req.body).save();

await Post.findByIdAndUpdate(id, { $push: { comments: comment._id } });
res.status(201).json(comment);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Update post
const updatePost = async (req, res) => {
const { id } = req.params;

try {
const post = await Post.findByIdAndUpdate(id, req.body, { new: true });

if (!id) {
return res.status(400).json({ message: 'Post not found to make comments.' });
}
res.status(200).json(post);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

// Delete post
const deletePost = async (req, res) => {
// Retrieve the id from the route params
const { id } = req.params;

try {
const post = await Post.findByIdAndDelete(id);
if (!post) {
return res.status(404).json({ message: 'Post not found' });
}
res.status(200).json(post);
} catch (error) {
const { message } = error;
res.status(500).json({ message });
}
};

export default {
getPosts,
getPostsByCategory,
getPost,
createPost,
createComment,
updatePost,
deletePost
};
26 changes: 26 additions & 0 deletions apps/api/src/models/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose, { Document, Schema } from 'mongoose';

interface IComment extends Document {
author: string;
content: string;
}

export const commentSchema = new Schema<IComment>(
{
author: {
type: String,
required: [true, 'Property is required']
},
content: {
type: String,
required: [true, 'Property is required']
}
},
{
timestamps: true
}
);

const Comment = mongoose.model<IComment>('Comment', commentSchema);

export default Comment;
42 changes: 42 additions & 0 deletions apps/api/src/models/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mongoose, { Document, Schema, Types } from 'mongoose';

interface IPost extends Document {
title: string;
image: string;
description: string;
category: Types.ObjectId;
comments: [Types.ObjectId];
}

export const postSchema = new Schema<IPost>(
{
title: {
type: String,
required: [true, 'Property is required']
},
image: {
type: String
},
description: {
type: String,
required: [true, 'Property is required']
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category'
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}
]
},
{
timestamps: true
}
);

const Post = mongoose.model<IPost>('Post', postSchema);

export default Post;
28 changes: 28 additions & 0 deletions apps/api/src/routes/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from 'express';

import postController from '../controllers/posts';

const router = express.Router();

// Get all posts
router.get('/', postController.getPosts);

// Get post by category
router.get('/category/:category', postController.getPostsByCategory);

// Get post by id
router.get('/:id', postController.getPost);

// Create post
router.post('/', postController.createPost);

// Create comment
router.post('/:id/comments', postController.createComment);

// Update post
router.patch('/:id', postController.updatePost);

// Delete post
router.delete('/:id', postController.deletePost);

export default router;
Loading