73 lines
2 KiB
Python
73 lines
2 KiB
Python
|
|
from flask_jwt_extended import jwt_required
|
||
|
|
from flask_pydantic import validate
|
||
|
|
|
||
|
|
from app import db
|
||
|
|
from app.decorators import load_card_owned, load_comment_owned
|
||
|
|
from app.models import Comment, User
|
||
|
|
from app.schemas import (CommentCreateRequest, CommentResponse,
|
||
|
|
CommentWithUserResponse)
|
||
|
|
|
||
|
|
from . import kanban_bp
|
||
|
|
|
||
|
|
|
||
|
|
@kanban_bp.route("/cards/<int:card_id>/comments", methods=["GET"])
|
||
|
|
@jwt_required()
|
||
|
|
@load_card_owned
|
||
|
|
def get_comments(card_id, card):
|
||
|
|
"""Get all comments for a card"""
|
||
|
|
comments = []
|
||
|
|
for comment in card.comments.order_by(Comment.created_at.desc()).all():
|
||
|
|
comment_dict = comment.to_dict()
|
||
|
|
user = db.session.get(User, comment.user_id)
|
||
|
|
comment_dict["user"] = user.to_dict() if user else None
|
||
|
|
response = CommentWithUserResponse(**comment_dict)
|
||
|
|
comments.append(response.model_dump())
|
||
|
|
|
||
|
|
return comments, 200
|
||
|
|
|
||
|
|
|
||
|
|
@kanban_bp.route("/cards/<int:card_id>/comments", methods=["POST"])
|
||
|
|
@jwt_required()
|
||
|
|
@load_card_owned
|
||
|
|
@validate(body=CommentCreateRequest)
|
||
|
|
def create_comment(card_id, card, body: CommentCreateRequest):
|
||
|
|
"""Create a new comment on a card"""
|
||
|
|
from app.decorators import get_current_user_id
|
||
|
|
|
||
|
|
user_id = get_current_user_id()
|
||
|
|
|
||
|
|
comment = Comment(
|
||
|
|
text=body.text,
|
||
|
|
card_id=card_id,
|
||
|
|
user_id=user_id,
|
||
|
|
)
|
||
|
|
|
||
|
|
db.session.add(comment)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return CommentResponse.model_validate(comment).model_dump(), 201
|
||
|
|
|
||
|
|
|
||
|
|
@kanban_bp.route("/comments/<int:comment_id>", methods=["PUT"])
|
||
|
|
@jwt_required()
|
||
|
|
@load_comment_owned
|
||
|
|
@validate(body=CommentCreateRequest)
|
||
|
|
def update_comment(comment_id, comment, body: CommentCreateRequest):
|
||
|
|
"""Update a comment"""
|
||
|
|
comment.text = body.text
|
||
|
|
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return CommentResponse.model_validate(comment).model_dump(), 200
|
||
|
|
|
||
|
|
|
||
|
|
@kanban_bp.route("/comments/<int:comment_id>", methods=["DELETE"])
|
||
|
|
@jwt_required()
|
||
|
|
@load_comment_owned
|
||
|
|
def delete_comment(comment_id, comment):
|
||
|
|
"""Delete a comment"""
|
||
|
|
db.session.delete(comment)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return {"message": "Comment deleted"}, 200
|