fix: News now display author
This commit is contained in:
@@ -2,26 +2,28 @@ import { Router } from "express";
|
|||||||
import News from "@schemas/News"
|
import News from "@schemas/News"
|
||||||
import { Perms, adminPerm } from "@/utility";
|
import { Perms, adminPerm } from "@/utility";
|
||||||
import capability, { Features } from "@/helpers/capability";
|
import capability, { Features } from "@/helpers/capability";
|
||||||
|
import { IUser } from "@/schemas/User";
|
||||||
|
|
||||||
const newsRouter = Router()
|
const newsRouter = Router()
|
||||||
|
|
||||||
newsRouter.use(adminPerm(Perms.News))
|
newsRouter.use(adminPerm(Perms.News))
|
||||||
newsRouter.use(capability.mw(Features.News))
|
newsRouter.use(capability.mw(Features.News))
|
||||||
|
|
||||||
newsRouter.get('/', async (req,res)=>{
|
newsRouter.get('/', async (req, res) => {
|
||||||
res.send(await News.find({},null,{sort: {pinned: -1 ,date: -1}}))
|
var news = await News.find(undefined, undefined, { sort: { pinned: -1, date: -1 } }).populate<{ author: Pick<IUser, "fname" | "surname" | "uname"> }>("author", ["fname", "surname", "uname"])
|
||||||
|
res.send(news)
|
||||||
})
|
})
|
||||||
newsRouter.post('/', async (req,res)=>{
|
newsRouter.post('/', async (req, res) => {
|
||||||
await News.create({title: req.body.title, content: req.body.content})
|
await News.create({ title: req.body.title, content: req.body.content, author: req.user._id })
|
||||||
res.status(201).send({status: 201})
|
res.status(201).send({ status: 201 })
|
||||||
})
|
})
|
||||||
newsRouter.delete('/:id', async (req,res)=>{
|
newsRouter.delete('/:id', async (req, res) => {
|
||||||
await News.findByIdAndDelete(req.params.id)
|
await News.findByIdAndDelete(req.params.id)
|
||||||
res.send({status: 200})
|
res.send({ status: 200 })
|
||||||
})
|
})
|
||||||
newsRouter.put('/:id', async (req,res)=>{
|
newsRouter.put('/:id', async (req, res) => {
|
||||||
await News.findByIdAndUpdate(req.params.id, req.body)
|
await News.findByIdAndUpdate(req.params.id, { ...req.body, author: req.user._id })
|
||||||
res.send({status: 200})
|
res.send({ status: 200 })
|
||||||
})
|
})
|
||||||
|
|
||||||
export {newsRouter};
|
export { newsRouter };
|
||||||
@@ -10,13 +10,14 @@ import usettings from "@/helpers/usettings";
|
|||||||
import Grade from "@schemas/Grade";
|
import Grade from "@schemas/Grade";
|
||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import Inbox from "@/schemas/Inbox";
|
import Inbox from "@/schemas/Inbox";
|
||||||
|
import { IUser } from "@/schemas/User";
|
||||||
|
|
||||||
export const appRouter = Router();
|
export const appRouter = Router();
|
||||||
|
|
||||||
appRouter.use(islogged)
|
appRouter.use(islogged)
|
||||||
|
|
||||||
appRouter.get("/news", capability.mw(Features.News), async (req, res) => {
|
appRouter.get("/news", capability.mw(Features.News), async (req, res) => {
|
||||||
var news = await News.find({"visible": {"$ne": false}}, {_id: 0, visible: 0}, {sort: {pinned: -1 ,date: -1}})
|
var news = await News.find({"visible": {"$ne": false}}, {_id: 0, visible: 0}, {sort: {pinned: -1 ,date: -1}}).populate<{author: Pick<IUser, "fname" | "surname" | "uname">}>("author", ["fname", "surname", "uname"])
|
||||||
res.send(news)
|
res.send(news)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import mongoose, { Schema } from "mongoose"
|
import mongoose, { Schema, Types } from "mongoose"
|
||||||
|
|
||||||
interface INews {
|
interface INews {
|
||||||
content: string;
|
content: string;
|
||||||
title: string;
|
title: string;
|
||||||
date: Date;
|
date: Date;
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
pinned?: boolean
|
pinned?: boolean;
|
||||||
|
author: Types.ObjectId
|
||||||
}
|
}
|
||||||
|
|
||||||
const newsSchema = new Schema<INews>({
|
const newsSchema = new Schema<INews>({
|
||||||
@@ -13,7 +14,8 @@ const newsSchema = new Schema<INews>({
|
|||||||
title: {type: String, required: true},
|
title: {type: String, required: true},
|
||||||
date: {type: Date, requred: true, default: Date.now},
|
date: {type: Date, requred: true, default: Date.now},
|
||||||
visible: {type: Boolean, default: false},
|
visible: {type: Boolean, default: false},
|
||||||
pinned: {type: Boolean, default: false}
|
pinned: {type: Boolean, default: false},
|
||||||
|
author: {type: "ObjectId", ref: "logins", required: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
export default mongoose.model("news", newsSchema)
|
export default mongoose.model("news", newsSchema)
|
||||||
Reference in New Issue
Block a user