예제 #1
0
        public async Task <IActionResult> Index(string fromCode = null, string toCode = null)
        {
            if (!string.IsNullOrWhiteSpace(fromCode) && !string.IsNullOrWhiteSpace(toCode))
            {
                _scheduleApi.From = fromCode;
                _scheduleApi.To   = toCode;
                var request  = RequestApiFactory.Create(_scheduleApi);
                var response = await request.GetResponseAsync()
                               .ConfigureAwait(continueOnCapturedContext: false);

                var result = RequestApiFactory.RequestJsonDesirialize <ScheduleResponse>(response);
                if (result != null && result.Error == null)
                {
                    var filter = new StarredThreadFilter(x =>
                                                         User.FindFirstValue(Services.Identity.User.IdClaimType).Equals(x.UserReference.UserId));
                    var sttaredThreads = await ThreadsRepository
                                         .FetchAsync(filter, RequestWindow.All)
                                         .ConfigureAwait(continueOnCapturedContext: false);

                    var model = new SchedulesViewModel
                    {
                        Segments          = result.Segments,
                        SttaredThreadKeys = sttaredThreads.Select(x => x.ThreadKey).ToArray(),
                        FromCode          = fromCode,
                        ToCode            = toCode,
                    };
                    return(View(model));
                }
            }

            return(View(new SchedulesViewModel
            {
                From = string.Empty,
                To = string.Empty,
            }));
        }
예제 #2
0
        public async Task <IActionResult> Index(SchedulesViewModel model)
        {
            try
            {
                if (model == null)
                {
                    return(View(new SchedulesViewModel
                    {
                        From = string.Empty,
                        To = string.Empty,
                    }));
                }
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                if (model.FromCode == null || model.ToCode == null)
                {
                    var fromCityTask = _cities.GetCityKeyAsync(model.From);
                    var toCityTask   = _cities.GetCityKeyAsync(model.To);

                    await Task.WhenAll(fromCityTask, toCityTask)
                    .ConfigureAwait(continueOnCapturedContext: false);

                    if (fromCityTask.Result == null || toCityTask.Result == null)
                    {
                        return(View(model));
                    }

                    model.FromCode = fromCityTask.Result.Code;
                    model.ToCode   = toCityTask.Result.Code;
                }

                _scheduleApi.From = model.FromCode;
                _scheduleApi.To   = model.ToCode;
                var request  = RequestApiFactory.Create(_scheduleApi);
                var response = await request.GetResponseAsync()
                               .ConfigureAwait(continueOnCapturedContext: false);

                var result = RequestApiFactory.RequestJsonDesirialize <ScheduleResponse>(response);
                if (result != null && result.Error == null)
                {
                    var filter = new StarredThreadFilter(x =>
                                                         User.FindFirstValue(Services.Identity.User.IdClaimType).Equals(x.UserReference.UserId));
                    var sttaredThreads = await ThreadsRepository
                                         .FetchAsync(filter, RequestWindow.All)
                                         .ConfigureAwait(continueOnCapturedContext: false);

                    var filtredSegments = result.Segments.AsEnumerable();
                    if (model.StartDate.HasValue)
                    {
                        filtredSegments = filtredSegments.Where(x => x.StartDate >= model.StartDate.Value);
                    }
                    if (model.EndDate.HasValue)
                    {
                        filtredSegments = filtredSegments.Where(x => x.StartDate <= model.EndDate.Value);
                    }
                    model.Segments = filtredSegments.ToArray();

                    model.SttaredThreadKeys = sttaredThreads.Select(x => x.ThreadKey).ToArray();
                    return(View(model));
                }
            }
            catch (Exception exc)
            {
                return(RedirectToAction("Error", new ErrorViewModel {
                    Exception = exc
                }));
            }

            return(View(model));
        }