Skip to content

Commit

Permalink
add peatch model. add create peatch, bind peatch to owner (user)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiodor Gorobet committed Nov 21, 2022
1 parent 3942d91 commit 72e7f47
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 29 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ app.locals.appTitle = projectName;
require("./config/session.config")(app);

// 👇 Start handling routes here
app.use("/peatches", isLoggedIn, require("./routes/peatch.routes"));
app.use("/", require("./routes/index.routes"));
app.use("/", require("./routes/auth.routes"));
app.use("/", isLoggedIn, require("./routes/user.routes"));
Expand Down
2 changes: 2 additions & 0 deletions config/cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cloudinary.config({
api_secret: process.env.CD_API_SECRET,
});

// cloudinary.image(userpic, { width: 300, height: 300, crop: "fill" });

const storage = new CloudinaryStorage({
cloudinary,
params: {
Expand Down
7 changes: 3 additions & 4 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const MongoStore = require("connect-mongo");

const handlebars = require("express-handlebars");

// Connects the mongo uri to maintain the same naming structure
const MONGO_URI = process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/peatch-io";

// Middleware configuration
module.exports = (app) => {
// In development environment the app logs
Expand All @@ -53,8 +50,10 @@ module.exports = (app) => {
// Sets the view engine to handlebars
app.set("view engine", "hbs");
// Handles access to the public folder
app.use("/:username/profile", express.static(path.join(__dirname, "..", "/public")))
// app.use("/:username", express.static(path.join(__dirname, "..", "/public")))
app.use(express.static(path.join(__dirname, "..", "/public")));
app.use("/:username/profile", express.static(path.join(__dirname, "..", "/public")))
app.use("/peatches", express.static(path.join(__dirname, "..", "/public")))

// Handles access to the favicon
app.use(favicon(path.join(__dirname, "..", "public", "images", "favicon.ico")));
Expand Down
22 changes: 22 additions & 0 deletions models/Peatch.models.js
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);
20 changes: 20 additions & 0 deletions models/Proposal.js
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);
2 changes: 1 addition & 1 deletion models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { Schema, model } = require("mongoose");

// TODO: Please make sure you edit the User model to whatever makes sense in this case
const userSchema = new Schema(
{
username: {
Expand Down Expand Up @@ -35,6 +34,7 @@ const userSchema = new Schema(
country: String,
},
},
peatches: [{ type: Schema.Types.ObjectId, ref: "Peatch" }],
},
{
// this second object adds extra properties: `createdAt` and `updatedAt`
Expand Down
9 changes: 9 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ body {
.navbar-dashboard {
/* background: linear-gradient(90deg, rgba(63, 21, 30, 0.925), rgba(255, 194, 108, 0.938)); */
background-color: var(--color-main-dark);
box-shadow: 0 2px 5px gray;
}

.navbar-dashboard a {
Expand All @@ -84,6 +85,14 @@ body {
font-weight: 500;
}

.navbar-dashboard a:hover {
color: #fff;
}

.dropdown a {
color: #000;
}

.userpic-profile {
width: 20rem;
height: 20rem;
Expand Down
5 changes: 2 additions & 3 deletions routes/index.routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const express = require('express');
const router = express.Router();
const router = require("express").Router();

/* GET home page */
router.get("/", (req, res, next) => {
res.render("index", {
layout: "startpage",
isIndex: true,
user: req.session.user
user: req.session.user,
});
});

Expand Down
57 changes: 57 additions & 0 deletions routes/peatch.routes.js
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;
4 changes: 2 additions & 2 deletions routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { cloudinary, uploader } = require("../config/cloudinary");
router.get("/:username", (req, res, next) => {
res.render("user/dashboard", {
user: req.session.user,
isDashboard: true
});
});

Expand Down Expand Up @@ -37,6 +38,7 @@ router.get("/:username/profile", async (req, res, next) => {
userpicPath,
address: { street, houseNr, city, zipCode, country },
},
isProfile: true
});
});

Expand All @@ -47,8 +49,6 @@ router.post("/:username/profile", uploader.single("userpic"), async (req, res, n
const userpicPath = req.file.path;
const userpicPublicId = req.file.filename;

cloudinary.image(userpic, { width: 300, height: 300, crop: "fill" });

console.log(req.file);
try {
req.session.user = await User.findOneAndUpdate(
Expand Down
3 changes: 2 additions & 1 deletion views/layouts/main.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{>_header}}


{{>_navbar}}
{{>_navbar}}

<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
<div class="px-3">

Expand Down
34 changes: 16 additions & 18 deletions views/partials/_navbar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
</a>

<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="#" class="nav-link px-2">My Peatches</a></li>
<li><a href="/{{session.user.username}}" class="nav-link px-2">Dashboard</a></li>
{{#unless isDashboard}}
<li>
<a href="/{{session.user.username}}" class="nav-link px-2">Dashboard</a>
</li>
{{/unless}}
{{!-- <li><a href="/peatches" class="nav-link px-2">My Peatches</a></li> --}}
<li><a href="/peatches/new" class="nav-link px-2 {{#if isActive}}active{{/if}}">New Peatch</a></li>
</ul>

<div class="dropdown text-end">
<a
href="#"
class="d-block link-dark text-decoration-none dropdown-toggle"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<img
{{#if session.user.profile.userpicPath}}
src="{{session.user.profile.userpicPath}}"
<a href="#" class="d-block link-dark text-decoration-none dropdown-toggle" data-bs-toggle="dropdown"
aria-expanded="false">
<img
{{#if session.user.profile.userpicPath}}
src="{{session.user.profile.userpicPath}}"
{{else}}
src="https://picsum.photos/200"
{{/if}}
alt="{{session.user.username}}'' userpic"
width="32"
height="32"
class="rounded-circle"
/>
src="https://picsum.photos/200"
{{/if}}
alt="{{session.user.username}}'s userpic" width="32" height="32"
class="rounded-circle" />
</a>
<ul class="dropdown-menu text-small">
<li><a class="dropdown-item" href="#">New Peach</a></li>
Expand Down
1 change: 1 addition & 0 deletions views/peatches/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>All Peatches</h1>
17 changes: 17 additions & 0 deletions views/peatches/new.hbs
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>
7 changes: 7 additions & 0 deletions views/peatches/peatch.hbs
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>

0 comments on commit 72e7f47

Please sign in to comment.