示例#1
0
        public ActionResult Create(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewMovieViewModel()
                {
                    Movie  = movie,
                    Genres = db.Genres.ToList()
                };
                return(View("New", viewModel));
            }
            var movieInDB = db.Movie.SingleOrDefault(m => m.Id == movie.Id);

            if (movieInDB == null)
            {
                db.Movie.Add(movie);
            }
            else
            {
                movieInDB.Name          = movie.Name;
                movieInDB.NumberInStock = movie.NumberInStock;
                movieInDB.ReleasedDate  = movie.ReleasedDate;
                movieInDB.GenreId       = movie.GenreId;
            }
            db.SaveChanges();
            return(RedirectToAction("Index", "Movies"));
        }
        public ActionResult Create(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerViewModel()
                {
                    Customer        = customer,
                    MembershipTypes = db.MembershipTypes.ToList()
                };
                return(View("New", viewModel));
            }
            var customerInDB = db.Customer.SingleOrDefault(customerID => customerID.Id == customer.Id);

            if (customerInDB == null)
            {
                db.Customer.Add(customer);
            }
            else
            {
                customerInDB.Name      = customer.Name;
                customerInDB.BirthDate = customer.BirthDate;
                customerInDB.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
                customerInDB.MembershipTypeId         = customer.MembershipTypeId;
            }

            db.SaveChanges();

            return(RedirectToAction("Index", "Customers"));
        }
