public void Create(RequestShared r) { if (ModelState.IsValid) { } var userId = _user.GetUserId(HttpContext.User); var id = "18fb9898-50b3-4854-9df3-f7d834bdd3b9"; Request data = new Request() { CategoryId = r.CategoryId, UserId = id, CityId = r.CityId, PhoneNumber = r.PhoneNumber, Title = r.Title, Description = r.Description, Approved = false, Email = r.Email, Active = true, Address = r.Address, PostalCode = r.PostalCode, InsertedDate = DateTime.Now }; _context.Request.Add(data); _context.SaveChanges(); }
public JsonResult Search(int?page = 1) { //Giphy API params string key = System.Web.Configuration.WebConfigurationManager.AppSettings["GiphyAPIKey"]; //retrieve the key from a secret place string q = Request.QueryString["q"]; //the user's search string string rating = Request.QueryString["rating"]; //desired max msrp rating string lang = Request.QueryString["lang"]; //language selected, for localization purposes //Pagination Params - the page variable is from the url route's {page} variable int limit = 9; //number of images per page, let's not change this for now int offset = (int)page * 9 - limit; //get the offset for the current page //Get User Info for logging the request in the DB log Table DateTime timestamp = DateTime.Now; //Timestamp for user request string userAgent = Request.UserAgent; //User Agent Type string userIP = Request.UserHostAddress; //User IP ADDR //Create new log entry var log = db.RequestLogs.Create(); //Add data to the new entry fields log.RequestTime = timestamp; log.RequestClientIP = userIP; log.RequestClientAgent = userAgent; log.RequestQuery = q; log.RequestRating = rating; log.RequestLang = lang; //add the new log to the DB Table and save db.RequestLogs.Add(log); db.SaveChanges(); //Giphy API Reqquest string url = "https://api.giphy.com/v1/gifs/search?api_key=" + key + "&q=" + q + "&limit=" + limit + "&offset=" + offset + "&rating=" + rating + "&lang=" + lang; //Get the JSON from Giphy //inspired by: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-request-data-using-the-webrequest-class WebRequest request = WebRequest.Create(url); //send the request WebResponse response = request.GetResponse(); //get the response Stream dataStream = response.GetResponseStream(); //start the data stream string reader = new StreamReader(dataStream).ReadToEnd(); //read in as a string //Parse the string into a JSON Object //inspired by: https://stackoverflow.com/questions/20437279/getting-json-data-from-a-response-stream-and-reading-it-as-a-string var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //prepare the serializer to parse var data = serializer.DeserializeObject(reader); //parse the string into a JSON object with the serializer's Deserialize method //clean up dataStream.Close(); //close the stream response.Close(); //close the response return(Json(data, JsonRequestBehavior.AllowGet)); //return the JSON object, allow GET requests }
public ActionResult Requests(Requests requests) { if (ModelState.IsValid) { //Add new request to the database db.Requests.Add(requests); db.SaveChanges(); return(RedirectToAction("Index")); } //Return the requests return(View(requests)); }
public void Create(DonationRegisterShared register) { if (ModelState.IsValid) { } var userId = _user.GetUserId(HttpContext.User); var id = "18fb9898-50b3-4854-9df3-f7d834bdd3b9"; Donation data = new Donation() { CategoryId = register.CategoryId, UserId = id, PhoneNumber = register.PhoneNumber, Title = register.Title, Description = register.Description, Email = register.Email, InsertedDate = DateTime.Now }; _context.Donation.Add(data); _context.SaveChanges(); }
private void AddToDatabase(string data) { Request request = new Request { IPAddress = Request.UserHostAddress, DateOfRequest = DateTime.Now, Browser = Request.Browser.Type, SpecialSite = (string)data }; Debug.WriteLine("IPAddress = " + request.IPAddress); Debug.WriteLine("Browser = " + request.Browser); Debug.WriteLine("DateOfRequest = " + request.DateOfRequest); Debug.WriteLine("SpecialSite = " + request.SpecialSite); db.Requests.Add(request); try { db.SaveChanges(); Debug.WriteLine("Changes were saved"); } //Got a bunch of errors so this was how i figured out how to fix them. catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); Debug.WriteLine(message); } } } }
static void Main(string[] args) { using (var db = new RequestsContext()) { db.Requests.Add(new Requests { Index = 5, Name = "Another Blog ", Visits = null, Date = DateTime.Now }); db.SaveChanges(); var requests = db.Requests; foreach (var request in db.Requests) { Console.WriteLine(request.Name); } var s = db.Database.Connection.Database; var t = db.Database.Connection.ConnectionString; var c = requests.Count(); } //Console.WriteLine("Hello World!"); //Console.ReadKey(); }