mirror of
https://github.com/mskatoni/ni-mail.git
synced 2026-08-29 12:07:24 +08:00
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
from flask import Blueprint, jsonify, request
|
|
from app.db import db
|
|
from app.models import Account
|
|
|
|
bp = Blueprint("accounts", __name__, url_prefix="/api/accounts")
|
|
|
|
@bp.get("/")
|
|
def list_accounts():
|
|
accounts = Account.query.all()
|
|
return jsonify([a.to_dict() for a in accounts])
|
|
|
|
@bp.post("/")
|
|
def add_account():
|
|
data = request.json or {}
|
|
email = data.get("email")
|
|
if not email:
|
|
return jsonify({"error": "email is required"}), 400
|
|
|
|
acct = Account.query.filter_by(email=email).first()
|
|
if not acct:
|
|
acct = Account(email=email)
|
|
|
|
acct.method = data.get("method", "graph")
|
|
acct.client_id = data.get("client_id")
|
|
acct.refresh_token = data.get("refresh_token")
|
|
acct.password = data.get("password")
|
|
|
|
db.session.add(acct)
|
|
db.session.commit()
|
|
return jsonify(acct.to_dict()), 201
|
|
|
|
@bp.delete("/<int:acct_id>")
|
|
def delete_account(acct_id):
|
|
acct = Account.query.get_or_404(acct_id)
|
|
db.session.delete(acct)
|
|
db.session.commit()
|
|
return jsonify({"success": True})
|