mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-10 06:10:17 -04:00
Merge pull request #23 from cssgunc/backend-emmafoster-GEN-95-entities
Backend emmafoster gen 95 entities
This commit is contained in:
commit
b6a7f8201f
|
@ -1,2 +1,10 @@
|
|||
from .entity_base import EntityBase
|
||||
from .sample_entity import SampleEntity
|
||||
from .tag_entity import TagEntity
|
||||
from .user_entity import UserEntity
|
||||
from .resource_entity import ResourceEntity
|
||||
from .resource_tag_entity import ResourceTagEntity
|
||||
from .service_entity import ServiceEntity
|
||||
from .service_tag_entity import ServiceTagEntity
|
||||
from .program_enum import ProgramEnum
|
||||
from .user_enum import RoleEnum
|
||||
|
|
10
backend/entities/program_enum.py
Normal file
10
backend/entities/program_enum.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from sqlalchemy import Enum
|
||||
|
||||
|
||||
class ProgramEnum(Enum):
|
||||
ECONOMIC = "economic"
|
||||
DOMESTIC = "domestic"
|
||||
COMMUNITY = "community"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(name="program_enum")
|
68
backend/entities/resource_entity.py
Normal file
68
backend/entities/resource_entity.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
""" Defines the table for storing resources """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import Integer, String, DateTime
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
# Import datetime for created_at type
|
||||
from datetime import datetime
|
||||
|
||||
# Import self for to model
|
||||
from typing import Self
|
||||
from backend.entities.program_enum import ProgramEnum
|
||||
|
||||
|
||||
class ResourceEntity(EntityBase):
|
||||
|
||||
# set table name
|
||||
__tablename__ = "resource"
|
||||
|
||||
# set fields
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now)
|
||||
name: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
summary: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
link: Mapped[str] = mapped_column(String, nullable=False)
|
||||
program: Mapped[ProgramEnum] = mapped_column(ProgramEnum, nullable=False)
|
||||
|
||||
# relationships
|
||||
resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(
|
||||
back_populates="resource", cascade="all,delete"
|
||||
)
|
||||
|
||||
#
|
||||
# @classmethod
|
||||
# def from_model(cls, model: user_model) -> Self:
|
||||
# """
|
||||
# Create a UserEntity from a User model.
|
||||
|
||||
# Args:
|
||||
# model (User): The model to create the entity from.
|
||||
|
||||
# Returns:
|
||||
# Self: The entity (not yet persisted).
|
||||
# """
|
||||
|
||||
# return cls (
|
||||
# id = model.id,
|
||||
# created_at = model.created_at,
|
||||
# name = model.name,
|
||||
# summary = model.summary,
|
||||
# link = model.link,
|
||||
# program = model.program,
|
||||
# )
|
||||
|
||||
# def to_model(self) -> user_model:
|
||||
# return user_model (
|
||||
# id = self.id,
|
||||
# created_at = self.created_at,
|
||||
# name = self.name,
|
||||
# summary = self.summary,
|
||||
# link = self.link,
|
||||
# program = self.program,
|
||||
# )
|
46
backend/entities/resource_tag_entity.py
Normal file
46
backend/entities/resource_tag_entity.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
""" Defines the table for resource tags """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import ForeignKey, Integer, String, DateTime
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
# Import datetime for created_at type
|
||||
from datetime import datetime
|
||||
|
||||
# Import self for to model
|
||||
from typing import Self
|
||||
|
||||
|
||||
class ResourceTagEntity(EntityBase):
|
||||
|
||||
# set table name to user in the database
|
||||
__tablename__ = "resourceTag"
|
||||
|
||||
# set fields or 'columns' for the user table
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
resourceId: Mapped[int] = mapped_column(ForeignKey("resource.id"))
|
||||
tagId: Mapped[int] = mapped_column(ForeignKey("tag.id"))
|
||||
|
||||
# relationships
|
||||
resource: Mapped["ResourceEntity"] = relationship(back_populates="resourceTags")
|
||||
tag: Mapped["TagEntity"] = relationship(back_populates="resourceTags")
|
||||
|
||||
# @classmethod
|
||||
# def from_model (cls, model: resource_tag_model) -> Self:
|
||||
# return cls (
|
||||
# id = model.id,
|
||||
# resourceId = model.resourceId,
|
||||
# tagId = model.tagId,
|
||||
# )
|
||||
|
||||
# def to_model (self) -> resource_tag_model:
|
||||
# return user_model(
|
||||
# id = self.id,
|
||||
# resourceId = self.resourceId,
|
||||
# tagId = self.tagId,
|
||||
# )
|
44
backend/entities/service_entity.py
Normal file
44
backend/entities/service_entity.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
""" Defines the table for storing services """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import Integer, String, DateTime, ARRAY
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
# Import datetime for created_at type
|
||||
from datetime import datetime
|
||||
|
||||
# Import enums for Program
|
||||
import enum
|
||||
from sqlalchemy import Enum
|
||||
|
||||
|
||||
class ProgramEnum(enum.Enum):
|
||||
"""Determine program for Service"""
|
||||
|
||||
DOMESTIC = "DOMESTIC"
|
||||
ECONOMIC = "ECONOMIC"
|
||||
COMMUNITY = "COMMUNITY"
|
||||
|
||||
|
||||
class ServiceEntity(EntityBase):
|
||||
|
||||
# set table name
|
||||
__tablename__ = "service"
|
||||
|
||||
# set fields
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now)
|
||||
name: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
summary: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
requirements: Mapped[list[str]] = mapped_column(ARRAY(String))
|
||||
program: Mapped[ProgramEnum] = mapped_column(Enum(ProgramEnum), nullable=False)
|
||||
|
||||
# relationships
|
||||
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(
|
||||
back_populates="service", cascade="all,delete"
|
||||
)
|
25
backend/entities/service_tag_entity.py
Normal file
25
backend/entities/service_tag_entity.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
""" Defines the table for service tags """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import ForeignKey, Integer
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
|
||||
class ServiceTagEntity(EntityBase):
|
||||
|
||||
# set table name to user in the database
|
||||
__tablename__ = "serviceTag"
|
||||
|
||||
# set fields or 'columns' for the user table
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
serviceId: Mapped[int] = mapped_column(ForeignKey("service.id"))
|
||||
tagId: Mapped[int] = mapped_column(ForeignKey("tag.id"))
|
||||
|
||||
# relationships
|
||||
service: Mapped["ServiceEntity"] = relationship(back_populates="resourceTags")
|
||||
tag: Mapped["TagEntity"] = relationship(back_populates="resourceTags")
|
62
backend/entities/tag_entity.py
Normal file
62
backend/entities/tag_entity.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
""" Defines the table for storing tags """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import Integer, String, DateTime
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
# Import datetime for created_at type
|
||||
from datetime import datetime
|
||||
|
||||
class TagEntity(EntityBase):
|
||||
|
||||
#set table name
|
||||
__tablename__ = "tag"
|
||||
|
||||
#set fields
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now)
|
||||
content: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
|
||||
#relationships
|
||||
resourceTags: Mapped[list["ResourceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete")
|
||||
serviceTags: Mapped[list["ServiceTagEntity"]] = relationship(back_populates="tag", cascade="all,delete")
|
||||
|
||||
|
||||
"""
|
||||
@classmethod
|
||||
def from_model(cls, model: Tag) -> Self:
|
||||
|
||||
Create a user entity from model
|
||||
|
||||
Args: model (User): the model to create the entity from
|
||||
|
||||
Returns:
|
||||
self: The entity
|
||||
|
||||
|
||||
return cls(
|
||||
id=model.id,
|
||||
content=model.id,
|
||||
)
|
||||
|
||||
def to_model(self) -> Tag:
|
||||
|
||||
Create a user model from entity
|
||||
|
||||
Returns:
|
||||
User: A User model for API usage
|
||||
|
||||
|
||||
return Tag(
|
||||
id=self.id,
|
||||
content=self.id,
|
||||
)
|
||||
|
||||
"""
|
||||
|
||||
|
90
backend/entities/user_entity.py
Normal file
90
backend/entities/user_entity.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
""" Defines the table for storing users """
|
||||
|
||||
# Import our mapped SQL types from SQLAlchemy
|
||||
from sqlalchemy import Integer, String, DateTime, ARRAY
|
||||
|
||||
|
||||
# Import mapping capabilities from the SQLAlchemy ORM
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
|
||||
# Import the EntityBase that we are extending
|
||||
from .entity_base import EntityBase
|
||||
|
||||
|
||||
# Import datetime for created_at type
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# Import enums for Role and Program
|
||||
from backend.entities.program_enum import ProgramEnum
|
||||
from .user_enum import RoleEnum
|
||||
|
||||
|
||||
class UserEntity(EntityBase):
|
||||
"""Serves as the database model for User table"""
|
||||
|
||||
# set table name to user in the database
|
||||
__tablename__ = "user"
|
||||
|
||||
# set fields or 'columns' for the user table
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now)
|
||||
username: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="", unique=True
|
||||
)
|
||||
role: Mapped[RoleEnum] = mapped_column(RoleEnum, nullable=False)
|
||||
username: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="", unique=True
|
||||
)
|
||||
role: Mapped[RoleEnum] = mapped_column(RoleEnum, nullable=False)
|
||||
email: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
|
||||
program: Mapped[list[ProgramEnum]] = mapped_column(
|
||||
ARRAY(ProgramEnum), nullable=False
|
||||
)
|
||||
program: Mapped[list[ProgramEnum]] = mapped_column(
|
||||
ARRAY(ProgramEnum), nullable=False
|
||||
)
|
||||
experience: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
group: Mapped[str] = mapped_column(String(50))
|
||||
|
||||
"""
|
||||
@classmethod
|
||||
def from_model(cls, model: User) -> Self:
|
||||
|
||||
Create a user entity from model
|
||||
|
||||
Args: model (User): the model to create the entity from
|
||||
|
||||
Returns:
|
||||
self: The entity
|
||||
|
||||
|
||||
return cls(
|
||||
id=model.id,
|
||||
username=model.username,
|
||||
role=model.role,
|
||||
email=model.email,
|
||||
program=model.program,
|
||||
experience=model.experience,
|
||||
group=model.group,
|
||||
)
|
||||
|
||||
def to_model(self) -> User:
|
||||
|
||||
Create a user model from entity
|
||||
|
||||
Returns:
|
||||
User: A User model for API usage
|
||||
|
||||
|
||||
return User(
|
||||
id=self.id,
|
||||
username=self.id,
|
||||
role=self.role,
|
||||
email=self.email,
|
||||
program=self.program,
|
||||
experience=self.experience,
|
||||
group=self.group,
|
||||
)
|
||||
"""
|
12
backend/entities/user_enum.py
Normal file
12
backend/entities/user_enum.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from sqlalchemy import Enum
|
||||
|
||||
|
||||
class RoleEnum(Enum):
|
||||
"""Determine role for User"""
|
||||
|
||||
ADMIN = "ADMIN"
|
||||
EMPLOYEE = "EMPLOYEE"
|
||||
VOLUNTEER = "VOLUNTEER"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(name="role_enum")
|
19
backend/test/entities/tag_entity_test.py
Normal file
19
backend/test/entities/tag_entity_test.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
""" Testing Tag Entity """
|
||||
|
||||
from sqlalchemy import Engine
|
||||
from ... import entities
|
||||
from ...entities.tag_entity import TagEntity
|
||||
|
||||
|
||||
def test_add_sample_data_tag(session: Engine):
|
||||
|
||||
"""Inserts a sample data point and verifies it is in the database"""
|
||||
entity = TagEntity(content="Test tag")
|
||||
session.add(entity)
|
||||
session.commit()
|
||||
data = session.get(TagEntity, 1)
|
||||
assert data.id == 1
|
||||
assert data.content == "Test tag"
|
||||
|
||||
|
||||
|
24
backend/test/entities/user_entity_test.py
Normal file
24
backend/test/entities/user_entity_test.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
""" Testing User Entity """
|
||||
|
||||
from sqlalchemy import Engine
|
||||
from ... import entities
|
||||
from ...entities.user_entity import UserEntity
|
||||
from ...entities.user_entity import RoleEnum
|
||||
from ...entities.user_entity import ProgramEnum
|
||||
|
||||
def test_add_sample_data_user(session: Engine):
|
||||
|
||||
|
||||
"""Inserts a sample data point and verifies it is in the database"""
|
||||
entity = UserEntity(id=1, username="emmalynf", role=RoleEnum.ADMIN, email="efoster@unc.edu", program=[ProgramEnum.COMMUNITY, ProgramEnum.DOMESTIC, ProgramEnum.ECONOMIC], experience=10, group="group")
|
||||
session.add(entity)
|
||||
session.commit()
|
||||
data = session.get(UserEntity, 1)
|
||||
assert data.id == 1
|
||||
assert data.username == "emmalynf"
|
||||
assert data.email == "efoster@unc.edu"
|
||||
assert data.experience == 10
|
||||
assert data.role == RoleEnum.ADMIN
|
||||
assert data.program == [ProgramEnum.COMMUNITY, ProgramEnum.DOMESTIC, ProgramEnum.ECONOMIC]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user