コード例 #1
0
        public static async Task <bool> Run([HttpTrigger(AuthorizationLevel.Function, "post", "put", Route = "CreateOrUpdateResturant")] HttpRequest req, ILogger log)
        {
            log.LogInformation($"C# HTTP trigger function to create or update a resturant");

            IDbRepository <Resturant> Respository = new DbRepository <Resturant>();

            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                var    resturant   = JsonConvert.DeserializeObject <Resturant>(requestBody);
                if (req.Method == "POST")
                {
                    resturant.Id = null;
                    resturant.NumberOfTimesRated = 1;
                    resturant.RatingTotal        = resturant.Rating;
                    resturant.AverageRating      = resturant.Rating;
                    await Respository.CreateItemAsync(resturant, "resturant");
                }
                else
                {
                    resturant.NumberOfTimesRated++;
                    resturant.RatingTotal   = resturant.RatingTotal + resturant.Rating;
                    resturant.AverageRating = resturant.RatingTotal / resturant.NumberOfTimesRated;
                    await Respository.UpdateItemAsync(resturant.Id, resturant, "resturant");
                }
                return(true);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error occured: ");
                return(false);
            }
        }
コード例 #2
0
        public async Task <ActionResult> CreateAsync([Bind(Include = "ProductId, Name, ProductModel, Category, Description")] ProductsFromCosmos item)
        {
            if (ModelState.IsValid)
            {
                await DbRepository <ProductsFromCosmos> .CreateItemAsync(item);

                return(RedirectToAction("Index"));
            }
            return(View(item));
        }