mirror of
https://github.com/gmbrax/Pilgrim.git
synced 2026-11-09 17:20:17 +00:00
Added the photo_service.py and modified photo.py to have all the crud operations
This commit is contained in:
parent
8ad57b548f
commit
7e29a590a2
@@ -1,3 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
@@ -12,8 +14,16 @@ class Photo(Base):
|
||||
name = Column(String)
|
||||
addition_date = Column(String)
|
||||
caption = Column(String)
|
||||
entries = relationship(
|
||||
entries:relationship = relationship(
|
||||
"Entry",
|
||||
secondary=photo_entry_association,
|
||||
back_populates="photos"
|
||||
)
|
||||
def __init__(self, filepath, name, addition_date=None, caption=None, entries=None, **kw: Any):
|
||||
super().__init__(**kw)
|
||||
self.filepath = filepath
|
||||
self.name = name
|
||||
self.addition_date = addition_date
|
||||
self.caption = caption
|
||||
self.entries = entries
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from pilgrim import Photo
|
||||
|
||||
|
||||
class PhotoService:
|
||||
def __init__(self, session):
|
||||
self.session = session
|
||||
|
||||
def create(self, filepath:Path, name:str, addition_date=None, caption=None,) -> Photo:
|
||||
|
||||
new_photo = Photo(filepath, name, addition_date=addition_date, caption=caption)
|
||||
self.session.add(new_photo)
|
||||
self.session.commit()
|
||||
self.session.refresh(new_photo)
|
||||
|
||||
return new_photo
|
||||
def read_by_id(self, photo_id:int) -> Photo:
|
||||
return self.session.query(Photo).get(photo_id)
|
||||
|
||||
def read_all(self) -> List[Photo]:
|
||||
return self.session.query(Photo).all()
|
||||
|
||||
def update(self,photo_src:Photo,photo_dst:Photo) -> Photo | None:
|
||||
original:Photo = self.read_by_id(photo_src.id)
|
||||
if original:
|
||||
original.filepath = photo_dst.filepath
|
||||
original.name = photo_dst.name
|
||||
original.addition_date = photo_dst.addition_date
|
||||
original.caption = photo_dst.caption
|
||||
original.entries.extend(photo_dst.entries)
|
||||
self.session.commit()
|
||||
self.session.refresh(original)
|
||||
return original
|
||||
return None
|
||||
|
||||
def delete(self, photo_src:Photo) -> Photo | None:
|
||||
excluded = self.read_by_id(photo_src.id)
|
||||
if excluded:
|
||||
self.session.delete(excluded)
|
||||
self.session.commit()
|
||||
self.session.refresh(excluded)
|
||||
return excluded
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user