Skip to content

Commit b35c627

Browse files
author
drkspark
committed
Create Blog Schema
1 parent b6a0e14 commit b35c627

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

backend/models/blog.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const mongoose = require("mongoose");
2+
3+
const blogSchema = new mongoose.Schema(
4+
{
5+
title: {
6+
type: String,
7+
required: [true, "Can't create Blog without Title"],
8+
},
9+
author: {
10+
type: String,
11+
required: [true, "Can't create Blog without Author"],
12+
},
13+
content: {
14+
type: String,
15+
required: [true, "Can't create Blog without Content"],
16+
},
17+
tags: [
18+
{
19+
type: String,
20+
},
21+
],
22+
// So as to identfiy the owner of the blog and will help in retrieval of blogs
23+
// User Id has to be fetched from FireBase user DB, since we will be uisng it for authentication.
24+
userId: {
25+
type: String,
26+
},
27+
date: {
28+
type: String,
29+
},
30+
likes: {
31+
type: Number,
32+
default: 0,
33+
},
34+
},
35+
{ timestamps: true }
36+
);
37+
38+
// This will extract Date from the createAt and store as dd-mm-yyyy
39+
blogSchema.pre("save", function (next) {
40+
let date = JSON.stringify(this.createdAt)
41+
.substring(1, 11)
42+
.split("-")
43+
.reverse()
44+
.join("-");
45+
46+
this.date = date;
47+
next();
48+
});
49+
50+
const Blog = mongoose.model("Blog", blogSchema);
51+
52+
module.exports = Blog;

backend/server.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
const express = require('express');
2-
const path = require('path');
3-
const cors = require('cors');
4-
const { PORT } = require('./config/serverConfig');
5-
const connect = require('./config/database');
6-
1+
const express = require("express");
2+
const path = require("path");
3+
const cors = require("cors");
4+
const { PORT } = require("./config/serverConfig");
5+
const connect = require("./config/database");
6+
const Blog = require("./models/blog");
77

88
const app = express();
99

10-
app.use(express.static(path.resolve(__dirname, '../frontend/build')));
10+
app.use(express.static(path.resolve(__dirname, "../frontend/build")));
1111
app.use(cors());
1212
app.use(express.urlencoded({ extended: false }));
1313
app.use(express.json());
1414

15-
app.get('/', (req, res) => {
16-
res.send('hello World!');
15+
app.get("/", (req, res) => {
16+
res.send("hello World!");
1717
});
1818

1919
app.listen(PORT, async () => {
2020
console.log(`Server started on http://localhost:${PORT}`);
2121
await connect();
22-
console.log('DB Connected');
22+
console.log("DB Connected");
23+
24+
// const blog = await Blog.create({
25+
// title: "Blog With Commentss",
26+
// author: "drkspark",
27+
// content: "Hello World",
28+
// tags: ["WebDev", "Trying First Time"],
29+
// });
30+
// console.log(blog);
2331
});

0 commit comments

Comments
 (0)