Esempio n. 1
0
        public static async System.Threading.Tasks.Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "awards")]
            HttpRequestMessage req,
            [CosmosDB(
                 databaseName: "contesthub",
                 collectionName: "Awards",
                 ConnectionStringSetting = "contesthub_DOCUMENTDB"
                 )]
            DocumentClient client,
            TraceWriter log)
        {
            PostBodyAwards data = await req.Content.ReadAsAsync <PostBodyAwards>();

            ///////2nd level Auth code
            //var jwtHandler = new JwtSecurityTokenHandler();
            //var jwtInput = req.Headers.Authorization.ToString();

            //var jwt = "";

            //if (jwtInput.Contains("Bearer"))
            //    jwt = jwtInput.Substring(7);
            //else
            //    jwt = jwtInput;



            //var readableToken = jwtHandler.CanReadToken(jwt);


            //var dId = "";
            //if (readableToken != true)
            //{
            //    return new NotFoundResult();
            //}
            //if (readableToken == true)
            //{
            //    var token = jwtHandler.ReadJwtToken(jwt);


            //    var claims = token.Claims;


            //    var claim = claims.Where(c => c.Type == "upn").FirstOrDefault();
            //    dId = claim.Value.Substring(0, claim.Value.IndexOf('@'));



            //}
            //if (dId.ToLower() != data.id.ToLower())
            //{
            //    return new UnauthorizedResult();
            //}

            log.Info($"Processed request for {data.id} in Awards, settingslimit: {data.limit},{data.limit}");

            Uri collectionUri = UriFactory.CreateDocumentCollectionUri("contesthub", "Awards");

            IDocumentQuery <AwardsElement> query = client.CreateDocumentQuery <AwardsElement>(collectionUri, new FeedOptions {
                EnableCrossPartitionQuery = true
            })
                                                   .Where(d => d.logonid.ToUpper() == data.id.ToUpper())
                                                   .AsDocumentQuery();


            //list result
            List <AwardsElement> awardsElementList = new List <AwardsElement>();

            while (query.HasMoreResults)
            {
                foreach (AwardsElement result in await query.ExecuteNextAsync())
                {
                    awardsElementList.Add(result);
                }
            }

            if (awardsElementList.Count == 0)
            {
                return(new NoContentResult());
            }

            else
            {
                //filter for one year
                awardsElementList = awardsElementList.Where(x => (DateTime.Now - DateTime.ParseExact(x.dateAndTime, "M/d/yyyy h:mm:ss tt", new CultureInfo("en-US"))).TotalDays < 365).ToList();
                // AwardsContainer awardsContainer = new AwardsContainer(awardsElementList.Count.ToString(), awardsElementList.Skip(((data.offset == 0 ? 1 : data.offset) - 1) * data.limit).Take(data.limit).ToList());
                AwardsContainer awardsContainer = new AwardsContainer("3500", awardsElementList.Skip(((data.offset == 0 ? 1 : data.offset) - 1) * data.limit).Take(data.limit).ToList());

                ////logic to copy award id to id
                foreach (AwardsElement item in awardsContainer.awardsList)
                {
                    item.id = item.awardId;
                }
                AwardsResponse awardsResponse = new AwardsResponse(awardsContainer);

                return(new OkObjectResult(awardsResponse));
            }
        }
Esempio n. 2
0
 public AwardsResponse(AwardsContainer awards)
 {
     this.awards = awards;
 }