-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathcategories.py
More file actions
73 lines (64 loc) · 2.34 KB
/
categories.py
File metadata and controls
73 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from flask import Blueprint, jsonify, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from ..extensions import db
from ..models import Category
from ..services.memory_cache import invalidate_user_cache
bp = Blueprint("categories", __name__)
logger = logging.getLogger("finmind.categories")
@bp.get("")
@jwt_required()
def list_categories():
uid = int(get_jwt_identity())
items = (
db.session.query(Category).filter_by(user_id=uid).order_by(Category.name).all()
)
logger.info("List categories for user=%s count=%s", uid, len(items))
return jsonify([{"id": c.id, "name": c.name} for c in items])
@bp.post("")
@jwt_required()
def create_category():
uid = int(get_jwt_identity())
data = request.get_json() or {}
name = (data.get("name") or "").strip()
if not name:
logger.warning("Create category missing name user=%s", uid)
return jsonify(error="name required"), 400
# Optional: enforce unique name per user
exists = db.session.query(Category).filter_by(user_id=uid, name=name).first()
if exists:
return jsonify(error="category already exists"), 409
c = Category(user_id=uid, name=name)
db.session.add(c)
db.session.commit()
logger.info("Created category id=%s user=%s", c.id, uid)
invalidate_user_cache(uid)
return jsonify(id=c.id, name=c.name), 201
@bp.patch("/<int:category_id>")
@jwt_required()
def update_category(category_id: int):
uid = int(get_jwt_identity())
c = db.session.get(Category, category_id)
if not c or c.user_id != uid:
return jsonify(error="not found"), 404
data = request.get_json() or {}
name = (data.get("name") or "").strip()
if not name:
return jsonify(error="name required"), 400
c.name = name
db.session.commit()
logger.info("Updated category id=%s user=%s", c.id, uid)
invalidate_user_cache(uid)
return jsonify(id=c.id, name=c.name)
@bp.delete("/<int:category_id>")
@jwt_required()
def delete_category(category_id: int):
uid = int(get_jwt_identity())
c = db.session.get(Category, category_id)
if not c or c.user_id != uid:
return jsonify(error="not found"), 404
db.session.delete(c)
db.session.commit()
logger.info("Deleted category id=%s user=%s", c.id, uid)
invalidate_user_cache(uid)
return jsonify(message="deleted")