43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import os.path
|
|
|
|
from sqlalchemy.orm import Session
|
|
from models import Organization
|
|
|
|
|
|
class OrganizationService:
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
|
|
def create_organization(self, name: str, username: str, full_name: str, email: str, public_key: str) -> Organization:
|
|
from services import UserService
|
|
user = UserService(self.db).get_user_by_username(username)
|
|
if not user:
|
|
user = UserService(self.db).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
|
|
)
|
|
|
|
self.db.add(organization)
|
|
self.db.commit()
|
|
self.db.refresh(organization)
|
|
|
|
UserService(self.db).add_org_to_user(user, organization)
|
|
|
|
return organization
|
|
|
|
def get_organization(self, org_id: int) -> Organization | None:
|
|
return self.db.query(Organization).filter(Organization.id == org_id).first()
|
|
|
|
def get_organization_by_name(self, name: str) -> Organization | None:
|
|
return self.db.query(Organization).filter(Organization.name == name).first()
|
|
|
|
def list_organizations(self):
|
|
return self.db.query(Organization).all() |