From 4cd172bee50e106c63b37faa0eb4b20a570e98f3 Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Wed, 4 Mar 2026 10:36:43 +0700 Subject: [PATCH] feat(booking-service): add shop-wide staff schedules endpoint Add GET /api/v1/schedules?shopId= to return all staff schedules for a shop. Existing per-staff endpoint unchanged. BFF needs this to display all schedules on the admin dashboard. Co-Authored-By: Claude Opus 4.6 --- .../Queries/GetSchedulesByShopQuery.cs | 11 +++++ .../Queries/GetSchedulesByShopQueryHandler.cs | 33 +++++++++++++ .../Controllers/SchedulesController.cs | 47 +++++++++++++++++++ .../Repositories/IStaffScheduleRepository.cs | 1 + .../Repositories/StaffScheduleRepository.cs | 9 ++++ 5 files changed, 101 insertions(+) create mode 100644 services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQuery.cs create mode 100644 services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQueryHandler.cs create mode 100644 services/booking-service-net/src/BookingService.API/Controllers/SchedulesController.cs diff --git a/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQuery.cs b/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQuery.cs new file mode 100644 index 00000000..a9da64bf --- /dev/null +++ b/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQuery.cs @@ -0,0 +1,11 @@ +// EN: Query to get all staff schedules for a shop. +// VI: Query lấy tất cả lịch làm việc nhân viên cho một shop. + +using BookingService.API.Application.DTOs; +using MediatR; + +namespace BookingService.API.Application.Queries; + +public record GetSchedulesByShopQuery( + Guid ShopId +) : IRequest>; diff --git a/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQueryHandler.cs b/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQueryHandler.cs new file mode 100644 index 00000000..3d7e8492 --- /dev/null +++ b/services/booking-service-net/src/BookingService.API/Application/Queries/GetSchedulesByShopQueryHandler.cs @@ -0,0 +1,33 @@ +// EN: Handler for GetSchedulesByShopQuery. +// VI: Handler cho GetSchedulesByShopQuery. + +using BookingService.API.Application.DTOs; +using BookingService.Infrastructure.Repositories; +using MediatR; + +namespace BookingService.API.Application.Queries; + +public class GetSchedulesByShopQueryHandler : IRequestHandler> +{ + private readonly IStaffScheduleRepository _staffScheduleRepository; + + public GetSchedulesByShopQueryHandler(IStaffScheduleRepository staffScheduleRepository) + { + _staffScheduleRepository = staffScheduleRepository ?? throw new ArgumentNullException(nameof(staffScheduleRepository)); + } + + public async Task> Handle(GetSchedulesByShopQuery request, CancellationToken cancellationToken) + { + var schedules = await _staffScheduleRepository.GetByShopIdAsync(request.ShopId, cancellationToken); + + return schedules.Select(s => new StaffScheduleDto + { + Id = s.Id, + StaffId = s.StaffId, + ShopId = s.ShopId, + DayOfWeek = s.DayOfWeek, + StartTime = s.StartTime, + EndTime = s.EndTime + }).ToList(); + } +} diff --git a/services/booking-service-net/src/BookingService.API/Controllers/SchedulesController.cs b/services/booking-service-net/src/BookingService.API/Controllers/SchedulesController.cs new file mode 100644 index 00000000..27af32d2 --- /dev/null +++ b/services/booking-service-net/src/BookingService.API/Controllers/SchedulesController.cs @@ -0,0 +1,47 @@ +// EN: Schedules Controller - Shop-wide staff schedule queries. +// VI: Controller Schedules - Truy vấn lịch làm việc nhân viên theo shop. + +using Asp.Versioning; +using BookingService.API.Application.DTOs; +using BookingService.API.Application.Queries; +using BookingService.API.Models.Responses; +using MediatR; +using Microsoft.AspNetCore.Mvc; + +namespace BookingService.API.Controllers; + +[ApiController] +[ApiVersion("1.0")] +[Route("api/v{version:apiVersion}/schedules")] +[Produces("application/json")] +public class SchedulesController : ControllerBase +{ + private readonly IMediator _mediator; + private readonly ILogger _logger; + + public SchedulesController(IMediator mediator, ILogger logger) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + /// + /// EN: Get all staff schedules for a shop. + /// VI: Lấy tất cả lịch làm việc nhân viên cho một shop. + /// + [HttpGet] + [ProducesResponseType(typeof(ApiResponse>), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task>>> GetSchedulesByShop( + [FromQuery] Guid shopId, + CancellationToken cancellationToken = default) + { + if (shopId == Guid.Empty) + return BadRequest(ApiResponse>.Fail("Shop ID is required")); + + var query = new GetSchedulesByShopQuery(shopId); + var result = await _mediator.Send(query, cancellationToken); + + return Ok(ApiResponse>.Ok(result)); + } +} diff --git a/services/booking-service-net/src/BookingService.Infrastructure/Repositories/IStaffScheduleRepository.cs b/services/booking-service-net/src/BookingService.Infrastructure/Repositories/IStaffScheduleRepository.cs index 273e7876..82d2099b 100644 --- a/services/booking-service-net/src/BookingService.Infrastructure/Repositories/IStaffScheduleRepository.cs +++ b/services/booking-service-net/src/BookingService.Infrastructure/Repositories/IStaffScheduleRepository.cs @@ -14,4 +14,5 @@ public interface IStaffScheduleRepository void Remove(StaffSchedule schedule); Task> GetByStaffIdAsync(Guid staffId, Guid shopId, CancellationToken cancellationToken = default); Task> GetByShopIdAndDayAsync(Guid shopId, int dayOfWeek, CancellationToken cancellationToken = default); + Task> GetByShopIdAsync(Guid shopId, CancellationToken cancellationToken = default); } diff --git a/services/booking-service-net/src/BookingService.Infrastructure/Repositories/StaffScheduleRepository.cs b/services/booking-service-net/src/BookingService.Infrastructure/Repositories/StaffScheduleRepository.cs index b45e7c92..802af07b 100644 --- a/services/booking-service-net/src/BookingService.Infrastructure/Repositories/StaffScheduleRepository.cs +++ b/services/booking-service-net/src/BookingService.Infrastructure/Repositories/StaffScheduleRepository.cs @@ -46,4 +46,13 @@ public class StaffScheduleRepository : IStaffScheduleRepository .Where(s => s.ShopId == shopId && s.DayOfWeek == dayOfWeek) .ToListAsync(cancellationToken); } + + public async Task> GetByShopIdAsync(Guid shopId, CancellationToken cancellationToken = default) + { + return await _context.StaffSchedules + .Where(s => s.ShopId == shopId) + .OrderBy(s => s.StaffId) + .ThenBy(s => s.DayOfWeek) + .ToListAsync(cancellationToken); + } }