[TIL] Toy Project Day 3 - Like Button Cont. & Delete Specific Card & AWS EC2 Deployment
03/29/23
Table of contents
Like Button
How to solve - jQuery id selector works only for the first row in the DB: https://stackoverflow.com/questions/11114622/jquery-id-selector-works-only-for-the-first-element.
How to solve - increase the number when the button clicked in jQuery
How to solve - async button
By using
$(this).html(function (idx, val) {return val})
# Read
@app.route("/store", methods=["GET"])
def store_get():
stores = list(db.stores.find())
for store in stores:
store['_id'] = str(store['_id'])
return jsonify({"stores": stores})
# Like button
@app.route("/like", methods=["POST"])
def like_update():
id_receive = request.form['id_give']
like = db.stores.find_one({"_id": ObjectId(id_receive)}, {"like": 1})
num_like = int(like["like"]) + 1
add_like = { '$set': {'like': num_like }}
db.stores.update_one({"_id": ObjectId(id_receive)}, add_like)
return jsonify({'msg': 'like is increased by 1'})
let temp_html = `<button type="button" class="btn like" value=${id}>👍 ${like}</button>`
$(".like").click(function () {
// Frontend: Increasing Like count on the page
$(this).html(function (i, val) {
return `👍 ${val.split(" ")[1] * 1 + 1}`;
});
// Backend: Sending Like count on th page to the DB
let formData = new FormData();
formData.append("id_give", this.value);
fetch("/like", { method: "POST", body: formData })
.then((response) => response.json())
.then((data) => {});
});
Delete Button
# Delete
@app.route("/store", methods=["DELETE"])
def store_delete():
id_receive = request.form['id_give']
db.stores.delete_one({"_id": ObjectId(id_receive)})
return jsonify({'msg': 'Store is successfully deleted!'})
let temp_html = `<button type="button" class="btn btn-dark delete" value=${id}>DELETE</button>`
$(".delete").click(function () {
let formData = new FormData();
formData.append("id_give", this.value);
fetch("/store", { method: "DELETE", body: formData })
.then((response) => response.json())
.then((data) => {
window.location.reload();
});
});
AWS EC2 Deployment
- Why using AWS EC2?
: “Fast and reliable cloud servers” is the major cause for having a greater number of developers who prefer using Amazon EC2, yet a lower number of developers shed light on the feature of “Integrates with a variety of AWS services” as the number one reason behind going with the AWS Elastic Beanstalk.
AWS EC2 > Instances
Launch an instance > Name and tags > Application and OS Images: Ubuntu > Instance type: t2.micro > Key pair: Create new key pair: Key pair name, RSA, .pem > Download .pem file > Network settings: Auto-assign public IP - Enable > Launch
Copy Public IPv4 address
Open FileZilla
Once connected successfully, it will show like this
Create a directory and move all files from the Local site to the Remote site (do not include
venv
, only the source codes) (or we can just dogit clone <remote repo url>
)Open bash > enter the following command:
ssh -i <.pem key address> ubuntu@<Public IPv4 address>
Open bash under the current directory as a directory we created > Enter the following command:
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install python3-pip
, then move to the directorypip install -r requirements.txt
Run the server:
python3 app.py
Open the website: Public IPv4 address + Port number (like http://100.25.193.224:5000/)
ERROR:
Edit Inbound Rules like the following screenshot:
Now, we are successfully able to open the deployed website