| | 1 | | using CoWorkingApp.Core.Application.Abstracts; |
| | 2 | | using CoWorkingApp.Core.Application.Contracts.Adapters; |
| | 3 | | using CoWorkingApp.Core.Application.Contracts.Repositories; |
| | 4 | | using CoWorkingApp.Core.Application.Contracts.Services; |
| | 5 | | using CoWorkingApp.Core.Domain.DTOs; |
| | 6 | | using CoWorkingApp.Core.Domain.Entities; |
| | 7 | |
|
| | 8 | | namespace CoWorkingApp.Core.Application.Services |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Implementación concreta del servicio para la entidad Seat. |
| | 12 | | /// </summary> |
| | 13 | | public class SeatService : ServiceGeneric<ISeatRepository, Seat, SeatRequest, SeatResponse>, ISeatService |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Constructor de la clase SeatService. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="seatRepository">El repositorio de asientos asociado al servicio.</param> |
| 152 | 19 | | public SeatService(ISeatRepository seatRepository, IMapperAdapter mapper) : base(seatRepository, mapper) { } |
| | 20 | |
|
| | 21 | | // Implementación de métodos específicos de ISeatService |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Obtiene todos los asientos disponibles. |
| | 25 | | /// </summary> |
| | 26 | | /// <returns>Una colección de respuestas de asientos disponibles.</returns> |
| | 27 | | public async Task<IEnumerable<SeatResponse>> GetAvailablesAsync() |
| 3 | 28 | | { |
| | 29 | | try |
| 3 | 30 | | { |
| | 31 | | // Obtener los asientos disponibles desde el repositorio |
| 3 | 32 | | var seatsAvailable = await _repository.GetAvailablesAsync(); |
| | 33 | |
|
| | 34 | | // Mapear los asientos disponibles a respuestas de asientos |
| 2 | 35 | | var responses = _mapper.Map<IEnumerable<Seat>, IEnumerable<SeatResponse>>(seatsAvailable).ToList(); |
| 5 | 36 | | responses.ForEach(responses => responses.Success = true); |
| 2 | 37 | | return responses; |
| | 38 | | } |
| 1 | 39 | | catch (Exception ex) |
| 1 | 40 | | { |
| | 41 | | // Manejar excepciones y lanzar un error genérico |
| 1 | 42 | | return new List<SeatResponse> { HandleException(ex) }; |
| | 43 | | } |
| 3 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Obtiene un asiento por su nombre. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="name">El nombre del asiento.</param> |
| | 50 | | /// <returns>Una respuesta de asiento correspondiente al nombre proporcionado.</returns> |
| | 51 | | public async Task<SeatResponse> GetByNameAsync(string name) |
| 9 | 52 | | { |
| | 53 | | try |
| 9 | 54 | | { |
| | 55 | | // Verificar si el nombre es nulo o vacío |
| 9 | 56 | | if (string.IsNullOrEmpty(name)) |
| 2 | 57 | | { |
| | 58 | | // Si es nulo o vacío, lanzar una excepción |
| 2 | 59 | | throw new ArgumentNullException("The name cannot be null or empty"); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | // Obtener el asiento por su nombre desde el repositorio. Si no, lanzar una excepción |
| 7 | 63 | | var seat = await _repository.GetByNameAsync(name) ?? throw new ArgumentException($"Seat {name} not found |
| | 64 | |
|
| | 65 | | // Mapear el asiento a una respuesta de asiento y establecer el éxito en verdadero |
| 4 | 66 | | var response = _mapper.Map<Seat, SeatResponse>(seat); |
| 1 | 67 | | response.Success = true; |
| 1 | 68 | | return response; |
| | 69 | | } |
| 2 | 70 | | catch (ArgumentNullException ex) |
| 2 | 71 | | { |
| | 72 | | // Manejar la excepción de argumento nulo y generar una respuesta de error |
| 2 | 73 | | return HandleException(ex); |
| | 74 | | } |
| 3 | 75 | | catch (ArgumentException ex) |
| 3 | 76 | | { |
| | 77 | | // Manejar la excepción de argumento inválido y generar una respuesta de error |
| 3 | 78 | | return HandleException(ex); |
| | 79 | | } |
| 3 | 80 | | catch (Exception) |
| 3 | 81 | | { |
| | 82 | | // Manejar excepciones inesperadas y generar una respuesta de error |
| 3 | 83 | | var exception = new Exception(); |
| 3 | 84 | | return HandleException(exception); |
| | 85 | | } |
| 9 | 86 | | } |
| | 87 | |
|
| | 88 | | // Implementación de métodos abstractos de ServiceGeneric |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// ACTualiza las propiedades de un asiento existente con los valores de una solicitud de asiento. |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="existingEntity">La entidad de asiento existente que se actualizará.</param> |
| | 94 | | /// <param name="request">La solicitud de asiento que contiene los nuevos valores.</param> |
| | 95 | | /// <returns>El asiento actualizado.</returns> |
| | 96 | | protected override Seat UpdateProperties(Seat existingEntity, SeatRequest request) |
| 13 | 97 | | { |
| | 98 | | // ACTualizar las propiedades del asiento existente con los valores de la solicitud |
| 13 | 99 | | return new Seat |
| 13 | 100 | | { |
| 13 | 101 | | Name = string.IsNullOrEmpty(request.Name) ? existingEntity.Name : request.Name, |
| 13 | 102 | | IsBlocked = request.IsBlocked, |
| 13 | 103 | | Description = string.IsNullOrEmpty(request.Description) ? existingEntity.Description : request.Descripti |
| 13 | 104 | | }; |
| 13 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Verifica si un asiento es válido. |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="entity">El asiento que se va a validar.</param> |
| | 111 | | /// <returns>True si el asiento es válido, False en caso contrario.</returns> |
| | 112 | | protected override bool IsValid(Seat entity) |
| 21 | 113 | | { |
| | 114 | | // Verificar si la entidad es válida (en este caso, el nombre no puede ser nulo o vacío) |
| 21 | 115 | | return !string.IsNullOrEmpty(entity.Name); |
| 21 | 116 | | } |
| | 117 | |
|
| | 118 | | // Otros métodos específicos para SeatService |
| | 119 | | } |
| | 120 | | } |