Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get post and create post services and api routes #18

Merged
merged 9 commits into from
Mar 30, 2023
16 changes: 16 additions & 0 deletions backend/api/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fastapi import APIRouter, Depends
from ..services import PostService, UserService
from ..models import Post
from .authentication import registered_user

api = APIRouter(prefix="/api/post")


@api.post("", response_model=Post, tags=['Post'])
def create(post: Post, post_svc: PostService = Depends(), usr_svc: UserService = Depends()):
user_entity = usr_svc.findUser(post.user)
return post_svc.create(post,user_entity)

@api.get("", response_model=list[Post], tags=['Post'])
def getAll(post_svc: PostService = Depends()):
return post_svc.getAll()
1 change: 1 addition & 0 deletions backend/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .role_entity import RoleEntity
from .permission_entity import PermissionEntity
from .user_role_entity import user_role_table
from .post_entity import PostEntity


__authors__ = ["Kris Jordan"]
Expand Down
37 changes: 25 additions & 12 deletions backend/entities/post_entity.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
'''User accounts for all registered users in the application.'''


from sqlalchemy import Integer, String
from sqlalchemy import Integer, String, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import Self
from .entity_base import EntityBase
from .user_role_entity import user_role_table
from ..models import User
#from ..models import User
from ..models import Post

from fastapi import Depends
from .user_entity import UserEntity
from .post_votes_entity import post_votes_table
#from ..services import UserPostService

__authors__ = ['Kris Jordan']
__copyright__ = 'Copyright 2023'
__license__ = 'MIT'


class PostEntity(EntityBase):
__tablename__ = 'user'
__tablename__ = 'posts'

id: Mapped[int] = mapped_column(Integer, primary_key=True)
content: Mapped[str] = mapped_column(String(64), nullable=False, default='')
user: Mapped[User] = mapped_column(User, nullable=False, default='')
votes: Mapped[list[User]]

user_id: Mapped[int] = mapped_column(ForeignKey('user.id'))
user: Mapped[UserEntity] = relationship("UserEntity",back_populates='posts')

votes: Mapped[list[UserEntity]] = relationship(secondary=post_votes_table)

timestamp: Mapped[str] = mapped_column(String(64), nullable=False, default='')

@classmethod
def from_model(cls, model: Post) -> Self:
def from_model(cls, model: Post, user: UserEntity ) -> Self:
#user_svc: UserPostService = Depends()
return cls(
id=model.id,
content=model.content,
user=model.user,
votes=model.votes,
user = user,
votes= [],
timestamp=model.timestamp,

#user_svc.findUser(model.user)
#[user_svc.findUser(vote) for vote in model.votes]
)

def to_model(self) -> Post:
vote_num = [vote.to_model() for vote in self.votes]
return Post(
id=self.id,
content=self.content,
user=self.user,
votes=self.votes,
user=self.user.to_model(),
votes=vote_num,
timestamp=self.timestamp,
)

def update(self, model: Post) -> None:
self.content = model.content


9 changes: 9 additions & 0 deletions backend/entities/post_votes_entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from sqlalchemy import Table, Column, ForeignKey
from .entity_base import EntityBase

post_votes_table = Table(
"post_votes",
EntityBase.metadata,
Column('user_id', ForeignKey('user.id'), primary_key=True),
Column('post_id', ForeignKey('posts.id'), primary_key=True)
)
2 changes: 1 addition & 1 deletion backend/entities/role_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RoleEntity(EntityBase):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, unique=True)

users: Mapped[list['UserEntity']] = relationship(secondary=user_role_table, back_populates='roles')
users: Mapped[list['UserEntity']] = relationship(secondary=user_role_table)
permissions: Mapped[list['PermissionEntity']] = relationship(back_populates='role')

@classmethod
Expand Down
9 changes: 8 additions & 1 deletion backend/entities/user_entity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'''User accounts for all registered users in the application.'''


from sqlalchemy import Integer, String
from sqlalchemy import Integer, String, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import Self
from .entity_base import EntityBase
from .user_role_entity import user_role_table
from ..models import User
from .post_votes_entity import post_votes_table


