Skip to content

Commit

Permalink
A first calendar use case
Browse files Browse the repository at this point in the history
  • Loading branch information
horia141 committed Jul 9, 2024
1 parent 24d439c commit 0595792
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/cli/jupiter/cli/command/calendar_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Command for loading a calendar."""
from jupiter.cli.command.command import LoggedInReadonlyCommand
from jupiter.core.use_cases.application.calendar.load_for_date_and_period import (
CalendarLoadForDateAndPeriodResult,
CalendarLoadForDateAndPeriodUseCase,
)
from jupiter.core.use_cases.infra.use_cases import AppLoggedInReadonlyUseCaseContext
from rich.console import Console


class CalendarShow(
LoggedInReadonlyCommand[
CalendarLoadForDateAndPeriodUseCase, CalendarLoadForDateAndPeriodResult
]
):
"""Command for loading a calendar."""

def _render_result(
self,
console: Console,
context: AppLoggedInReadonlyUseCaseContext,
result: CalendarLoadForDateAndPeriodResult,
) -> None:
console.print(result)
1 change: 0 additions & 1 deletion src/cli/jupiter/cli/command/email_task_show.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""UseCase for showing the email tasks."""

from jupiter.cli.command.command import LoggedInReadonlyCommand
from jupiter.cli.command.rendering import (
actionable_date_to_rich_text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ScheduleEventFullDays(LeafEntity):

time_event_full_day_block = OwnsOne(
TimeEventFullDaysBlock,
namespace=TimeEventNamespace.SCHEDULE_FULL_DAY_BLOCK,
namespace=TimeEventNamespace.SCHEDULE_FULL_DAYS_BLOCK,
source_entity_ref_id=IsRefId(),
)
note = OwnsAtMostOne(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TimeEventFullDaysBlock(LeafSupportEntity):
source_entity_ref_id: EntityId
start_date: ADate
duration_days: int
end_date: ADate

@staticmethod
@create_entity_action
Expand All @@ -49,6 +50,7 @@ def new_time_event(
source_entity_ref_id=source_entity_ref_id,
start_date=start_date,
duration_days=duration_days,
end_date=start_date.add_days(duration_days),
)

@update_entity_action
Expand All @@ -62,14 +64,12 @@ def update(
if duration_days < 1:
raise InputValidationError("Duration must be at least 1 day.")
return self._new_version(
ctx, start_date=start_date, duration_days=duration_days
ctx,
start_date=start_date,
duration_days=duration_days,
end_date=start_date.add_days(duration_days),
)

@property
def end_date(self) -> ADate:
"""Get the end date."""
return self.start_date.add_days(self.duration_days)


class TimeEventFullDaysBlockRepository(
LeafEntityRepository[TimeEventFullDaysBlock], abc.ABC
Expand All @@ -83,3 +83,12 @@ async def load_for_namespace(
source_entity_ref_id: EntityId,
) -> TimeEventFullDaysBlock:
"""Load a time event full days block for a namespace and source entity."""

@abc.abstractmethod
async def find_all_between(
self,
parent_ref_id: EntityId,
start_date: ADate,
end_date: ADate,
) -> list[TimeEventFullDaysBlock]:
"""Find all time events in a range."""
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ async def load_for_namespace(
allow_archived: bool = False,
) -> TimeEventInDayBlock:
"""Load a time event for a namespace."""

@abc.abstractmethod
async def find_all_between(
self,
parent_ref_id: EntityId,
start_date: ADate,
end_date: ADate,
) -> list[TimeEventInDayBlock]:
"""Find all time events between two dates."""
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class TimeEventNamespace(EnumValue):
"""Time event namespaces."""

SCHEDULE_EVENT_IN_DAY = "schedule-event-in-day"
SCHEDULE_FULL_DAY_BLOCK = "schedule-full-day-block"
SCHEDULE_FULL_DAYS_BLOCK = "schedule-full-days-block"
INBOX_TASK = "inbox-task"
PERSON_BIRTHDAY = "person-birthday"
40 changes: 40 additions & 0 deletions src/core/jupiter/core/repository/sqlite/domain/core/time_events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sqltite implementation of the time events repository."""
from jupiter.core.domain.core.adate import ADate
from jupiter.core.domain.core.time_events.time_event_full_days_block import (
TimeEventFullDaysBlock,
TimeEventFullDaysBlockRepository,
Expand All @@ -14,6 +15,7 @@
from sqlalchemy import (
select,
)
from sqlalchemy.sql import and_, or_


class SqliteTimeEventInDayBlockRepository(
Expand Down Expand Up @@ -42,6 +44,20 @@ async def load_for_namespace(
)
return self._row_to_entity(result)

async def find_all_between(
self, parent_ref_id: EntityId, start_date: ADate, end_date: ADate
) -> list[TimeEventInDayBlock]:
"""Find all time events in day blocks between two dates."""
query_stmt = (
select(self._table)
.where(self._table.c.archived.is_(False))
.where(self._table.c.time_event_domain_ref_id == parent_ref_id.as_int())
.where(self._table.c.start_date >= start_date.the_date)
.where(self._table.c.start_date <= end_date.the_date)
)
result = await self._connection.execute(query_stmt)
return [self._row_to_entity(row) for row in result]


class SqliteTimeEventFullDaysBlockRepository(
SqliteLeafEntityRepository[TimeEventFullDaysBlock], TimeEventFullDaysBlockRepository
Expand All @@ -68,3 +84,27 @@ async def load_for_namespace(
f"Time event in full day block with namespace {namespace} and source {source_entity_ref_id} does not exist"
)
return self._row_to_entity(result)

async def find_all_between(
self, parent_ref_id: EntityId, start_date: ADate, end_date: ADate
) -> list[TimeEventFullDaysBlock]:
"""Find all time events in full day blocks between two dates."""
query_stmt = (
select(self._table)
.where(self._table.c.archived.is_(False))
.where(self._table.c.time_event_domain_ref_id == parent_ref_id.as_int())
.where(
or_(
and_(
self._table.c.start_date >= start_date.the_date,
self._table.c.start_date <= end_date.the_date,
),
and_(
self._table.c.end_date >= start_date.the_date,
self._table.c.end_date <= end_date.the_date,
),
)
)
)
result = await self._connection.execute(query_stmt)
return [self._row_to_entity(row) for row in result]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Use cases for the calendar application."""
Loading

0 comments on commit 0595792

Please sign in to comment.