-
Notifications
You must be signed in to change notification settings - Fork 6
Tool booking #449
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
Open
maffe03
wants to merge
4
commits into
main
Choose a base branch
from
tool-booking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tool booking #449
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from typing import Annotated | ||
| from api_schemas.tool_schema import SimpleToolRead | ||
| from api_schemas.user_schemas import SimpleUserRead | ||
| from helpers.types import datetime_utc | ||
| from pydantic import StringConstraints | ||
| from api_schemas.base_schema import BaseSchema | ||
| from helpers.constants import MAX_TOOL_BOOKING_DESC | ||
|
|
||
|
|
||
| class ToolBookingCreate(BaseSchema): | ||
| tool_id: int | ||
| amount: int | ||
| start_time: datetime_utc | ||
| end_time: datetime_utc | ||
| description: Annotated[str, StringConstraints(max_length=MAX_TOOL_BOOKING_DESC)] | ||
|
|
||
|
|
||
| class ToolBookingRead(BaseSchema): | ||
| id: int | ||
| tool: SimpleToolRead | ||
| amount: int | ||
| user: SimpleUserRead | ||
| start_time: datetime_utc | ||
| end_time: datetime_utc | ||
| description: str | ||
|
|
||
|
|
||
| class ToolBookingUpdate(BaseSchema): | ||
| amount: int | None = None | ||
| start_time: datetime_utc | None = None | ||
| end_time: datetime_utc | None = None | ||
| description: Annotated[str, StringConstraints(max_length=MAX_TOOL_BOOKING_DESC)] | None = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from api_schemas.base_schema import BaseSchema | ||
| from typing import Annotated | ||
| from pydantic import StringConstraints | ||
|
|
||
| from helpers.constants import MAX_TOOL_DESC | ||
|
|
||
|
|
||
| class ToolCreate(BaseSchema): | ||
| name_sv: str | ||
| name_en: str | ||
| amount: int | ||
| description_sv: Annotated[str, StringConstraints(max_length=MAX_TOOL_DESC)] | None = None | ||
| description_en: Annotated[str, StringConstraints(max_length=MAX_TOOL_DESC)] | None = None | ||
|
|
||
|
|
||
| class ToolRead(BaseSchema): | ||
| id: int | ||
| name_sv: str | ||
| name_en: str | ||
| amount: int | ||
| description_sv: str | None | ||
| description_en: str | None | ||
|
|
||
|
|
||
| class ToolUpdate(BaseSchema): | ||
| name_sv: str | ||
| name_en: str | ||
| amount: int | ||
| description_sv: Annotated[str, StringConstraints(max_length=MAX_TOOL_DESC)] | None = None | ||
| description_en: Annotated[str, StringConstraints(max_length=MAX_TOOL_DESC)] | None = None | ||
|
|
||
|
|
||
| class SimpleToolRead(BaseSchema): | ||
| id: int | ||
| amount: int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from helpers.constants import MAX_TOOL_BOOKING_DESC | ||
| from .base_model import BaseModel_DB | ||
| from sqlalchemy.orm import mapped_column, Mapped, relationship | ||
| from typing import TYPE_CHECKING, Optional | ||
| from sqlalchemy import ForeignKey, String | ||
| from helpers.types import datetime_utc | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .user_model import User_DB | ||
| from .tool_model import Tool_DB | ||
|
|
||
|
|
||
| class ToolBooking_DB(BaseModel_DB): | ||
| __tablename__ = "tool_booking_table" | ||
|
|
||
| id: Mapped[int] = mapped_column(primary_key=True, init=False) | ||
|
|
||
| amount: Mapped[int] = mapped_column() | ||
|
|
||
| start_time: Mapped[datetime_utc] = mapped_column() | ||
| end_time: Mapped[datetime_utc] = mapped_column() | ||
|
|
||
| tool_id: Mapped[int] = mapped_column(ForeignKey("tool_table.id")) | ||
| tool: Mapped["Tool_DB"] = relationship(back_populates="bookings", init=False) | ||
|
|
||
| user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("user_table.id")) | ||
| user: Mapped[Optional["User_DB"]] = relationship(back_populates="tool_bookings", init=False) | ||
|
|
||
| description: Mapped[Optional[str]] = mapped_column(String(MAX_TOOL_BOOKING_DESC), default=None) | ||
|
|
||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from helpers.constants import MAX_TOOL_NAME, MAX_TOOL_DESC | ||
| from .base_model import BaseModel_DB | ||
| from .tool_booking_model import ToolBooking_DB | ||
| from sqlalchemy.orm import mapped_column, Mapped, relationship | ||
| from typing import TYPE_CHECKING, Optional | ||
| from sqlalchemy import String, Integer | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .tool_booking_model import ToolBooking_DB | ||
|
|
||
|
|
||
| class Tool_DB(BaseModel_DB): | ||
| __tablename__ = "tool_table" | ||
|
|
||
| id: Mapped[int] = mapped_column(primary_key=True, init=False) | ||
|
|
||
| name_sv: Mapped[str] = mapped_column(String(MAX_TOOL_NAME)) | ||
| name_en: Mapped[str] = mapped_column(String(MAX_TOOL_NAME)) | ||
|
|
||
| amount: Mapped[int] = mapped_column(Integer) | ||
|
|
||
| bookings: Mapped[list["ToolBooking_DB"]] = relationship( | ||
| back_populates="tool", cascade="all, delete-orphan", init=False | ||
| ) | ||
|
|
||
| description_sv: Mapped[Optional[str]] = mapped_column(String(MAX_TOOL_DESC), default=None) | ||
| description_en: Mapped[Optional[str]] = mapped_column(String(MAX_TOOL_DESC), default=None) | ||
|
|
||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| from fastapi import APIRouter, HTTPException | ||
| from sqlalchemy import and_ | ||
| from api_schemas.tool_booking_schema import ( | ||
| ToolBookingCreate, | ||
| ToolBookingRead, | ||
| ToolBookingUpdate, | ||
| ) | ||
| from database import DB_dependency | ||
| from typing import Annotated | ||
| from db_models.tool_model import Tool_DB | ||
| from user.permission import Permission | ||
| from db_models.user_model import User_DB | ||
| from db_models.tool_booking_model import ToolBooking_DB | ||
| from helpers.types import datetime_utc | ||
| from services import tool_booking_service | ||
|
|
||
|
|
||
| tool_booking_router = APIRouter() | ||
|
|
||
|
|
||
| @tool_booking_router.post( | ||
| "/", response_model=ToolBookingRead, dependencies=[Permission.require("manage", "ToolBookings")] | ||
| ) | ||
| def create_tool_booking( | ||
| data: ToolBookingCreate, | ||
| current_user: Annotated[User_DB, Permission.require("manage", "ToolBookings")], | ||
| db: DB_dependency, | ||
| ): | ||
| tool = db.query(Tool_DB).filter(Tool_DB.id == data.tool_id).one_or_none() | ||
| if tool is None: | ||
| raise HTTPException(404, "Tool not found") | ||
|
|
||
| if data.amount <= 0: | ||
| raise HTTPException(400, "Amount must be positive") | ||
|
|
||
| if data.end_time <= data.start_time: | ||
| raise HTTPException(400, "End time must be after start time") | ||
|
|
||
| overlapping_bookings = ( | ||
| db.query(ToolBooking_DB) | ||
| .filter( | ||
| and_( | ||
| ToolBooking_DB.tool_id == data.tool_id, | ||
| ToolBooking_DB.start_time < data.end_time, | ||
| data.start_time < ToolBooking_DB.end_time, | ||
| ) | ||
| ) | ||
| .all() | ||
| ) | ||
|
|
||
| booked_amount = tool_booking_service.max_booked(overlapping_bookings) | ||
|
|
||
| if booked_amount + data.amount > tool.amount: | ||
| raise HTTPException(400, "Not enough tools available at that time") | ||
|
|
||
| tool_booking = ToolBooking_DB( | ||
| tool_id=data.tool_id, | ||
| amount=data.amount, | ||
| start_time=data.start_time, | ||
| end_time=data.end_time, | ||
| user_id=current_user.id, | ||
| description=data.description, | ||
| ) | ||
|
|
||
| db.add(tool_booking) | ||
|
|
||
| db.commit() | ||
|
|
||
| return tool_booking | ||
|
|
||
|
|
||
| @tool_booking_router.get( | ||
| "/get_booking/{booking_id}", | ||
| response_model=ToolBookingRead, | ||
| dependencies=[Permission.require("view", "ToolBookings")], | ||
| ) | ||
| def get_tool_booking(booking_id: int, db: DB_dependency): | ||
| booking = db.query(ToolBooking_DB).filter(ToolBooking_DB.id == booking_id).one_or_none() | ||
| if booking is None: | ||
| raise HTTPException(404, "Tool booking not found") | ||
| return booking | ||
|
|
||
|
|
||
| @tool_booking_router.get( | ||
| "/get_all", | ||
| response_model=list[ToolBookingRead], | ||
| dependencies=[Permission.require("view", "ToolBookings")], | ||
| ) | ||
| def get_all_tool_bookings(db: DB_dependency): | ||
| bookings = db.query(ToolBooking_DB).all() | ||
| return bookings | ||
|
|
||
|
|
||
| @tool_booking_router.get( | ||
| "/get_between_times", | ||
| response_model=list[ToolBookingRead], | ||
| dependencies=[Permission.require("view", "ToolBookings")], | ||
| ) | ||
| def get_tool_bookings_between_times(db: DB_dependency, start_time: datetime_utc, end_time: datetime_utc): | ||
| bookings = ( | ||
| db.query(ToolBooking_DB) | ||
| .filter(and_(ToolBooking_DB.start_time >= start_time, ToolBooking_DB.end_time <= end_time)) | ||
| .all() | ||
| ) | ||
| return bookings | ||
|
|
||
|
|
||
| @tool_booking_router.get( | ||
| "/get_by_tool/", | ||
| response_model=list[ToolBookingRead], | ||
| dependencies=[Permission.require("view", "ToolBookings")], | ||
| ) | ||
| def get_tool_bookings_by_tool(tool_id: int, db: DB_dependency): | ||
| tool = db.query(Tool_DB).filter(Tool_DB.id == tool_id).one_or_none() | ||
| if tool is None: | ||
| raise HTTPException(404, "Tool not found") | ||
| bookings = tool.bookings | ||
| return bookings | ||
|
|
||
|
|
||
| @tool_booking_router.delete( | ||
| "/{booking_id}", response_model=ToolBookingRead, dependencies=[Permission.require("manage", "ToolBookings")] | ||
| ) | ||
| def remove_tool_booking( | ||
| booking_id: int, | ||
| db: DB_dependency, | ||
| ): | ||
| booking = db.query(ToolBooking_DB).filter(ToolBooking_DB.id == booking_id).one_or_none() | ||
| if booking is None: | ||
| raise HTTPException(404, "Tool booking not found") | ||
|
|
||
| db.delete(booking) | ||
| db.commit() | ||
| return booking | ||
|
|
||
|
|
||
| @tool_booking_router.patch( | ||
| "/{booking_id}", response_model=ToolBookingRead, dependencies=[Permission.require("manage", "ToolBookings")] | ||
| ) | ||
| def update_tool_booking( | ||
| booking_id: int, | ||
| data: ToolBookingUpdate, | ||
| db: DB_dependency, | ||
| ): | ||
| tool_booking = db.query(ToolBooking_DB).filter(ToolBooking_DB.id == booking_id).one_or_none() | ||
| if tool_booking is None: | ||
| raise HTTPException(404, "Tool booking not found") | ||
|
|
||
| if data.start_time is None: | ||
| data.start_time = tool_booking.start_time | ||
| if data.end_time is None: | ||
| data.end_time = tool_booking.end_time | ||
| if data.end_time <= data.start_time: | ||
| raise HTTPException(400, "End time must be after start time") | ||
|
|
||
| if data.amount is not None: | ||
| if data.amount <= 0: | ||
| raise HTTPException(400, "Amount must be positive") | ||
|
|
||
| overlapping_bookings = ( | ||
| db.query(ToolBooking_DB) | ||
| .filter( | ||
| and_( | ||
| ToolBooking_DB.id != booking_id, | ||
| ToolBooking_DB.tool_id == tool_booking.tool_id, | ||
| ToolBooking_DB.start_time < data.end_time, | ||
| data.start_time < ToolBooking_DB.end_time, | ||
| ) | ||
| ) | ||
| .all() | ||
| ) | ||
|
|
||
| booked_amount = tool_booking_service.max_booked(overlapping_bookings) | ||
|
|
||
| if booked_amount + data.amount > tool_booking.tool.amount: | ||
| raise HTTPException(400, "Not enough tools available at that time") | ||
|
|
||
| for var, value in vars(data).items(): | ||
| setattr(tool_booking, var, value) if value else None | ||
|
|
||
| db.commit() | ||
| db.refresh(tool_booking) | ||
| return tool_booking |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amount is in SimpleToolRead too. Do we need both?