示例#3
0
        public static void LogException(Exception e, Models.Fault Fault)
        {
            try
            {
                string eInnerString = "No InnerException";
                if (e.InnerException != null)
                {
                    eInnerString = e.InnerException.ToString();
                }
                using (var db = new Models.Database())
                {
                    var exception = new Models.ExceptionLog();
                    exception.Data       = e.Data.ToString();
                    exception.Fault      = Fault.ToString();
                    exception.Message    = e.Message;
                    exception.Time       = DateTime.UtcNow;
                    exception.Exception  = eInnerString;
                    exception.StackTrace = e.StackTrace;
                    exception.Method     = e.TargetSite.Name;
                    exception.Source     = e.Source;

                    db.ExceptionLogs.Add(exception);

                    db.SaveChanges();
                    System.Diagnostics.Debug.WriteLine("Exception was logged to the database successfully.");
                }
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Failed to log exception to database. We probably broke that too. :(");
            }
        }
示例#4
0
 public static bool addPanel(Models.LedPanel panel)
 {
     using (var db = new Models.Database())
     {
         db.LedPanels.Add(panel);
         db.SaveChanges();
     }
     return true;
 }
示例#5
0
 private static void LinkTargetLocationToTweet(int TweetID, int TargetLocationID)
 {
     using (var db = new Models.Database())
     {
         var status = db.TwitterStatuses.First(ts => ts.ID == TweetID);
         var Target = db.TwitterLocations.First(tl => tl.ID == TargetLocationID);
         Target.Status = status;
         db.SaveChanges();
     }
 }
示例#6
0
        private void SaveToDiskDbAndTweet(byte[] bytes)
        {
            string fileName = this.GetFileName();

            File.WriteAllBytes(fileName, bytes);
            System.Diagnostics.Debug.WriteLine("Wrote file: " + fileName);

            int imageID;

            using (var db = new Models.Database())
            {
                var robot = db.Robots.FirstOrDefault(l => l.ID == this.ROBOT_ID);

                var image = new Models.Image()
                {
                    iRobot        = robot,
                    FileDirectory = fileName,
                    TimeAdded     = DateTime.UtcNow,
                    TimeCreated   = DateTime.UtcNow,
                    TimeTaken     = DateTime.UtcNow
                };

                db.Images.Add(image);

                db.SaveChanges();
                imageID = image.ID;
            }

            System.Diagnostics.Debug.WriteLine("Saved Image to db with ID: " + imageID);

            string TweetText = string.Empty;

            //get stuffs from markov factory
            try
            {
                TweetText = Helpers.Markov.GetNextTwitterPictureMarkov();
            }
            catch (Exception e)
            {
                TweetText = "Wasn't able to generate Markov.";
                System.Diagnostics.Debug.WriteLine("Generating Markov threw an error.");
                Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
            }

            Helpers.FileOperations.ImageOperations.ApplyTextToImage(TweetText, fileName);

            try
            {
                Helpers.Twitter.PostTweetWithImage(this.ROBOT_ID, imageID, TweetText);
            }
            catch (Exception e)
            {
                Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
            }
        }
 public ActionResult Excluir(IEnumerable <int> Ids)
 {
     if (Ids != null && Ids.Count() > 0)
     {
         List <Product> products =
             db.Product.Where(c => Ids.Contains(c.Id)).ToList();
         products.ForEach(c =>
         {
             db.Entry(c).State = EntityState.Deleted;
         });
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
示例#8
0
 public static bool addFace(Models.Face face)
 {
     using (var db = new Models.Database())
     {
         foreach (var panel in face.Panels)
         {
             addPanel(panel);
             db.LedPanels.Attach(panel);
         }
         db.Passwords.Attach(face.UserAccount);
         db.Faces.Add(face);
         db.SaveChanges();
     }
     return true;
 }
示例#9
0
 public ActionResult Register(Models.Customer inCustomer)
 {
     using (var db = new Models.Database())
     {
         try
         {
             db.Customer.Add(inCustomer);
             db.SaveChanges();
         }
         catch (Exception ex)
         {
             //Must add something here
         }
     }
     return(RedirectToAction("Index"));
 }
示例#10
0
 public bool UpdateHitchBotLocation(int HitchBotID, double Latitude, double Longitude, double Altitude, float Accuracy, float Velocity, string TakenTime)
 {
     DateTime StartTimeReal = DateTime.ParseExact(TakenTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
     using (var db = new Models.Database())
     {
         var hitchBOT = db.hitchBOTs.Include(l => l.Locations).First(h => h.ID == HitchBotID);
         var location = new Location();
         location.Latitude = Latitude;
         location.Longitude = Longitude;
         location.Altitude = Altitude;
         location.Accuracy = Accuracy;
         location.Velocity = Velocity;
         location.TakenTime = StartTimeReal;
         location.TimeAdded = DateTime.UtcNow;
         location.HitchBOT = hitchBOT;
         db.Locations.Add(location);
         db.SaveChanges();
         hitchBOT.Locations.Add(location);
         db.SaveChanges();
     }
     return true;
 }
        protected void buttonSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                var wikiEntry = inputWiki1.InnerText;
                var radius = inputRadiusValue.Value;
                var name = inputName.Value;
                var lat = inputLat.Value;
                var lng = inputLong.Value;

                double? radiusActual = null;
                double latActual;
                double lngActual;

                using (var db = new Models.Database())
                {
                    var user = (Models.Password)Session["New"];
                    var hitchbot = db.hitchBOTs.Include(l => l.WikipediaEntries).First(l => l.ID == user.hitchBOT.ID);

                    Models.Location location = null;

                    if (LocationCheckBox.Checked)
                    {
                        /*
                        nullable double parse code borrowed from
                        http://stackoverflow.com/questions/3390750/how-to-use-int-tryparse-with-nullable-int
                        */
                        double tmp;

                        if (!double.TryParse(radius, out tmp))
                        {
                            setErrorMessage("Selected Radius is not valid!");
                            return;
                        }
                        radiusActual = tmp;

                        if (!double.TryParse(lat, out latActual))
                        {
                            setErrorMessage("Latitude is not valid number!");
                            return;
                        }

                        if (!double.TryParse(lng, out lngActual))
                        {
                            setErrorMessage("Longitude is not valid number!");
                            return;
                        }

                        location = new Models.Location
                        {
                            Latitude = latActual,
                            Longitude = lngActual,
                            TimeAdded = DateTime.UtcNow,
                            TakenTime = DateTime.UtcNow
                        };

                        db.Locations.Add(location);
                        db.SaveChanges();

                    }

                    var wiki = new Models.WikipediaEntry
                    {
                        TargetLocation = location,
                        WikipediaText = wikiEntry,
                        EntryName = name,
                        RadiusKM = radiusActual,
                        HitchBot = hitchbot,
                        TimeAdded = DateTime.UtcNow
                    };

                    db.WikipediaEntries.Add(wiki);

                    db.SaveChanges();
                }

                Response.Redirect("AddTargetSuccess.aspx");
            }
            catch
            {
                setErrorMessage("An unknown error occurred. let the sys admin know you saw this message.");
            }
        }
示例#12
0
        public static async void PostTweetWithImage(int iRobotID, int ImageID, string TweetText = "")
        {
            if (string.IsNullOrWhiteSpace(TweetText))
            {
                try
                {
                    TweetText = Helpers.Markov.GetNextTwitterPictureMarkov();
                }
                catch (Exception e)
                {
                    TweetText = "Wasn't able to generate Markov.";
                    System.Diagnostics.Debug.WriteLine("Generating Markov threw an error.");
                    Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
                }
            }
            using (var db = new Models.Database())
            {
                string UserID;
                var    twitterContext = GetContext(iRobotID, out UserID);

                var image = db.Images.FirstOrDefault(l => l.ID == ImageID);

                byte[] imageBytes;

                System.Diagnostics.Debug.WriteLine(Helpers.FileOperations.TextOverlayString(image.FileDirectory));

                if (Helpers.FileOperations.TextOverlayExists(image.FileDirectory))
                {
                    imageBytes = Helpers.FileOperations.GetFileBytes(Helpers.FileOperations.TextOverlayString(image.FileDirectory));
                }
                else
                {
                    imageBytes = Helpers.FileOperations.GetFileBytes(image.FileDirectory);
                }

                try
                {
                    Status tweet = await twitterContext.TweetWithMediaAsync(TweetText, false, imageBytes);

                    System.Diagnostics.Debug.WriteLine("Tweet with image sent successfully: " + TweetText);

                    try
                    {
                        var TwitterAccount = db.Robots.First(l => l.ID == iRobotID).TwitterAccount;

                        var tweetText = new Models.TweetedText()
                        {
                            TweetText      = TweetText,
                            TweetID        = tweet.StatusID.ToString(),
                            TimeAdded      = DateTime.UtcNow,
                            ImageTweet     = true,
                            TwitterAccount = TwitterAccount
                        };

                        db.Tweets.Add(tweetText);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Unable to save Tweet to database.");
                        Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
                    }
                }
                catch (TwitterQueryException e)
                {
                    System.Diagnostics.Debug.WriteLine("Something went wrong with tweeting that image!");
                    Handlers.ExceptionLogger.LogException(e, Models.Fault.Unknown);
                }
            }
        }
示例#13
0
        private void Button_Remove_Image(object sender, System.EventArgs e)
        {
            Button b = (Button)sender;
            using (var db = new Models.Database())
            {
                int ID = int.Parse(b.CommandArgument);
                var img = db.Images.First(i => i.ID == ID);

                img.TimeDenied = DateTime.UtcNow;

                db.Images.Attach(img);

                db.ChangeTracker.DetectChanges();

                db.SaveChanges();
            }

            Response.Redirect("ViewImages.aspx", true);
        }
示例#14
0
        public static string GetEncodedPolyLine(int HitchBotID)
        {
            using (var db = new Models.Database())
            {
                var OrderedLocations = db.hitchBOTs.Include(h => h.Locations).First(h => h.ID == HitchBotID).Locations.Where(l => l.TakenTime > new DateTime(2014, 07, 27, 13, 30, 0)).OrderBy(l => l.TakenTime).ToList();
                string tempRegionString = string.Empty;
                string tempURL = EncodeCoordsForGMAPS(SlimLocations(OrderedLocations));
                if (OrderedLocations.Count > 0)
                {
                    var mostRecent = OrderedLocations.Last();
                    try
                    {
                        tempRegionString = GetRegion(mostRecent);
                    }
                    catch
                    {

                    }
                    tempURL += gmapsMarkerString + Math.Round(mostRecent.Latitude, 3) + "," + Math.Round(mostRecent.Longitude, 3);
                }
                else
                {
                    tempRegionString = "Apparently my devs don't know where I am.. They should probably be fixing this and not doing $insert_awesome_activity_here$";
                }

                var hitchBOT = db.hitchBOTs.First(h => h.ID == HitchBotID);

                var tempStaticLink = new Models.GoogleMapsStatic()
                {
                    HitchBot = hitchBOT,
                    URL = tempURL,
                    NearestCity = tempRegionString,
                    ViewCount = 1,
                    TimeGenerated = DateTime.UtcNow,
                    TimeAdded = DateTime.UtcNow

                };

                db.StaticMaps.Add(tempStaticLink);
                db.SaveChanges();

                return tempURL;
            }
        }
示例#15
0
        public static async void UploadImageAndAddToDb(int hbID, DateTime TimeTaken, string localFileDirectory, string fileName)
        {
            Task<string> publicURL = UploadImageAndGetPublicUrl(localFileDirectory, fileName);

            using (var db = new Models.Database())
            {
                var hb = db.hitchBOTs.Include(l => l.Locations).FirstOrDefault(l => l.ID == hbID);
                var location = hb.Locations.OrderByDescending(l => l.TakenTime).FirstOrDefault();

                var image = new Models.Image()
                {
                    HitchBOT = hb,
                    Location = location,
                    url = await publicURL,
                    TimeAdded = DateTime.UtcNow,
                    TimeTaken = TimeTaken
                };

                db.Images.Add(image);
                db.SaveChanges();
            }
        }
示例#16
0
        public bool UpdateHitchBotLocationMin(string HitchBotID, string Latitude, string Longitude, string TakenTime)
        {
            DateTime StartTimeReal = DateTime.ParseExact(TakenTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
            int newLocationID;
            int hitchBotID;
            using (var db = new Models.Database())
            {
                hitchBotID = int.Parse(HitchBotID);
                double LatDouble = double.Parse(Latitude);
                double LongDouble = double.Parse(Longitude);
                var hitchBOT = db.hitchBOTs.Include(h => h.Locations).First(h => h.ID == hitchBotID);
                var location = new Location()
                {
                    Latitude = LatDouble,
                    Longitude = LongDouble,
                    TakenTime = StartTimeReal,
                    TimeAdded = DateTime.UtcNow,
                    HitchBOT = hitchBOT
                };

                db.Locations.Add(location);
                db.SaveChanges();
                //hitchBOT.Locations.Add(location);
                //db.SaveChanges();

                newLocationID = location.ID;
            }

            //Helpers.LocationHelper.CheckForTargetLocation(hitchBotID, newLocationID);
            return true;
        }
示例#17
0
        public static async void PostTweetText(int iRobot = 1, string TweetText = "")
        {
            if (string.IsNullOrWhiteSpace(TweetText))
            {
                try
                {
                    TweetText = Helpers.Markov.GetNextTwitterMarkov();
                }
                catch (Exception e)
                {
                    TweetText = "Wasn't able to generate Markov.";
                    System.Diagnostics.Debug.WriteLine("Generating Markov threw an error.");
                    Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
                }
            }

            using (var db = new Models.Database())
            {
                try
                {
                    string UserID;
                    var    twitterContext = GetContext(iRobot, out UserID);
                    Status response       = await twitterContext.TweetAsync(TweetText);

                    System.Diagnostics.Debug.WriteLine("Tweet sent successfully: " + TweetText);

                    try
                    {
                        var TwitterAccount = db.Robots.First(l => l.ID == iRobot).TwitterAccount;

                        var tweetText = new Models.TweetedText()
                        {
                            TweetText      = TweetText,
                            TweetID        = response.StatusID.ToString(),
                            TimeAdded      = DateTime.UtcNow,
                            ImageTweet     = false,
                            TwitterAccount = TwitterAccount
                        };

                        db.Tweets.Add(tweetText);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Unable to save Tweet to database.");
                        Handlers.ExceptionLogger.LogException(e, Models.Fault.Server);
                    }
                }
                //catch (TwitterQueryException e)
                //{
                //    return e.ToString();
                //}
                //catch (InvalidOperationException e)
                //{
                //    return e.ToString();
                //}
                //catch (System.Data.SqlClient.SqlException e)
                //{
                //    return e.ToString();
                //}
                //catch (System.NotSupportedException e)
                //{
                //    return e.ToString();
                //}
                catch (Exception e)
                {
                    Handlers.ExceptionLogger.LogException(e, Models.Fault.Unknown);
                }
            }
        }