Skip to content

Commit

Permalink
fixed update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rajnidua committed Sep 10, 2021
1 parent e97943a commit b35ad82
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 60 deletions.
4 changes: 3 additions & 1 deletion controllers/api/blogPostRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ router.delete("/:id", withAuth, async (req, res) => {
}
});

router.put("/:id", withAuth, async (req, res) => {
router.patch("/:id", withAuth, async (req, res) => {
try {
console.log("requestid" + req.params.id);
console.log("requestuserid" + req.session.user_id);
const updatedBlogpost = await Blogpost.update(
{
...req.body,
Expand Down
4 changes: 0 additions & 4 deletions public/js/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@ document
document
.querySelector(".project-list")
.addEventListener("click", delButtonHandler);

document
.querySelector(".project-list")
.addEventListener("click", updateButtonHandler);
11 changes: 6 additions & 5 deletions public/js/updateblogpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ const updateButtonHandler = async (event) => {
const summary = document.querySelector("#project-desc").value.trim();
const paramValue = document.querySelector("#pickId");
const post_id = paramValue.dataset.id1;

console.log("patch request");
const fetchURL = "/api/blogpost/" + post_id;
const responseURL = "/blogpost/" + post_id;
if (title) {
const response = await fetch(fetchURL, {
method: "PUT",
method: "PATCH",
body: JSON.stringify({ title, content, summary }),
headers: {
"Content-Type": "application/json",
},
});

if (response.ok) {
document.location.replace(fetchURL);
document.location.replace(responseURL);
} else {
alert("Something went wrong. Try updating blog post later");
console.log("Something went wrong. Try updating blog post later");
}
}
};

document
.querySelector(".update-blog-form")
.addEventListener("click", updateButtonHandler);
.addEventListener("submit", updateButtonHandler);
83 changes: 76 additions & 7 deletions views/blogpost.handlebars
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<div class="text-center">
{{!-- <div class="text-center">
<h2>{{title}}</h2>
<p>fill somethign</p>
<p>{{content}}</p>
<p>{{summary}}</p>
<p>Created by {{user.fname}} on {{format_date created_date}}</p>
<p>
<span for="img" aria-label="money">💲</span>
<span class="dollar-amount">try</span>
needed
</p>
{{#if updated_date}}
<p>Updated by {{user.fname}} on {{format_date updated_date}}</p>
{{/if}}
<div>
{{#if postcomments.length}}
<div class="col-md-6 project-list">
Expand Down Expand Up @@ -51,6 +49,77 @@
</form>
</div>
</p>
</div> --}}

{{! <script src="../js/postcomment.js"></script> }}

<div class="container container-fluid mt-5">

<div class="card text-right">
<div class="card-header">
<div class="row justify-space-between p-4">
<h5 class="card-title">{{title}}</h5>
<p>Created On: {{format_date created_date}}</p>

</div>
</div>

<div class="card-body">
<div class="row">
<div class="col col-md-3">
<h5 class="card-title text-left">{{title}}</h5>

</div>
<div class="col col-md-3 text-right">
<p class="card-text">{{content}}</p>
<p class="card-text">{{summary}}</p>

</div>
</div>
</div>
<div class="card-footer text-muted text-center">
<p>Created by :
{{user.fname}}</p>
</div>
</div>
<div class="card text-right">
<div class="card-header">
<div class="row justify-space-between p-4">
<h5 class="card-title text-center">COMMENT</h5>

</div>
</div>

<div class="card-body">
<div class="row">

<div class="col col-md-12 text-right">
<form class="form signup-form">
<div class="form-group">
<label for="name-signup"></label>
<input class="form-input" type="text" id="name-signup" />
</div>
<div>
<div id="pickId" data-id1="{{id}}" data-id2="{{user.id}}"></div>

</div>
<div class="form-group text-center">
<button class="btn btn-primary" type="submit">Post Comment</button>
</div>
</form>

</div>
</div>
</div>
<div class="card-footer text-muted text-right">
<p>posted by :
{{user.fname}}</p>
<p>posted on
{{format_date created_date}}
</p>
</div>
</div>

</div>

<script src="../js/postcomment.js"></script>
33 changes: 1 addition & 32 deletions views/homepage.handlebars
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
{{!-- <!-- Loop over each object in the projects array -->
{{#each Blogposts as |blogpost|}}
<div class="row mb-4 project">
<div class="col-md-5">
<h2>
<a href="/blogpost/{{blogpost.id}}">{{blogpost.title}}</a>
</h2>
<p>
<span for="img" aria-label="money"></span>
<!-- Pass needed_funding value to the helper function -->
<span class="dollar-amount"Need to add something here}</span>
needed!
</p>
<p>Created by
{{blogpost.user.fname}}
on
{{format_date created_date}}
</p>
</div>
<div class="col-md-7">
<p>
{{blogpost.content}}
</p>
</div>
</div>
{{/each}} --}}

{{#each Blogposts as |blogpost|}}
<div class="card text-right">
<div class="card-header">
Expand All @@ -42,8 +12,7 @@
<div class="row">
<div class="col col-md-3">
<h5 class="card-title text-left">{{blogpost.title}}</h5>
{{!-- <p>{{get_emoji}}</p> --}}
{{!-- <img src="{{get_emoji}}" alt="blog-image" /> --}}

</div>
<div class="col col-md-3 text-right">
<p class="card-text">{{blogpost.summary}}</p>
Expand Down
105 changes: 96 additions & 9 deletions views/profile.handlebars
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row">
{{!-- <div class="row">
<div class="col-auto">
<h2>Welcome, {{fname}}!</h2>
<h2>Welcome, {{user.fname}}!</h2>
</div>
</div>
Expand All @@ -19,7 +19,7 @@
/>
</div>
<div class="form-group">
<label for="project-funding">blog Content ($):</label>
<label for="project-funding">Blog Content:</label>
<input
class="form-input"
type="text"
Expand Down Expand Up @@ -54,12 +54,7 @@
href="/updateblogpost/{{blogpost.id}}"
class="btn btn-primary"
>Update</a>
{{!-- <div class="col-md-4">
<button
class="btn btn-sm btn-danger"
data-id="{{blogpost.id}}"
>UPDATE</button>
</div> --}}
</div>
<div class="row mb-2">
<div class="col-md-8">
Expand All @@ -77,4 +72,96 @@
{{/if}}
</div>
<script src="./js/profile.js"></script> --}}

<div class="row">
<div class="col-auto">
<h2>Welcome, {{user.fname}}!</h2>
</div>
</div>

<div class="row mt-4">
<div class="col-md-12">
<h3>Create a New Blog:</h3>

<form class="form new-project-form">
<div class="form-group">
<label for="project-name">Blog title:</label>
<input
class="form-input"
type="text"
id="project-name"
name="project-name"
/>
</div>
<div class="form-group">
<label for="project-funding">Blog Content:</label>
<input
class="form-input"
type="text"
id="project-funding"
name="project-funding"
/>
</div>
<div class="form-group">
<label for="project-desc">Summary:</label>
<textarea
class="form-input"
id="project-desc"
name="project-desc"
></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Create</button>
</div>
</form>
</div>
</div>
<div class="row mt-4">
<div class="col-md-12">
{{#if blogposts.length}}
<div class="col-md-12 project-list">
<h3>Current BlogPosts:</h3>

{{#each blogposts as |blogpost|}}
<div class="card">
<div class="row mb-2">
<div class="col-md-8">
<h4><a
href="/blogpost/{{blogpost.id}}"
>{{blogpost.title}}</a></h4>
</div>
<div class="col-md-4">
<a
href="/updateblogpost/{{blogpost.id}}"
class="btn btn-primary"
>Update</a>

<button
class="btn btn-sm btn-danger"
data-id="{{blogpost.id}}"
>DELETE</button>
</div>

</div>
</div>
{{!-- <div class="row mb-2">
<div class="col-md-8">
<h4><a
href="/blogpost/{{blogpost.id}}"
>{{blogpost.title}}</a></h4>
</div>
<div class="col-md-4">
<button
class="btn btn-sm btn-danger"
data-id="{{blogpost.id}}"
>DELETE</button>
</div>
</div> --}}
{{/each}}
</div>
{{/if}}
</div>
</div>

<script src="./js/profile.js"></script>
4 changes: 2 additions & 2 deletions views/updateblogpost.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
</div>
<script src="./js/updateblogpost.js"></script>
<script src="../js/updateblogpost.js"></script>

0 comments on commit b35ad82

Please sign in to comment.