__authors__ = ['Kris Jordan']
Expand All @@ -32,6 +33,12 @@ class UserEntity(EntityBase):

roles: Mapped[list['RoleEntity']] = relationship(secondary=user_role_table, back_populates='users')
permissions: Mapped['PermissionEntity'] = relationship(back_populates='user')

#create Secondary table for posts???
posts: Mapped[list['PostEntity']] = relationship(back_populates='user')

votes: Mapped[list['PostEntity']] = relationship(secondary=post_votes_table, back_populates='votes')


@classmethod
def from_model(cls, model: User) -> Self:
Expand Down
3 changes: 2 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Entrypoint of backend API exposing the FastAPI `app` to be served by an application server such as uvicorn."""

from fastapi import FastAPI
from .api import health, static_files, profile, authentication, user
from .api import health, static_files, profile, authentication, user, post
from .api.admin import users as admin_users
from .api.admin import roles as admin_roles

Expand All @@ -26,4 +26,5 @@
app.include_router(authentication.api)
app.include_router(admin_users.api)
app.include_router(admin_roles.api)
app.include_router(post.api)
app.mount("/", static_files.StaticFileMiddleware(directory="./static"))
6 changes: 6 additions & 0 deletions backend/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from .user import User, ProfileForm, NewUser
from .role import Role
from .role_details import RoleDetails
from .post import Post
# import sys
# sys.path.append("/workspace/backend/models")
# import



__authors__ = ["Kris Jordan"]
__copyright__ = "Copyright 2023"
Expand Down
8 changes: 5 additions & 3 deletions backend/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from pydantic import BaseModel
from . import User


class Post(BaseModel):
id: int | None = None
content: str
user: User
votes: list[User]
user: 'User'
votes: list['User'] = []
timestamp: str




4 changes: 3 additions & 1 deletion backend/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .user import UserService
from .permission import PermissionService, UserPermissionError
from .role import RoleService
from .role import RoleService
from .post import PostService
from .user_post import UserPostService
28 changes: 28 additions & 0 deletions backend/services/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from fastapi import Depends
from sqlalchemy import select, or_, func
from sqlalchemy.orm import Session
from ..database import db_session
from ..models import User, Role, RoleDetails, Permission, Post
from ..entities import RoleEntity, PermissionEntity, UserEntity
from ..entities.post_entity import PostEntity
from .permission import PermissionService, UserPermissionError


class PostService:

def __init__(self, session: Session = Depends(db_session), permission: PermissionService = Depends()):
self._session = session
self._permission = permission

def create(self, post: Post, user: UserEntity) -> Post:
entity = PostEntity.from_model(post,user)
self._session.add(entity)
self._session.commit()
return entity.to_model()
user_entity = user_svc.findUser(post.user)
return None

def getAll(self) -> list[Post]:
query = select(PostEntity)
entities = self._session.scalars(query).all()
return [entity.to_model() for entity in entities]
9 changes: 9 additions & 0 deletions backend/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ def search(self, _subject: User, query: str) -> list[User]:
statement = statement.where(criteria).limit(10)
entities = self._session.execute(statement).scalars()
return [entity.to_model() for entity in entities]

def findUser(self, _subject: User) -> UserEntity:
# query = select(UserEntity).filter_by(id=_subject.id)
query = select(UserEntity).where(UserEntity.pid == _subject.pid)
user_entity: UserEntity = self._session.execute(query).scalar()
return user_entity



def list(self, subject: User, pagination_params: PaginationParams) -> Paginated[User]:
"""List Users.
Expand Down Expand Up @@ -148,3 +156,4 @@ def update(self, subject: User, user: User) -> User:
entity.update(user)
self._session.commit()
return entity.to_model()

22 changes: 22 additions & 0 deletions backend/services/user_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi import Depends
from sqlalchemy import select, or_, func
from sqlalchemy.orm import Session
from ..database import db_session
from ..models import User, Paginated, PaginationParams
from ..entities import UserEntity
from .permission import PermissionService
from . import *

class UserPostService:

_session: Session
_permission: PermissionService

def findUser(self, _subject: User, user_svc: 'UserService' = Depends()) -> UserEntity:
return user_svc.findUser(_subject)