Skip to content
Snippets Groups Projects
Commit 6b87344c authored by bmate711's avatar bmate711
Browse files

Merge branch 'feature/news_api' into 'dev'

Feature/news api

See merge request kszk/devteam/bodysch/bodysch-backend!6
parents ba214b32 81c6f79a
No related branches found
No related tags found
2 merge requests!10Feature/12 dev auto deploy,!6Feature/news api
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
"watch": ["src"], "watch": ["src"],
"ext": "ts", "ext": "ts",
"ignore": ["src/public"], "ignore": ["src/public"],
"exec": "NODE_ENV=development ts-node src/index.ts" "exec": "ts-node --files src/index.ts"
} }
import "./utils/env" import "./utils/env";
import express, { Request, Response, NextFunction, Application } from "express"; import express, { Request, Response, NextFunction, Application } from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser"; import bodyParser from "body-parser";
import expressSession from "express-session"; import expressSession from "express-session";
import authRoute from './routes/auth'; import authRoute from "./routes/auth";
import userRoute from './routes/user'; import newsRoute from "./routes/news";
mongoose
.connect("mongodb://localhost:27017/bodysch", {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Connected to database");
})
.catch(err => {
console.log(
"MongoDB connection error. Please make sure MongoDB is running. " + err
);
// process.exit();
});
const app: Application = express(); const app: Application = express();
...@@ -26,6 +43,9 @@ app.get("/", (req: Request, res: Response) => { ...@@ -26,6 +43,9 @@ app.get("/", (req: Request, res: Response) => {
// Register routes // Register routes
authRoute(app); authRoute(app);
// Register routes
newsRoute(app);
app.use((err: any, req: Request, res: Response, next: NextFunction) => { app.use((err: any, req: Request, res: Response, next: NextFunction) => {
res.status(500).send("Houston, we have a problem!"); res.status(500).send("Houston, we have a problem!");
......
import { Request, Response, NextFunction } from "express";
import News from "../../models/NewsSchema";
const addNews = () => {
return (req: Request, res: Response, next: NextFunction) => {
const news = new News();
news.title = req.body.title;
news.text = req.body.text;
news.publishedAt = new Date().toDateString();
news.save(err => {
if (err) {
} else {
res.status(201);
res.data.newsObject = news;
}
next();
});
};
};
export default addNews;
import { Request, Response, NextFunction } from "express";
import News from "../../models/NewsSchema";
const deleteNews = () => {
return (req: Request, res: Response, next: NextFunction) => {
News.findByIdAndDelete(req.params.id, (error, result) => {
if (!error) {
res.status(204);
res.json({});
} else {
console.warn(error);
}
});
};
};
export default deleteNews;
import { Request, Response, NextFunction } from "express";
import News from "../../models/NewsSchema";
const getNews = () => {
return (req: Request, res: Response, next: NextFunction) => {
News.findById(req.params.id, (error, result) => {
if (!error) {
res.status(200);
res.data.newsObject = result;
} else {
console.warn(error);
}
next();
});
};
};
export default getNews;
import { Request, Response, NextFunction } from "express";
import News from "../../models/NewsSchema";
const getNewsList = () => {
return (req: Request, res: Response, next: NextFunction) => {
News.find({}, (err, news) => {
if (!err) {
res.data.news = news;
}
next();
});
};
};
export default getNewsList;
import { Document, Schema, model } from 'mongoose';
export interface INews extends Document {
title : string
text : string
/* author : {
// id: IUser["_id"],
userName: string,
},
editedBy?: {
// id: IUser["_id"],
userName: string,
}, */
publishedAt: string
}
const NewsSchema = new Schema({
title : { type: String, required: true },
text : { type: String, required: true },
/* author : {
id: { type: Schema.Types.ObjectId, required: true },
userName: { type: String, required: true }
},
editedBy: {
id: { type: Schema.Types.ObjectId, required: true },
userName: { type: String, required: true }
}, */
publishedAt: { type: String, required: true },
});
export default model<INews>('News', NewsSchema);
\ No newline at end of file
import { Application, Response } from "express";
import getNewsListMiddleware from "../middlewares/news/getNewsList";
import addNews from "../middlewares/news/addNews";
import getNews from "../middlewares/news/getNews";
import deleteNews from "../middlewares/news/deleteNews";
export default (app: Application): void => {
app.get("/news", getNewsListMiddleware(), (req, res: Response) => {
if (res.data.news) {
res.json(res.data.news);
} else {
throw Error("Cant get the news list");
}
});
app.post("/news", addNews(), (req, res: Response) => {
if (res.data.newsObject) {
res.json(res.data.newsObject);
} else {
throw Error("Cant add news");
}
});
app.get("/news/:id", getNews(), (req, res: Response) => {
if (res.data.newsObject) {
res.json(res.data.newsObject);
} else {
throw Error("Cant get the news");
}
});
app.post("/news/:id");
app.delete("/news/:id", deleteNews());
};
declare namespace Express {
export interface Request {}
}
import { INews } from "../../models/NewsSchema";
declare global {
namespace Express {
export interface Response {
data: {
news?: INews[] | null;
newsObject?: INews | null;
};
}
}
}
{ {
"files": [ "files": [
"./src/utils/declarations/session.d.ts" "./src/utils/declarations/response.d.ts",
"./src/utils/declarations/request.d.ts",
"./src/utils/declarations/session.d.ts",
], ],
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"strict": true, "strict": true,
......
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