1+ from __future__ import annotations
2+
13from datetime import datetime
4+ from typing import Dict , Any
25
3- from app .config import PSQL_ENVIRONMENT
4- from app .database .database import Base
56from sqlalchemy import (DDL , Boolean , Column , DateTime , ForeignKey , Index ,
6- Integer , String , event )
7+ Integer , String , event , UniqueConstraint )
78from sqlalchemy .dialects .postgresql import TSVECTOR
8- from sqlalchemy .orm import relationship
9+ from sqlalchemy .exc import IntegrityError , SQLAlchemyError
10+ from sqlalchemy .orm import relationship , Session
11+
12+ from app .config import PSQL_ENVIRONMENT
13+ from app .database .database import Base
14+ from app .dependencies import logger
915
1016
1117class UserEvent (Base ):
@@ -50,11 +56,12 @@ class Event(Base):
5056 end = Column (DateTime , nullable = False )
5157 content = Column (String )
5258 location = Column (String )
59+ color = Column (String , nullable = True )
5360
54- owner = relationship ("User" )
5561 owner_id = Column (Integer , ForeignKey ("users.id" ))
56- color = Column (String , nullable = True )
62+ category_id = Column (Integer , ForeignKey ( "categories.id" ) )
5763
64+ owner = relationship ("User" )
5865 participants = relationship ("UserEvent" , back_populates = "events" )
5966
6067 # PostgreSQL
@@ -64,12 +71,45 @@ class Event(Base):
6471 'events_tsv_idx' ,
6572 'events_tsv' ,
6673 postgresql_using = 'gin' ),
67- )
74+ )
6875
6976 def __repr__ (self ):
7077 return f'<Event { self .id } >'
7178
7279
80+ class Category (Base ):
81+ __tablename__ = "categories"
82+
83+ __table_args__ = (
84+ UniqueConstraint ('user_id' , 'name' , 'color' ),
85+ )
86+ id = Column (Integer , primary_key = True , index = True )
87+ name = Column (String , nullable = False )
88+ color = Column (String , nullable = False )
89+ user_id = Column (Integer , ForeignKey ("users.id" ), nullable = False )
90+
91+ @staticmethod
92+ def create (db_session : Session , name : str , color : str ,
93+ user_id : int ) -> Category :
94+ try :
95+ category = Category (name = name , color = color , user_id = user_id )
96+ db_session .add (category )
97+ db_session .flush ()
98+ db_session .commit ()
99+ db_session .refresh (category )
100+ except (SQLAlchemyError , IntegrityError ) as e :
101+ logger .error (f"Failed to create category: { e } " )
102+ raise e
103+ else :
104+ return category
105+
106+ def to_dict (self ) -> Dict [str , Any ]:
107+ return {c .name : getattr (self , c .name ) for c in self .__table__ .columns }
108+
109+ def __repr__ (self ) -> str :
110+ return f'<Category { self .id } { self .name } { self .color } >'
111+
112+
73113class PSQLEnvironmentError (Exception ):
74114 pass
75115
@@ -87,7 +127,7 @@ class PSQLEnvironmentError(Exception):
87127 Event .__table__ ,
88128 'after_create' ,
89129 trigger_snippet .execute_if (dialect = 'postgresql' )
90- )
130+ )
91131
92132
93133class Invitation (Base ):
0 commit comments