public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Product> productCollection = dbcontext.database.GetCollection <Product>("Product"); IMongoCollection <SingleProduct> singleProductCollection = dbcontext.database.GetCollection <SingleProduct>("SingleProducts"); string name = req.Query["Name"]; string price = req.Query["Price"]; string time = req.Query["Time"]; List <string> products = new List <string>(); List <string> amounts = new List <string>(); string requestBody = new StreamReader(req.Body).ReadToEnd(); var data = JsonConvert.DeserializeObject <Product>(requestBody); name = data.Name; price = data.Price; products = data.Products; amounts = data.Amounts; time = data.Time; Product product = new Product(name, price, products, amounts, time); productCollection.InsertOne(product); return(name != null ? (ActionResult) new OkObjectResult(JsonConvert.SerializeObject("Ok")) : new BadRequestObjectResult(JsonConvert.SerializeObject("Bad"))); }
public static string FindUser(User user, bool s) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <User> userCollection = dbcontext.database.GetCollection <User>("Chamomile"); string retname = "Quest"; var filter = new BsonDocument(); if (s == true) { filter = new BsonDocument("Email", user.Email); } else { filter = new BsonDocument("$and", new BsonArray { new BsonDocument("Name", user.Name), new BsonDocument("Password", user.Password), new BsonDocument("Email", user.Email) }); } var list = userCollection.Find(filter).ToList(); foreach (var doc in list) { retname = doc.Name; } return(retname); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "UpdateProduct/{name}")] HttpRequest req, string name, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Product> productCollection = dbcontext.database.GetCollection <Product>("Product"); string uname = req.Query["Name"]; string uprice = req.Query["Price"]; string utime = req.Query["Time"]; List <string> uproducts = new List <string>(); List <string> uamounts = new List <string>(); string requestBody = new StreamReader(req.Body).ReadToEnd(); var data = JsonConvert.DeserializeObject <Product>(requestBody); uname = data.Name; uprice = data.Price; uproducts = data.Products; uamounts = data.Amounts; utime = data.Time; var object1 = productCollection.Find(Builders <Product> .Filter.Eq("Name", name)).ToList(); ObjectId id = new ObjectId(); foreach (var doc in object1) { id = doc.Id; } Product product = new Product(id, uname, uprice, uproducts, uamounts, utime); productCollection.ReplaceOne(Builders <Product> .Filter.Eq("Name", name), product); return(name != null ? (ActionResult) new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "TakeOrder")] HttpRequest req, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Order> ordersCollection = dbcontext.database.GetCollection <Order>("Orders"); List <Order> orders = ordersCollection.Find(Builders <Order> .Filter.Empty).ToList(); return(orders != null ? (ActionResult) new OkObjectResult(JsonConvert.SerializeObject(orders)) : new BadRequestObjectResult(JsonConvert.SerializeObject("Bad"))); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Buy/{name}/{coordinates}/{amount}")] HttpRequest req, string name, List <float> coordinates, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Product> productCollection = dbcontext.database.GetCollection <Product>("Product"); IMongoCollection <SingleProduct> singleProductCollection = dbcontext.database.GetCollection <SingleProduct>("SingleProducts"); IMongoCollection <Order> ordersCollection = dbcontext.database.GetCollection <Order>("Orders"); string requestBody = new StreamReader(req.Body).ReadToEnd(); var data = JsonConvert.DeserializeObject(requestBody); Product product = new Product(); var object2 = productCollection.Find(Builders <Product> .Filter.Eq("Name", name)).ToList(); foreach (var doc in object2) { product = doc; } List <string> products = product.Products; List <string> amounts = product.Amounts; SingleProduct singleProduct = new SingleProduct(); SingleProduct firstProduct = new SingleProduct(); ObjectId id = new ObjectId(); for (int i = 0; i < products.Count; i++) { var object1 = singleProductCollection.Find(Builders <SingleProduct> .Filter.Eq("Name", products[i])).ToList(); foreach (var doc in object1) { id = doc.Id; firstProduct = doc; } SingleProduct reproduct = new SingleProduct(); singleProduct = new SingleProduct(id, products[i], System.Convert.ToString(System.Convert.ToInt32(firstProduct.Amount) - System.Convert.ToInt32(amounts[i]))); singleProductCollection.ReplaceOne(Builders <SingleProduct> .Filter.Eq("Name", products[i]), singleProduct); var list = singleProductCollection.Find(new BsonDocument("Amount", singleProduct.Amount)).ToList(); foreach (var doc in list) { reproduct = doc; } if (reproduct.Amount == "0") { singleProductCollection.DeleteOne(Builders <SingleProduct> .Filter.Eq("Name", reproduct.Name)); } } Order order = new Order(product.Name, coordinates); ordersCollection.InsertOne(order); return(name != null ? (ActionResult) new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "DeleteProduct/{name}")] HttpRequest req, string name, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Product> productCollection = dbcontext.database.GetCollection <Product>("Product"); productCollection.DeleteOne(Builders <Product> .Filter.Eq("Name", name)); return(name != null ? (ActionResult) new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "TakeSingleProducts")] HttpRequest req, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <SingleProduct> productCollection = dbcontext.database.GetCollection <SingleProduct>("SingleProducts"); var products = productCollection.Find(Builders <SingleProduct> .Filter.Empty).ToList(); string jsonString = JsonConvert.SerializeObject(products, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented }); return(products != null ? (ActionResult) new OkObjectResult(jsonString) : new BadRequestObjectResult(JsonConvert.SerializeObject("Bad"))); }
public static Product FindProduct(string product) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Product> productCollection = dbcontext.database.GetCollection <Product>("Product"); Product reproduct = null; var filter = new BsonDocument("Name", product); var list = productCollection.Find(filter).ToList(); foreach (var doc in list) { reproduct = doc; } return(reproduct); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "CreateSingleProduct")] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <SingleProduct> productCollection = dbcontext.database.GetCollection <SingleProduct>("SingleProducts"); string path = @"C:\Users\Dell\Source\Repos\FunctionApp1\FunctionApp1\Menu\Menu1.txt"; string name = ""; string amount = ""; SingleProduct singleProduct = new SingleProduct(name, amount); try { using (StreamReader sr = new StreamReader(path)) { string line = ""; int j = 0; for (int i = 0; (line = sr.ReadLine()) != "*"; i++) { if (i % 2 == 0) { name = line; } else if (i % 2 != 0) { amount = line; singleProduct = new SingleProduct(name, amount); productCollection.InsertOne(singleProduct); } } } result = true; } catch (Exception e) { Console.WriteLine(e.Message); } return(result != false ? (ActionResult) new OkObjectResult(JsonConvert.SerializeObject("Ok")) : new BadRequestObjectResult(JsonConvert.SerializeObject("Bad"))); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "TakeOneOrder/{name}/{driverscoordinates}")] HttpRequest req, string name, List <float> driverscoordinates, TraceWriter log) { ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <Order> ordersCollection = dbcontext.database.GetCollection <Order>("Orders"); Order order = new Order(); var object1 = ordersCollection.Find(Builders <Order> .Filter.Eq("Name", name)).ToList(); foreach (var doc in object1) { order = new Order(doc.ProductsName, doc.Coordinates, driverscoordinates); } if (order.DriversCoordinates.Count >= 3) { } ordersCollection.ReplaceOne(Builders <Order> .Filter.Eq("Name", name), order); return(name != null ? (ActionResult) new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); ChamomileContext dbcontext = new ChamomileContext(); IMongoCollection <User> userCollection = dbcontext.database.GetCollection <User>("Chamomile"); string email = req.Query["email"]; string password = req.Query["password"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); password = password ?? data?.password; email = email ?? data?.email; User user = new User(email, password); string username = User.FindUser(user, true); return((username != "Quest") ? (ActionResult) new OkObjectResult(user) : new BadRequestObjectResult("Wrong")); }