//переадресовывает запрос на нужный метод, возвращает - JSON строку private async Task<JsonRespondBody> RedirectJsonString(JsonRequestBody json) { if (String.Compare(json.Method, "login", true) == 0) { return await LoginMethod(json); } //Далее методы которым нужен токен if (String.Compare(json.Method, "profile", true) == 0) { return ProfileMethod(json); } else if (String.Compare(json.Method, "dishlist", true) == 0) { return DishlistMethod(json); } else if (String.Compare(json.Method, "dishinfo", true) == 0) { return DishinfoMethod(json); } else if (String.Compare(json.Method, "offers", true) == 0) { return OffersMethod(json); } else if (String.Compare(json.Method, "orderinfo", true) == 0) { return OrderInfoMethod(json); } else if (String.Compare(json.Method, "orderlist", true) == 0) { return OrderListMethod(json); } else if (String.Compare(json.Method, "getcitylist", true) == 0) { return GetCityMethod(json); } else if (String.Compare(json.Method, "getcategorylist", true) == 0) { return GetCategoryList(json); } else if (String.Compare(json.Method, "getpricelimit", true) == 0) { return GetPriceLimit(json); } JsonRespondBody result = new JsonRespondBody { Error = "Invalid method", Status = "error" }; return result; }
// GET: Api public async Task<ActionResult> Index() { var jsonString = String.Empty; Request.InputStream.Position = 0; using (var inputStream = new StreamReader(Request.InputStream)) { jsonString = inputStream.ReadToEnd(); } if (jsonString != "") { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); JsonRequestBody serJsonDetails = null; try { serJsonDetails = (JsonRequestBody)javaScriptSerializer.Deserialize(jsonString, typeof(JsonRequestBody)); } catch { serJsonDetails = new JsonRequestBody(); } //если параметры не переданы то инициализируем пустым значением if (serJsonDetails.Parameters == null) { serJsonDetails.Parameters = new Dictionary<string, string>(); } JsonRespondBody result = await RedirectJsonString(serJsonDetails); string output = javaScriptSerializer.Serialize(result); Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.ContentEncoding = Encoding.UTF8; Response.Write(output); Response.End(); } return RedirectToAction("index", "home"); }
private async Task<JsonRespondBody> LoginMethod(JsonRequestBody json) { if (json.Parameters.ContainsKey("login") == false || json.Parameters.ContainsKey("password") == false) { return new JsonRespondBody { Status = "error", Error = "Invalid parameters" }; } string login = json.Parameters["login"]; string password = json.Parameters["password"]; string uname = login; //проверка логина и пароля на корректность var singingresult = await SignInManager.PasswordSignInAsync(login, password, false, false); if (singingresult == SignInStatus.Failure) { var suser = await UserManager.FindByEmailAsync(login); if (suser == null) { return new JsonRespondBody { Status = "Failure", Error = "Invalid login or password" }; } singingresult = await SignInManager.PasswordSignInAsync(suser.UserName, password, false, false); if (singingresult == SignInStatus.Failure) { return new JsonRespondBody { Status = "Failure", Error = "Invalid login or password" }; } uname = suser.UserName; } var user = DataManager.User.GetUserByName(uname); JsonRespondBody result = new JsonRespondBody { Status = "OK" }; //формируем результат usertoken token = DataManager.User.GenerateToken(user.Id); UserSerealizerBody ub = GetUserInfo(user.Id); ub.Token = token.Token; result.Result = ub; return result; }
private JsonRespondBody GetCategoryList(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } Dictionary<string, string> list = new Dictionary<string, string>(); //генерация ответа var cities = DataManager.Dish.GetDishTypes(); foreach (var item in cities) { list.Add(item.Id_DishType.ToString(), item.Name); } JsonRespondBody result = new JsonRespondBody { Result = list, Status = "OK" }; return result; }
private JsonRespondBody GetCityMethod(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } Dictionary<string, string> list = new Dictionary<string, string>(); //генерация ответа var cities = DataManager.Geolocation.GetAllCities(); foreach(var item in cities) { list.Add(item.id_city.ToString(), item.name); } JsonRespondBody result = new JsonRespondBody { Result = list, Status = "OK" }; return result; }
private JsonRespondBody ProfileMethod(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } if (json.Parameters.ContainsKey("username")) { user = DataManager.User.GetUserByName(json.Parameters["username"]); if (user == null) { return new JsonRespondBody { Error = "Invalid username", Status = "error" }; } } //генерация ответа UserSerealizerBody ub = GetUserInfo(user.Id); JsonRespondBody result = new JsonRespondBody { Result = ub, Status = "OK" }; return result; }
private JsonRespondBody DishinfoMethod(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } if (!json.Parameters.ContainsKey("Id_Dish")) { return new JsonRespondBody { Error = "Id_Dish is required", Status = "error" }; } int dish_id = 0; try { dish_id = Convert.ToInt32(json.Parameters["Id_Dish"]); } catch { return new JsonRespondBody { Error = "Invalid parameters", Status = "error" }; } //генерация ответа dish d = DataManager.Dish.GetDishById(dish_id); if (d == null) { return new JsonRespondBody { Error = "Dish not found", Status = "warning" }; } string address = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority); DishSerealizerBody res = new DishSerealizerBody { Description = d.Description, Id = d.Id_Dish, Ingridients = d.Ingridient, Name = d.Name, Price = d.Price, PriceWithIngridients = d.PriceWithIngridient, Type = d.dishtype.Name, Image = address + d.ImageUrl }; JsonRespondBody result = new JsonRespondBody { Result = res, Status = "OK" }; return result; }
private JsonRespondBody OffersMethod(JsonRequestBody json) { int Limit = 20; int Page = 0; string Search = ""; int City = 0; string category = ""; int minPrice = -1; int maxPrice = -1; aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } if (json.Parameters.ContainsKey("Limit")) { try { Limit = Convert.ToInt32(json.Parameters["Limit"]); Limit = Limit < 1 ? 1 : Limit; } catch { } } if (json.Parameters.ContainsKey("Page")) { try { Page = Convert.ToInt32(json.Parameters["Page"]); Page = Page < 0 ? 0 : Page; } catch { } } if (json.Parameters.ContainsKey("Search")) { Search = json.Parameters["Search"]; } if (json.Parameters.ContainsKey("City")) { try { City = Convert.ToInt32(json.Parameters["City"]); } catch { } } if (json.Parameters.ContainsKey("Category")) { try { int b; if (Int32.TryParse(json.Parameters["Category"], out b)) { category = json.Parameters["Category"] + ";"; } } catch { } } if (json.Parameters.ContainsKey("MinPrice")) { try { minPrice = Convert.ToInt32(json.Parameters["MinPrice"]); } catch { } } if (json.Parameters.ContainsKey("MaxPrice")) { try { maxPrice = Convert.ToInt32(json.Parameters["MaxPrice"]); } catch { } } //генерация ответа string address = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority); int count = 0; IEnumerable<dish> list = DataManager.Dish.GetDishList(Page, Limit, ref count, Search, City, category, false, false, minPrice, maxPrice); List<DishSerealizerBody> res = new List<DishSerealizerBody>(); foreach (var item in list) { res.Add(new DishSerealizerBody { Description = item.Description, Id = item.Id_Dish, Ingridients = item.Ingridient, Name = item.Name, Price = item.Price, PriceWithIngridients = item.PriceWithIngridient, Type = item.dishtype.Name, Image = address + item.ImageUrl }); } OfferSerealizerBody r = new OfferSerealizerBody { CurrentPage = Page, Limit = Limit, List = res, }; JsonRespondBody result = new JsonRespondBody { Status = "OK", Result = r }; return result; }
private JsonRespondBody OrderInfoMethod(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); int id_order = 0; if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } if (!json.Parameters.ContainsKey("Id_Order")) { return new JsonRespondBody { Error = "Id_Order is required", Status = "error" }; } try { id_order = Convert.ToInt32(json.Parameters["Id_Order"]); } catch { return new JsonRespondBody { Error = "Invalid parameters", Status = "warning" }; } order o = DataManager.Order.GetOrderById(id_order); if (o == null) { return new JsonRespondBody { Error = "Order not found", Status = "warning" }; } if (o.Id_Cook != user.Id && o.Id_Customer != user.Id) { return new JsonRespondBody { Error = "Access denied", Status = "warning" }; } //формируем ответ string address = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority); IEnumerable<orderproduct> op = DataManager.Order.GetOrderProducts(o.Id_Order); List<DishSerealizerBody> dl = new List<DishSerealizerBody>(); foreach (var item in op) { dl.Add(new DishSerealizerBody { Id = item.Id_Dish, Name = item.dish.Name, Price = item.Price, PriceWithIngridients = item.PriceWithIngridients, Image = address + item.dish.ImageUrl }); } DataManager.User.RefreshBuffer(); OrderSerializerBody res = GetOrderInformation(o); res.OrderList = dl; JsonRespondBody result = new JsonRespondBody { Status = "OK", Result = res }; return result; }
private JsonRespondBody OrderListMethod(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); bool incomingOrders = true; if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } if (json.Parameters.ContainsKey("incomingOrders")) { try { incomingOrders = Convert.ToBoolean(json.Parameters["incomingOrders"]); } catch { return new JsonRespondBody { Error = "Invalid parameters", Status = "error" }; } } DataManager.User.RefreshBuffer(); IEnumerable<order> ord = DataManager.Order.GetOrdersByUserId(user.Id, incomingOrders); List<OrderSerializerBody> orders = new List<OrderSerializerBody>(); foreach (var item in ord) { orders.Add(GetOrderInformation(item)); } JsonRespondBody result = new JsonRespondBody { Status = "OK", Result = orders }; return result; }
private JsonRespondBody GetPriceLimit(JsonRequestBody json) { aspnetuser user = DataManager.User.GetUserByToken(json.Token); if (user == null) { return new JsonRespondBody { Error = "Invalid token", Status = "error" }; } Dictionary<string, string> list = new Dictionary<string, string>(); //генерация ответа int minPrice = DataManager.Dish.GetMinPrice(); int maxPrice = DataManager.Dish.GetMaxPrice(); list.Add("MinPrice", minPrice.ToString()); list.Add("MaxPrice", maxPrice.ToString()); JsonRespondBody result = new JsonRespondBody { Result = list, Status = "OK" }; return result; }