Files
eamco_office_api/app/social/views.py
2024-10-24 11:51:14 -04:00

77 lines
2.2 KiB
Python

from flask import jsonify, request
import datetime
from app.social import social
from app import db
from app.classes.customer_social import (Customer_Customer_Social_schema,
Customer_Customer_Social)
@social.route("/posts/<int:customer_id>/<int:page>", methods=["GET"])
def get_customer_posts(customer_id, page):
per_page_amount = 50
if page is None:
offset_limit = 0
elif page == 1:
offset_limit = 0
else:
offset_limit = (per_page_amount * page) - per_page_amount
customer_posts = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.customer_id == customer_id)
.limit(per_page_amount).offset(offset_limit))
customer_social_schema = Customer_Customer_Social_schema(many=True)
return jsonify(customer_social_schema.dump(customer_posts))
@social.route("/create/<int:customer_id>", methods=["POST"])
def create_post(customer_id):
comment = request.json["comment"]
poster_employee_id = request.json["poster_employee_id"]
create_post = Customer_Customer_Social(
created = datetime.datetime.utcnow(),
customer_id = customer_id,
poster_employee_id = poster_employee_id,
comment = comment
)
db.session.add(create_post)
db.session.commit()
return jsonify({ "ok": True,}), 200
@social.route("/posts/<int:post_id>", methods=["PATCH"])
def edit_post(post_id):
customer_post = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.id == post_id)
.first())
comment = request.json["comment"]
customer_post.comment = comment
db.session.add(customer_post)
db.session.commit()
return jsonify({ "ok": True,}), 200
@social.route("/delete/<int:post_id>", methods=["DELETE"])
def delete_post(post_id):
customer_post = (db.session
.query(Customer_Customer_Social)
.filter(Customer_Customer_Social.id == post_id)
.first())
db.session.delete(customer_post)
db.session.commit()
return jsonify({ "ok": True,}), 200