mostly adding posts controller and model but also updating some users stuff
This commit is contained in:
58
api/controllers/postsController.js
Normal file
58
api/controllers/postsController.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import Post from '../models/postModel.js';
|
||||||
|
|
||||||
|
export const getAllPosts = async (req, res, next) => {
|
||||||
|
const allPosts = await Post.find({}).exec();
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: allPosts
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getPost = async (req, res, next) => {
|
||||||
|
const post = await Post.findById(req.params.id).exec();
|
||||||
|
if (!post) {
|
||||||
|
return next('No document found with that id', 404);
|
||||||
|
}
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: post
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getAllPostsByUser = async (req, res, next) => {
|
||||||
|
const post = await Post.find({email: req.params.user}).exec();
|
||||||
|
if (!post) {
|
||||||
|
return next('No document found with that id', 404);
|
||||||
|
}
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: post
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createPost = async (req, res, next) => {
|
||||||
|
Post.create(req.body).then(result => {
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const updatePost = async (req, res, next) => {
|
||||||
|
Post.updateOne({_id: req.params.id},{$set: req.body}).then(result => {
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const deletePost = async (req, res, next) => {
|
||||||
|
Post.deleteOne({_id: req.params.id}).then(result => {
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -2,11 +2,46 @@ import User from '../models/userModel.js';
|
|||||||
|
|
||||||
export const getAllUsers = async (req, res, next) => {
|
export const getAllUsers = async (req, res, next) => {
|
||||||
const allUsers = await User.find({}).exec();
|
const allUsers = await User.find({}).exec();
|
||||||
res.send(allUsers);
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: allUsers
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getUser = async (req, res, next) => {
|
||||||
|
const user = await User.findById(req.params.id).exec();
|
||||||
|
if (!user) {
|
||||||
|
return next('No document found with that id', 404);
|
||||||
|
}
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: user
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createUser = async (req, res, next) => {
|
export const createUser = async (req, res, next) => {
|
||||||
User.create(req.body).then(result => {
|
User.create(req.body).then(result => {
|
||||||
res.send(result)
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const updateUser = async (req, res, next) => {
|
||||||
|
User.updateOne({_id: req.params.id},{$set: req.body}).then(result => {
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const deleteUser = async (req, res, next) => {
|
||||||
|
User.deleteOne({_id: req.params.id}).then(result => {
|
||||||
|
res.status(200).json({
|
||||||
|
status: 'success',
|
||||||
|
data: result
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
30
api/models/postModel.js
Normal file
30
api/models/postModel.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
import mongoose from 'mongoose';
|
||||||
|
import User from './userModel';
|
||||||
|
|
||||||
|
const postSchema = new mongoose.Schema({
|
||||||
|
user: {
|
||||||
|
type: User,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
type: Date,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
photo: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
notes: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: String,
|
||||||
|
enum: ['created', 'pending', 'denied', 'approved'],
|
||||||
|
default: 'created'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const Post = mongoose.model('User', postSchema);
|
||||||
|
|
||||||
|
export default Post;
|
||||||
21
api/routes/posts.js
Normal file
21
api/routes/posts.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
import express from 'express';
|
||||||
|
import { createPost, deletePost, getAllPosts, getAllPostsByUser, getPost, updatePost } from './../controllers/usersController.js';
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router
|
||||||
|
.route('/')
|
||||||
|
.get(getAllPosts)
|
||||||
|
.post(createPost);
|
||||||
|
|
||||||
|
router
|
||||||
|
.route('/:id')
|
||||||
|
.get(getPost)
|
||||||
|
.patch(updatePost)
|
||||||
|
.delete(deletePost);
|
||||||
|
|
||||||
|
router
|
||||||
|
.route('/:user')
|
||||||
|
.get(getAllPostsByUser)
|
||||||
|
|
||||||
|
export default router;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { createUser, getAllUsers } from './../controllers/usersController.js';
|
import { createUser, deleteUser, getAllUsers, getUser, updateUser } from './../controllers/usersController.js';
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router
|
router
|
||||||
@@ -8,4 +8,10 @@ router
|
|||||||
.get(getAllUsers)
|
.get(getAllUsers)
|
||||||
.post(createUser);
|
.post(createUser);
|
||||||
|
|
||||||
|
router
|
||||||
|
.route('/:id')
|
||||||
|
.get(getUser)
|
||||||
|
.patch(updateUser)
|
||||||
|
.delete(deleteUser);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user