public ActionResult GetAll()
        {
            string api = ContentManager.APIUrl + "api/Categories/GetAll";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(api);
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "GET";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream rebut = httpResponse.GetResponseStream();
            StreamReader readStream = new StreamReader(rebut, Encoding.UTF8);
            string info = readStream.ReadToEnd();
            var arr = JsonConvert.DeserializeObject<JArray>(info);
            Category c = new Category();
            List<Category> Categories = new List<Category>();
            foreach (JObject i in arr)
            {
                int ID = (int)i["ID"];
                string Name = (string)i["Name"];
                c = new Category
                {
                    ID = ID,
                    Name = Name
                };
                Categories.Add(c);
            }

            ViewData["Categories"] = Categories;
            return View("ViewCategories");
        }
Exemple #2
0
        public ActionResult Create(string name, string info, DateTime time, string place, Nullable<int> maxAttendance, Nullable<int> requireAttendance, Nullable<decimal> price, HttpPostedFileBase image, int[] categories)
        {

            //Test get image
            var folderName = "/Content/Upload/Images/";
            var fileName = image.FileName;
            FileStream fileStream;
            using (fileStream = System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + folderName + fileName))
            {
                image.InputStream.CopyTo(fileStream);
            }
            byte[] data;
            string ImageUrl = "";
            using (Stream inputStream = image.InputStream)
            {
                MemoryStream memoryStream = inputStream as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    inputStream.CopyTo(memoryStream);
                }
                data = memoryStream.ToArray();
            }


            using (WebClient uploader = new WebClient())
            {
                try
                {
                    byte[] response = uploader.UploadFile(new Uri("http://uploads.im/api?upload"), fileStream.Name);
                    string s = uploader.Encoding.GetString(response);
                    JObject jo = JObject.Parse(s);
                    ImageUrl = (string)(jo["data"])["thumb_url"];
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + folderName + fileName);
                }
            }



            var httpWebRequest = (HttpWebRequest)WebRequest.Create(ContentManager.APIUrl + "api/Events/CreateEvent");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.MediaType = "application/json";
            httpWebRequest.Accept = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Headers["Authorization"] = "Bearer " + Session["Token"];
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                EventDetail temp = new EventDetail
                {
                    Name = name,
                    Info = info,
                    Time = time,
                    Place = place,
                    MaxAttendance = maxAttendance,
                    RequireAttendance = requireAttendance,
                    Price = price,
                    Image = ImageUrl,
                    Categories = new List<Category>()
                };
                if (categories != null)
                {
                    foreach (var item in categories)
                    {
                        var obj = new Category
                        {
                            ID = item,
                            Name = ""
                        };
                        temp.Categories.Add(obj);
                    }
                }
                string tmp = JsonConvert.SerializeObject(temp);

                streamWriter.Write(tmp);
                streamWriter.Flush();
                streamWriter.Close();
            }
            string result = "";
            string error = "";
            try
            {
                using (var httpResponse = httpWebRequest.GetResponse() as HttpWebResponse)
                {
                    if (httpWebRequest.HaveResponse && httpResponse != null)
                    {
                        using (var reader = new StreamReader(httpResponse.GetResponseStream()))
                        {
                            result = reader.ReadToEnd();
                        }
                        TempData["StatusCode"] = (int)httpResponse.StatusCode;
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)ex.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            error = reader.ReadToEnd();
                            //TODO: use JSON.net to parse this string and look at the error message
                            var JObject = JsonConvert.DeserializeObject<JObject>(error);
                            var token = JObject["ModelState"]["Time"];
                            if (token != null && token.Type != JTokenType.Null)
                            {
                                TempData["TimeError"] = (string)(JObject["ModelState"]["Time"][0]);
                            }
                            token = JObject["ModelState"]["MaxAttendance"];
                            if (token != null && token.Type != JTokenType.Null)
                            {
                                TempData["MaxError"] = (string)(JObject["ModelState"]["MaxAttendance"][0]);
                            }
                        }
                        TempData["StatusCode"] = (int)errorResponse.StatusCode;
                    }
                }
            }
            if (result != "")
            {
                var arr = JsonConvert.DeserializeObject<JObject>(result);
                return RedirectToAction("Detail", new { id = (int)arr["ID"] });
            }
            else
            {
                return RedirectToAction("Create");
            }
        }
Exemple #3
0
 public ActionResult Create()
 {
     var httpWebRequest = (HttpWebRequest)WebRequest.Create(ContentManager.APIUrl + "api/Categories/GetAll");
     httpWebRequest.ContentType = "application/json; charset=utf-8";
     httpWebRequest.Method = "GET";
     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
     Stream rebut = httpResponse.GetResponseStream();
     StreamReader readStream = new StreamReader(rebut, Encoding.UTF8);
     string info = readStream.ReadToEnd();
     var arr = JsonConvert.DeserializeObject<JArray>(info);
     List<Category> Categories = new List<Category>();
     foreach (JObject i in arr)
     {
         Category c = new Category();
         c.ID = (int)i["ID"];
         c.Name = (string)i["Name"];
         Categories.Add(c);
     }
     ViewData["Categories"] = Categories;
     ViewData["TimeError"] = TempData["TimeError"];
     ViewData["MaxError"] = TempData["MaxError"];
     return View("~/Views/Event/AddEvent.cshtml");
 }
Exemple #4
0
        public ActionResult Update(int id, string name, string info, System.DateTime time, string place, Nullable<int> maxAttendance, Nullable<int> requireAttendance, Nullable<decimal> price, string image, int[] categories)
        {
            EventDetail ed = new EventDetail();
            ed.ID = id;
            ed.Name = name;
            ed.Info = info;
            ed.Time = time;
            ed.Place = place;
            ed.MaxAttendance = maxAttendance;
            ed.RequireAttendance = requireAttendance;
            ed.Price = price;
            ed.Image = image;
            List<Category> CategoriesTmp = new List<Category>();
            if (categories != null)
            {
                foreach (var item in categories)
                {
                    Category c = new Category
                    {
                        ID = item,
                        Name = ""
                    };
                    CategoriesTmp.Add(c);
                }
            }
            ed.Categories = CategoriesTmp;
            ViewData["UpdateEvent"] = ed;


            //Get categories
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(ContentManager.APIUrl + "api/Categories/GetAll");
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "GET";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream rebut = httpResponse.GetResponseStream();
            StreamReader readStream = new StreamReader(rebut, Encoding.UTF8);
            string tmp = readStream.ReadToEnd();
            var arr = JsonConvert.DeserializeObject<JArray>(tmp);
            List<Category> Categories = new List<Category>();
            foreach (JObject i in arr)
            {
                Category c = new Category();
                c.ID = (int)i["ID"];
                c.Name = (string)i["Name"];
                Categories.Add(c);
            }
            ViewData["Categories"] = Categories;
            ViewData["TimeError"] = TempData["TimeError"];
            ViewData["MaxError"] = TempData["MaxError"];

            return View("~/Views/Event/EditEvent.cshtml");
        }