Exemplo n.º 1
0
 public HttpResponseMessage Count()
 {
     try {
         return(Request.CreateResponse(HttpStatusCode.OK, videoRepository.Count()));
     } catch (Exception _excepcion) {
         log.Error(_excepcion);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, _excepcion));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves all queries on guest accesses
        /// </summary>
        /// <param name="graphQlQuery"></param>
        public void ResolveQuery(GraphQlQuery graphQlQuery)
        {
            // LATEST VIDEO: returns the latest video of a channel
            graphQlQuery.FieldAsync <VideoType>(
                "latestVideo",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "channelId"
            }
                    ),
                resolve: async(context) =>
            {
                var channelId = context.GetArgument <string>("channelId");

                var video   = await _videoRepository.GetLatest(channelId);
                var channel = await _channelRepository.Get(channelId);

                var enrichedVideo = video with {
                    Channel = channel
                };

                // map entity to model
                return(_mapper.Map <VideoModel>(enrichedVideo));
            }
                );

            // VIDEOS: returns all videos of a certain channel
            graphQlQuery.FieldAsync <ListGraphType <VideoType> >(
                "videos",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "channelId"
            },
                    new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "skip"
            },
                    new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "take"
            }
                    ),
                resolve: async(context) =>
            {
                var channelId = context.GetArgument <string>("channelId");
                var channel   = await _channelRepository.Get(channelId);

                var videos = await _videoRepository.GetByChannel(
                    channelId,
                    context.GetArgument <int>("skip"),
                    context.GetArgument <int>("take")
                    );

                var enrichedVideos = EnrichVideosWithChannel(videos, channel);

                // map entity to model
                return(_mapper.Map <List <VideoModel> >(enrichedVideos));
            }
                );

            // VIDEOS: returns all videos of a certain channel
            graphQlQuery.FieldAsync <IntGraphType>(
                "videoCount",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "channelId"
            }
                    ),
                resolve: async(context) =>
            {
                var videoCount = await _videoRepository.CountByChannel(
                    context.GetArgument <string>("channelId")
                    );

                // map entity to model
                return(videoCount);
            }
                );

            // VIDEO COUNT TOTAL
            graphQlQuery.FieldAsync <IntGraphType>(
                "videoCountTotal",
                resolve: async(context) =>
            {
                var videoCount = await _videoRepository.Count();

                // map entity to model
                return(videoCount);
            }
                );

            // VIDEO PUBLISH DISTRIBUTION
            graphQlQuery.FieldAsync <ListGraphType <VideoPublishAggregationItemType> >(
                "videoPublishDistribution",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "channelId"
            }
                    ),
                resolve: async(context) =>
            {
                try
                {
                    var result = await _aggregateVideoPublishTimesUseCase.Handle(
                        new AggregateVideoPublishTimesRequest(
                            context.GetArgument <string>("channelId")
                            )
                        );

                    var aggregationModels = new List <VideoPublishAggregationItemModel>();

                    foreach (var daysOfWeek in result.Aggregation)
                    {
                        foreach (var hourOfDay in daysOfWeek.Value)
                        {
                            aggregationModels.Add(new VideoPublishAggregationItemModel()
                            {
                                DayOfTheWeek    = (int)daysOfWeek.Key,
                                HourOfTheDay    = hourOfDay.Key,
                                PublishedVideos = hourOfDay.Value
                            });
                        }
                    }

                    // map entity to model
                    return(aggregationModels);
                }
                catch
                {
                    return(null);
                }
            }
                );
        }