-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add peatch model. add create peatch, bind peatch to owner (user)
- Loading branch information
Fiodor Gorobet
committed
Nov 21, 2022
1 parent
3942d91
commit 72e7f47
Showing
15 changed files
with
162 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const { Schema, model } = require("mongoose"); | ||
|
||
const peatchSchema = new Schema( | ||
{ | ||
topic: { | ||
type: String, | ||
required: [true, "name for new peatch is required"], | ||
unique: true, | ||
}, | ||
description: { | ||
type: String, | ||
}, | ||
owner: { type: Schema.Types.ObjectId, ref: "User" }, | ||
members: [{ type: Schema.Types.ObjectId, ref: "User" }], | ||
proposals: [{ type: Schema.Types.ObjectId, ref: "User" }], | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
module.exports = model("Peatch", peatchSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { Schema, model } = require("mongoose"); | ||
|
||
const proposalSchema = new Schema( | ||
{ | ||
text: { | ||
type: String, | ||
required: [true, "question text is required"], | ||
unique: true, | ||
}, | ||
isVoted: { | ||
type: Boolean, | ||
}, | ||
votes: Number, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
module.exports = model("Proposal", proposalSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const Peatch = require("../models/Peatch.models"); | ||
const User = require("../models/User.model"); | ||
|
||
const router = require("express").Router(); | ||
|
||
router.get("/", (req, res, next) => { | ||
res.render("peatches/index", { | ||
isDashboard: true, | ||
}); | ||
}); | ||
|
||
router.get("/new", (req, res) => { | ||
res.render("peatches/new", { | ||
isActive: true, | ||
}); | ||
}); | ||
|
||
router.post("/new", async (req, res, next) => { | ||
const userId = req.session.user._id; | ||
const { topic, description } = req.body; | ||
|
||
console.log({ topic, description, userId }); | ||
|
||
try { | ||
const user = await User.findById({ _id: userId }); | ||
|
||
const peatch = await Peatch.create({ | ||
topic, | ||
description, | ||
owner: user, | ||
members: [user], | ||
}); | ||
|
||
res.redirect(`/peatches/${peatch.id}`); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
router.get("/:peatchId", async (req, res, next) => { | ||
const { peatchId } = req.params; | ||
|
||
try { | ||
const { topic, description, owner, members } = await Peatch.findOne({ _id: peatchId }).populate("owner").lean(); | ||
|
||
res.render("peatches/peatch", { | ||
topic, | ||
description, | ||
owner, | ||
members, | ||
}); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>All Peatches</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<h1>New Peatch</h1> | ||
|
||
<form action="/peatches/new" method="post"> | ||
|
||
<div class="mb-3"> | ||
<label for="topic" class="form-label">Topic</label> | ||
<input name="topic" type="text" class="form-control" id="topic" placeholder="What is the topic for this peatch?" /> | ||
</div> | ||
<div class="mb-3"> | ||
<label for="description" class="form-label">Description</label> | ||
<textarea name="description" class="form-control" id="description" rows="3"></textarea> | ||
</div> | ||
|
||
<div class="mb-3 float-end"> | ||
<button type="submit" class="btn btn-outline-secondary">Create</button> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1>{{topic}}</h1> | ||
<h3>by: {{owner.username}}</h3> | ||
|
||
<p> | ||
{{description}} | ||
</p> | ||
|