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 async Task <IActionResult> AddInfoAsync([FromBody] VisitInfoDto info) { _logger.LogInformation($"Starting method '{nameof(AddInfoAsync)}'."); try { // Ignore Id if the client set it. Id of entity is set internally by the server. info.Id = null; var infoToBeAdded = MapToDomainModel(info); var addedInfo = await _infoDbService.RestrictedAddAsync(infoToBeAdded); // Reverse map only for response to the client. var addedInfoDto = MapToDto(addedInfo); var response = new ResponseWrapper(addedInfoDto); string addedInfoUrl = $"{ControllerPrefix}/{addedInfo.Id}"; _logger.LogInformation($"Finished method '{nameof(addedInfo)}'."); return(Created(addedInfoUrl, response)); } catch (InvalidOperationException ex) { return(OnInvalidParameterError($"Element '{typeof(VisitInfo).Name}' already exists.", ex)); } catch (InternalDbServiceException ex) { LogInternalDbServiceException(ex, _infoDbService.GetType()); throw; } catch (Exception ex) { LogUnexpectedException(ex); throw; } }