public async Task <IActionResult> GetAllGroupsAsync() { _logger.LogInformation($"Starting method '{nameof(GetAllGroupsAsync)}'."); ISightseeingGroupDbService groupDbService = null; try { groupDbService = _dbServiceFactory[nameof(ISightseeingGroupDbService)] as ISightseeingGroupDbService; var groups = await groupDbService.GetAllAsync(); var groupDtos = MapToDtoEnumerable(groups); var response = new ResponseWrapper(groupDtos); _logger.LogInformation($"Finished method '{nameof(GetAllGroupsAsync)}'."); return(Ok(response)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, groupDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }
public async Task <IActionResult> GetAvailableGroupDatesAsync() { VisitInfo recentInfo = null; IVisitInfoDbService infoDbService = null; ISightseeingGroupDbService groupDbService = null; List <GroupInfo> availableDates = new List <GroupInfo>(); try { infoDbService = _dbServiceFactory[nameof(IVisitInfoDbService)] as IVisitInfoDbService; groupDbService = _dbServiceFactory[nameof(ISightseeingGroupDbService)] as ISightseeingGroupDbService; recentInfo = await GetRecentSightseeingInfoAsync(infoDbService); if (!IsSightseeingDurationSet(recentInfo)) { _logger.LogWarning($"{nameof(VisitInfo.SightseeingDuration)} is set to 0 hours."); return(Ok(new ResponseWrapper(availableDates))); } // Calculate the latest date when you can still buy a ticket. MaxTicketOrderInterval is in weeks. var maxTicketPurchaseDate = DateTime.Now.AddDays(recentInfo.MaxTicketOrderInterval * 7); var dateTime = DateTime.Now; var futureGroups = await groupDbService.GetByAsync(x => x.SightseeingDate > DateTime.Now); // Create available sightseeing dates. while (dateTime <= maxTicketPurchaseDate) { availableDates.AddRange(GetDailyDates(recentInfo, dateTime, futureGroups)); dateTime = dateTime.AddDays(1); } var response = new ResponseWrapper(availableDates); return(Ok(response)); } catch (InvalidOperationException ex) { _logger.LogWarning(ex, $"Element '{nameof(VisitInfo)}' not found."); return(Ok(new ResponseWrapper(availableDates))); } catch (InternalDbServiceException ex) when(recentInfo is null) { // Exception thrown by IVisitInfoDbService instance. LogInternalDbServiceException(ex, infoDbService.GetType()); throw; } catch (InternalDbServiceException ex) when(recentInfo != null) { // Exception thrown by ISightseeingGroupDbService instance. LogInternalDbServiceException(ex, groupDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }
public TicketOrderHandler(IIndex <string, IServiceBase> dbServiceFactory, IValidator <SightseeingGroup> groupValidator, ILogger <TicketOrderHandler> logger) { _orderStamp = Guid.NewGuid().ToString(); _tickets = new List <Ticket>(); _dbServiceFactory = dbServiceFactory; _groupDbService = _dbServiceFactory[nameof(ISightseeingGroupDbService)] as ISightseeingGroupDbService; _groupValidator = groupValidator; _logger = logger; }
public async Task <IActionResult> GetGroupAsync(string id) { _logger.LogInformation($"Starting method '{nameof(GetGroupAsync)}'."); if (string.IsNullOrEmpty(id)) { return(OnInvalidParameterError($"Parameter '{nameof(id)}' cannot be null or empty.")); } ISightseeingGroupDbService groupDbService = null; try { groupDbService = _dbServiceFactory[nameof(ISightseeingGroupDbService)] as ISightseeingGroupDbService; var group = await groupDbService.GetAsync(id); var groupDto = MapToDto(group); var response = new ResponseWrapper(groupDto); _logger.LogInformation($"Finished method '{nameof(GetGroupAsync)}'."); return(Ok(response)); } catch (InvalidOperationException ex) { return(OnNotFoundError($"Cannot found element '{typeof(SightseeingGroup).Name}' with specified id: '{id}'.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, groupDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }