sio-2425/delivery1/server/services/orgs.py

45 lines
1.4 KiB
Python
Raw Normal View History

import os.path
from database import db
from models import Organization
class OrganizationService:
@staticmethod
def create_organization(name: str, username: str, full_name: str, email: str, public_key: str) -> Organization:
from services import UserService
user = UserService().get_user_by_username(username)
if not user:
user = UserService().create_user(username, full_name, email, public_key)
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
repos = os.path.join(project_root, "repository")
if not os.path.exists(os.path.join(repos, name)):
os.mkdir(os.path.join(repos, name))
organization = Organization(
name=name,
owner_id=user.id,
owner=user
)
db.add(organization)
db.commit()
db.refresh(organization)
UserService().add_org_to_user(user, organization)
UserService().add_public_key_to_user(user, organization, public_key)
return organization
@staticmethod
def get_organization(org_id: int) -> Organization | None:
return db.query(Organization).filter(Organization.id == org_id).first()
@staticmethod
def get_organization_by_name(name: str) -> Organization | None:
return db.query(Organization).filter(Organization.name == name).first()
@staticmethod
def list_organizations():
return db.query(Organization).all()