diff --git a/app/handlers/booking.py b/app/handlers/booking.py index eae1d1f..057c212 100644 --- a/app/handlers/booking.py +++ b/app/handlers/booking.py @@ -7,7 +7,11 @@ from aiogram.types import Message, CallbackQuery from app.keyboards.services import get_services_keyboard from app.keyboards.masters import get_masters_keyboard from app.states.booking import BookingState -from app.services.booking_service import create_booking +from app.services.booking_service import ( + create_booking, + update_booking, + get_busy_times +) from app.keyboards.menu import menu, cancel_menu from app.keyboards.calendar import get_dates_keyboard from app.keyboards.time_slots import get_time_keyboard @@ -121,13 +125,22 @@ async def choose_date( date=selected_date ) + data = await state.get_data() + + busy_times = get_busy_times( + master=data["master"], + date=selected_date + ) + await state.set_state( BookingState.time ) await callback.message.edit_text( "🕒 Выберите время:", - reply_markup=get_time_keyboard() + reply_markup=get_time_keyboard( + busy_times + ) ) await callback.answer() @@ -179,18 +192,38 @@ async def confirm_booking_callback( ): data = await state.get_data() - create_booking( - user_id=callback.from_user.id, - service=data["service"], - date=data["date"], - master=data["master"], - time=data["time"] + editing_booking_id = data.get( + "editing_booking_id" ) + if editing_booking_id: + + update_booking( + booking_id=editing_booking_id, + service=data["service"], + master=data["master"], + date=data["date"], + time=data["time"] + ) + + result_text = "✅ Запись успешно обновлена" + + else: + + create_booking( + user_id=callback.from_user.id, + service=data["service"], + master=data["master"], + date=data["date"], + time=data["time"] + ) + + result_text = "✅ Запись успешно создана" + await state.clear() await callback.message.edit_text( - "✅ Запись успешно создана" + result_text ) await callback.answer() diff --git a/app/handlers/mybookings.py b/app/handlers/mybookings.py index 13bc8a8..6f69a63 100644 --- a/app/handlers/mybookings.py +++ b/app/handlers/mybookings.py @@ -1,32 +1,99 @@ from aiogram import Router, F -from aiogram.types import Message +from aiogram.types import ( + Message, + CallbackQuery +) + from aiogram.fsm.context import FSMContext -from app.db.memory import bookings +from app.states.booking import BookingState +from app.keyboards.services import get_services_keyboard + +from app.services.booking_service import ( + get_user_bookings, + cancel_booking, +) + +from app.keyboards.my_booking import ( + get_booking_actions +) router = Router() -@router.message(F.text == "📖 Мои записи") -async def my_bookings(message: Message, state: FSMContext): - await state.clear() - user_id = message.from_user.id +@router.message( + F.text == "📖 Мои записи" +) +async def my_bookings( + message: Message +): + bookings = get_user_bookings( + message.from_user.id + ) - user_bookings = [ - b for b in bookings if b["user_id"] == user_id - ] - - if not user_bookings: - await message.answer("У вас нет записей") + if not bookings: + await message.answer( + "У вас нет активных записей" + ) return - text = "📖 Ваши записи:\n\n" - - for b in user_bookings: - text += ( - f"💆 {b['service']}\n" - f"📅 {b['date']}\n" - f"🕒 {b['time']}\n\n" + for booking in bookings: + await message.answer( + f"💆 {booking['service']}\n" + f"👤 {booking['master']}\n" + f"📅 {booking['date']}\n" + f"🕒 {booking['time']}", + reply_markup=get_booking_actions( + booking["id"] + ) ) - await message.answer(text) \ No newline at end of file + +@router.callback_query( + F.data.startswith( + "cancel_booking_" + ) +) +async def cancel_booking_handler( + callback: CallbackQuery +): + booking_id = int( + callback.data.split("_")[-1] + ) + + cancel_booking( + booking_id + ) + + await callback.message.edit_text( + "❌ Запись отменена" + ) + + await callback.answer() + +@router.callback_query( + F.data.startswith("edit_booking_") +) +async def edit_booking_handler( + callback: CallbackQuery, + state: FSMContext +): + + booking_id = int( + callback.data.split("_")[-1] + ) + + await state.update_data( + editing_booking_id=booking_id + ) + + await state.set_state( + BookingState.service + ) + + await callback.message.edit_text( + "✏️ Выберите новую услугу:", + reply_markup=get_services_keyboard() + ) + + await callback.answer() \ No newline at end of file diff --git a/app/keyboards/my_booking.py b/app/keyboards/my_booking.py new file mode 100644 index 0000000..c161135 --- /dev/null +++ b/app/keyboards/my_booking.py @@ -0,0 +1,25 @@ +from aiogram.types import ( + InlineKeyboardMarkup, + InlineKeyboardButton +) + + +def get_booking_actions( + booking_id: int +): + return InlineKeyboardMarkup( + inline_keyboard=[ + [ + InlineKeyboardButton( + text="✏️ Изменить", + callback_data=f"edit_booking_{booking_id}" + ) + ], + [ + InlineKeyboardButton( + text="❌ Отменить", + callback_data=f"cancel_booking_{booking_id}" + ) + ] + ] + ) \ No newline at end of file diff --git a/app/keyboards/time_slots.py b/app/keyboards/time_slots.py index 9dc16e1..b93663c 100644 --- a/app/keyboards/time_slots.py +++ b/app/keyboards/time_slots.py @@ -1,7 +1,13 @@ -from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from aiogram.types import InlineKeyboardMarkup +from aiogram.types import InlineKeyboardButton -def get_time_keyboard(): +def get_time_keyboard( + busy_times=None +): + + if busy_times is None: + busy_times = [] times = [ "10:00", @@ -10,17 +16,22 @@ def get_time_keyboard(): "13:00", "14:00", "15:00", - "16:00" + "16:00", + "17:00" ] buttons = [] - for t in times: + for time in times: + + if time in busy_times: + continue + buttons.append( [ InlineKeyboardButton( - text=t, - callback_data=f"time_{t}" + text=time, + callback_data=f"time_{time}" ) ] ) diff --git a/app/main.py b/app/main.py index 614b1b3..8ce7621 100644 --- a/app/main.py +++ b/app/main.py @@ -10,7 +10,11 @@ from app.handlers.profile import router as profile_router from app.handlers.mybookings import router as mybooking_router from app.handlers.about import router as about_router from app.handlers.contacts import router as contacts_router - +from app.services.booking_service import ( + create_booking, + update_booking, + get_busy_times +) async def main(): bot = Bot(token=BOT_TOKEN) diff --git a/app/services/booking_service.py b/app/services/booking_service.py index 6dbee9a..fe6de45 100644 --- a/app/services/booking_service.py +++ b/app/services/booking_service.py @@ -2,13 +2,14 @@ from app.db.memory import bookings def create_booking( - user_id: int, - service: str, - date: str, - master:str, - time: str + user_id: int, + service: str, + date: str, + master: str, + time: str ): booking = { + "id": len(bookings) + 1, "user_id": user_id, "service": service, "date": date, @@ -19,4 +20,73 @@ def create_booking( bookings.append(booking) - return booking \ No newline at end of file + return booking + + +def get_busy_times( + master: str, + date: str +): + busy_times = [] + + for booking in bookings: + + if ( + booking["master"] == master + and booking["date"] == date + and booking["status"] == "active" + ): + busy_times.append( + booking["time"] + ) + + return busy_times + + +def get_user_bookings( + user_id: int +): + result = [] + + for booking in bookings: + + if ( + booking["user_id"] == user_id + and booking["status"] == "active" + ): + result.append(booking) + + return result + +def cancel_booking( + booking_id: int +): + + for booking in bookings: + + if booking["id"] == booking_id: + booking["status"] = "cancelled" + return True + + return False + +def update_booking( + booking_id: int, + service: str, + master: str, + date: str, + time: str +): + + for booking in bookings: + + if booking["id"] == booking_id: + + booking["service"] = service + booking["master"] = master + booking["date"] = date + booking["time"] = time + + return booking + + return None