sio-2425/delivery1/server/routes/org.py

33 lines
1.0 KiB
Python

import json
from flask import Blueprint, request, jsonify
from services import OrganizationService
org_bp = Blueprint("org", __name__)
@org_bp.route("/create", methods=["POST"])
def org_create():
data = request.json
if type(data) is str:
data = json.loads(data)
if "name" not in data or "username" not in data or "full_name" not in data or "email" not in data or "public_key" not in data:
return jsonify({"error": "Missing required fields"}), 400
existing_org = OrganizationService.get_organization_by_name(data["name"])
if existing_org:
return jsonify({"error": "Organization already exists"}), 400
org = OrganizationService.create_organization(
name=data["name"],
username=data["username"],
full_name=data["full_name"],
email=data["email"],
public_key=data["public_key"]
)
return jsonify(org.to_dict()), 201
@org_bp.route("/list", methods=["GET"])
def org_list():
orgs = OrganizationService.list_organizations()
return jsonify([org.to_dict() for org in orgs])