2022-08-10 01:04:19 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
from database.exceptions import NoResultFoundException
|
2022-08-29 20:24:42 +02:00
|
|
|
from database.schemas import Link
|
2022-08-10 01:04:19 +02:00
|
|
|
|
|
|
|
__all__ = ["add_link", "edit_link", "get_all_links", "get_link_by_name"]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_all_links(session: AsyncSession) -> list[Link]:
|
|
|
|
"""Get a list of all links"""
|
|
|
|
statement = select(Link)
|
2023-07-08 01:23:47 +02:00
|
|
|
return list((await session.execute(statement)).scalars().all())
|
2022-08-10 01:04:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def add_link(session: AsyncSession, name: str, url: str) -> Link:
|
|
|
|
"""Add a new link into the database"""
|
|
|
|
if name.islower():
|
|
|
|
name = name.capitalize()
|
|
|
|
|
|
|
|
instance = Link(name=name, url=url)
|
|
|
|
session.add(instance)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
async def get_link_by_name(session: AsyncSession, name: str) -> Optional[Link]:
|
|
|
|
"""Get a link by its name"""
|
|
|
|
statement = select(Link).where(func.lower(Link.name) == name.lower())
|
|
|
|
return (await session.execute(statement)).scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
async def edit_link(session: AsyncSession, name: str, new_url: str):
|
|
|
|
"""Edit an existing link"""
|
|
|
|
link: Optional[Link] = await get_link_by_name(session, name)
|
|
|
|
|
|
|
|
if link is None:
|
|
|
|
raise NoResultFoundException
|
|
|
|
|
|
|
|
link.url = new_url
|
|
|
|
session.add(link)
|
|
|
|
await session.commit()
|