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; } }
private async Task <VisitInfo> GetRecentSightseeingInfoAsync(IVisitInfoDbService infoDbService) { var allInfo = await infoDbService.GetAllAsync(); return(allInfo.OrderByDescending(x => x.UpdatedAt == DateTime.MinValue ? x.CreatedAt : x.UpdatedAt).First()); }
public VisitInfoController(IVisitInfoDbService infoDbService, ILogger <VisitInfoController> logger, IMapper mapper) : base(logger) { _logger = logger; _mapper = mapper; _infoDbService = infoDbService; }