Пример #1
0
        public async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/rank")] HttpRequestMessage req,
            ILogger log)
        {
            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                EnableCrossPartitionQuery = true
            };

            try
            {
                var documentQuery = _documentClient.CreateDocumentQuery <DocumentInterface>(UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions)
                                    .OrderByDescending(x => x.Score)
                                    .ToList();

                var rank = new RankResponseInterface();
                rank.Images = new List <Images>();
                var count = 0;
                foreach (var item in documentQuery)
                {
                    var image = new Images
                    {
                        ImageId    = item.ImageId,
                        User       = item.User,
                        UserIcon   = item.UserIcon,
                        PictureUrl = item.PictureUrl,
                        Score      = item.Score,
                        GoodCnt    = item.GoodCnt,
                        Rank       = count += 1
                    };
                    rank.Images.Add(image);
                }

                var res = req.CreateResponse(HttpStatusCode.OK);
                res.Content = new StringContent(JsonConvert.SerializeObject(rank));

                log.LogInformation($"Response:{JsonConvert.SerializeObject(rank)}");
                return(res);
            }
            catch (Exception ex)
            {
                log.LogError($"Error:{ex.Message}");
                log.LogError($"StackTrace:{ex.StackTrace}");
                return(req.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
        public async Task Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "v1/good")] HttpRequest req,
            ILogger log)
        {
            // POSTデータからパラメータを取得
            var jsonContent = await req.ReadAsStringAsync();

            log.LogInformation($"Request:{jsonContent}");

            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                EnableCrossPartitionQuery = true
            };

            try
            {
                dynamic data     = JsonConvert.DeserializeObject(jsonContent);
                string  image_id = data?.id;

                // バリデーションチェック
                if (string.IsNullOrWhiteSpace(image_id) || !(Regex.IsMatch(image_id, @"^[0-9]{14}$")))
                {
                    log.LogInformation("バリデーションエラー");
                    return;
                }

                var doc = _documentClient.CreateDocumentQuery <DocumentInterface>(
                    UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions)
                          .Where(x => x.ImageId == image_id)
                          .AsEnumerable()
                          .FirstOrDefault();

                if (doc == null)
                {
                    log.LogInformation("更新対象なし");
                    return;
                }

                var requestOptions = new RequestOptions()
                {
                    PartitionKey    = new PartitionKey(doc.ImageId),
                    AccessCondition = new AccessCondition()
                    {
                        Condition = doc.Etag,
                        Type      = AccessConditionType.IfMatch
                    }
                };

                TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");

                var update = new DocumentInterface()
                {
                    Id              = doc.Id,
                    ImageId         = doc.ImageId,
                    User            = doc.User,
                    UserIcon        = doc.UserIcon,
                    PictureUrl      = doc.PictureUrl,
                    Score           = doc.Score,
                    GoodCnt         = doc.GoodCnt + 1,
                    CreatedDatatime = doc.CreatedDatatime,
                    UpdatedDatatime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), tst)
                };

                await _documentClient.ReplaceDocumentAsync(
                    UriFactory.CreateDocumentUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID, doc.Id), update, requestOptions);

                var imageInfo = _documentClient.CreateDocumentQuery <DocumentInterface>(UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions)
                                .OrderByDescending(x => x.Score)
                                .ToList();

                var rank = new RankResponseInterface();
                rank.Images = new List <Images>();
                var count = 0;
                foreach (var res in imageInfo)
                {
                    var image = new Images
                    {
                        ImageId    = res.ImageId,
                        User       = res.User,
                        UserIcon   = res.UserIcon,
                        PictureUrl = res.PictureUrl,
                        Score      = res.Score,
                        GoodCnt    = res.GoodCnt,
                        Rank       = count += 1
                    };
                    rank.Images.Add(image);
                }

                await _httpClientService.PostJsonAsync <RankResponseInterface>(AppSettings.Instance.SIGNALR_URL, rank);
            }
            catch (Exception ex)
            {
                log.LogError($"Error:{ex.Message}");
                log.LogError($"StackTrace:{ex.StackTrace}");
            }
        }
Пример #3
0
        public static async void RegisterToCosmosdb([ActivityTrigger] IDurableActivityContext context, ILogger log)
        {
            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                EnableCrossPartitionQuery = true
            };

            var input = context.GetInput <(string user_id, string url, string score, string image_id)>();

            var httpClient = HttpStart.GetInstance();
            var userInfo   = await httpClient.GetAsync($"https://api.line.me/v2/bot/profile/{input.user_id}");

            var data = JsonConvert.DeserializeObject <LineUserInfoInterface>(userInfo);

            TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");

            var item = new DocumentInterface()
            {
                ImageId         = input.image_id,
                User            = data.Name,
                UserIcon        = data.PictureUrl,
                PictureUrl      = input.url.Replace(AppSettings.Instance.BLOB_URL, AppSettings.Instance.PROXY_URL),
                Score           = double.Parse(input.score),
                GoodCnt         = 0,
                CreatedDatatime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), tst),
                UpdatedDatatime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), tst)
            };

            var ducumentClient = HttpStart.GetDocumentInstance();
            await ducumentClient.CreateDatabaseIfNotExistsAsync(new Database { Id = AppSettings.Instance.DATABASE_ID });

            PartitionKeyDefinition pkDefn = new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/image_id"
                }
            };
            await ducumentClient.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(AppSettings.Instance.DATABASE_ID), new DocumentCollection { Id = AppSettings.Instance.COLLECTION_ID, PartitionKey = pkDefn },
                new RequestOptions { OfferThroughput = 400, PartitionKey = new PartitionKey("/image_id") });

            await ducumentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), item);

            var documentQuery = ducumentClient.CreateDocumentQuery <DocumentInterface>(UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions)
                                .OrderByDescending(x => x.Score)
                                .ToList();

            var rank = new RankResponseInterface();

            rank.Images = new List <Images>();
            var count = 0;

            foreach (var res in documentQuery)
            {
                var image = new Images
                {
                    ImageId    = res.ImageId,
                    User       = res.User,
                    UserIcon   = res.UserIcon,
                    PictureUrl = res.PictureUrl,
                    Score      = res.Score,
                    GoodCnt    = res.GoodCnt,
                    Rank       = count += 1
                };
                rank.Images.Add(image);
            }

            await httpClient.PostJsonAsync <RankResponseInterface>(AppSettings.Instance.SIGNALR_URL, rank);
        }