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//", 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) .order_by(Customer_Customer_Social.id.desc()) .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/", 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/", 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/", 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