public ActionResult EditListing(EditListingViewModel model) { CoreysListEntities db = new CoreysListEntities(); try { // if the listing ID is -1 then it is a new listing if (model.Listing.ListingID == -1) { // add new listing Listing listing = new Listing(); listing.CityID = model.Listing.CityID; listing.UserID = Convert.ToInt32(Session["UserId"]); listing.SubCategoryID = model.Listing.SubCategoryID; listing.Headline = model.Listing.Headline; listing.Location = model.Listing.Location; listing.Description = model.Listing.Description; listing.Price = model.Listing.Price; listing.CreatedBy = "corey"; listing.CreatedDate = DateTime.Now; db.Listings.Add(listing); db.SaveChanges(); } else { // get the listing being edited Listing listing = db.Listings.FirstOrDefault(l => l.ListingID == model.Listing.ListingID); // update all the fields and save listing.CityID = model.Listing.CityID; listing.UserID = Convert.ToInt32(Session["UserId"]); listing.SubCategoryID = model.Listing.SubCategoryID; listing.Headline = model.Listing.Headline; listing.Location = model.Listing.Location; listing.Description = model.Listing.Description; listing.Price = model.Listing.Price; listing.ModifiedBy = listing.User.Email; listing.ModifiedDate = DateTime.Now; db.SaveChanges(); } } catch (Exception e) { throw e; } // return success and handle navigation in jquery return(Json(new { Success = true }, JsonRequestBehavior.AllowGet)); }
// called when the user wants to activate or deactivate a listing public ActionResult ListingActivation(int listingId) { // establish connection to database CoreysListEntities db = new CoreysListEntities(); // create variable to get the current activation status string activationStatus; // get the selected listing to update Listing listing = db.Listings.FirstOrDefault(l => l.ListingID == listingId); try { // change the active listing status listing.IsActive = listing.IsActive ? false : true; // save the changes db.SaveChanges(); // get the new activation status activationStatus = listing.IsActive ? "active" : "notActive"; // return json with the new status return(Json(new { Success = true, ActivationStatus = activationStatus })); } catch (Exception e) { // if there was a problem with the return send error return(Json(new { Success = false, Error = "Activation or Deactivation Failed: " + e.Message })); } }
// when the user submits signup public ActionResult Create_Account(LoginViewModel model) { // establish connection to database CoreysListEntities db = new CoreysListEntities(); try { // Checking to see if the email entered already exist in database User testUser = db.Users.FirstOrDefault(u => u.Email == model.Email); // if not create and add new user if (testUser == null) { User newUser = new User(); newUser.FirstName = model.FirstName; newUser.LastName = model.LastName; newUser.Email = model.Email; newUser.PhoneNum = Convert.ToString(model.PhoneNumber.Replace("-", "")); newUser.Password = model.Password; newUser.CreatedBy = "Corey"; newUser.CreatedDate = DateTime.Now; // save the new user to the database db.Users.Add(newUser); db.SaveChanges(); // set the new users information in session variables Session["UserId"] = newUser.UserID; Session["UserEmail"] = newUser.Email; // send user to homepage UserHomeViewModel userHomeModel = new UserHomeViewModel(); return(View("UserHome", userHomeModel)); } else { // else return error message informing user that email already is registered model.CreateAccountErrorMessage = "Email already in use"; return(View("Index", model)); } } catch (Exception e) { // exception connecting to database string error = e.Message; return(View("Index", model)); } }
public ActionResult AccountUpdate(User updatedUserInfo) { // get the user from the database and update thier information CoreysListEntities db = new CoreysListEntities(); User user = db.Users.FirstOrDefault(u => u.UserID == updatedUserInfo.UserID); user.PhoneNum = updatedUserInfo.PhoneNum; user.FirstName = updatedUserInfo.FirstName; user.LastName = updatedUserInfo.LastName; user.Password = updatedUserInfo.Password; // save the changes made to the user db.SaveChanges(); // send the user to their homepage UserHomeViewModel userHomeModel = new UserHomeViewModel(); return(View("UserHome", userHomeModel)); }
static void Main(string[] args) { //connect to databas CoreysListEntities db = new CoreysListEntities(); //get all cities List <City> allCities = db.Cities.OrderBy(c => c.CityName).ToList(); //create a counter to track when to sleep thread due to api calls limit per sec int counter = 0; //for each city foreach (City c in allCities) { //if api call = 5 sleep the thread if (counter == 5) { Console.WriteLine(); Console.WriteLine("---Waiting on API------"); Console.WriteLine(); System.Threading.Thread.Sleep(5000); counter = 0; } //call the get postal code function string postalCode = GetPostalCode(c.CityName.Trim(), c.State.StateName); //get a reference for the correct city to update City updateCity = db.Cities.FirstOrDefault(u => u.CityID == c.CityID); //set the new value updateCity.PostalCode = postalCode; //save db.SaveChanges(); Console.WriteLine(c.CityName + " " + c.State.StateName + ": " + postalCode + " Record Updated"); counter++; } //pause application Console.WriteLine(); Console.WriteLine("Updates Finished....."); Console.ReadLine(); }
// called when user clicks delete linke in editImages partial public ActionResult Delete(int id) { CoreysListEntities db = new CoreysListEntities(); // get reference to image CoreysList.Entity.Image imgToDelete = db.Images.FirstOrDefault(i => i.ImageID == id); // get the listing associated with the image int listingId = imgToDelete.ListingID; // remove row from table and save db.Images.Remove(imgToDelete); db.SaveChanges(); // return partial EditListingImagesViewModel editListingImagesViewModel = new EditListingImagesViewModel(listingId); return(PartialView("~/views/Accounts/_EditListingImages.cshtml", editListingImagesViewModel)); }
// called when a user uploads images recieving the listing id public ActionResult Upload(int id) { int listingId = id; CoreysListEntities db = new CoreysListEntities(); // for each of the requested files for (int i = 0; i < Request.Files.Count; i++) { try { // create a new [image] CoreysList.Entity.Image newImage = new CoreysList.Entity.Image(); // Uploaded file HttpPostedFileBase file = Request.Files[i]; // Get the size of the file newImage.ImageSize = file.ContentLength; // get the file name newImage.FileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1); // get the type of file .jpg .gif .png etc.. newImage.ImageType = file.ContentType; // create a new byte array to fit the content size byte[] imageData = new byte[file.ContentLength]; // read in the file withe the byte array and content size file.InputStream.Read(imageData, 0, (int)file.ContentLength); // reposition the input stream to the beginning file.InputStream.Position = 0; // stream the file again into a System.Drawing.Image System.Drawing.Image sysImg = System.Drawing.Image.FromStream(file.InputStream); // assign the sizes from system image to coreyslist image newImage.ImageHeight = sysImg.Height; newImage.ImageWidth = sysImg.Width; newImage.ImageContent = imageData; // create encoding object to send image type ImageEncoding imgResizerEnc = new ImageEncoding(); // set the values for thumb images int thumbHeight = 75; int thumbWidth = 75; // switch statement to get the content type switch (file.ContentType) { case "image/jpeg": imgResizerEnc = ImageEncoding.Jpg90; break; case "image/gif": imgResizerEnc = ImageEncoding.Gif; break; case "image/png": imgResizerEnc = ImageEncoding.Png; break; } // create a resizer and send the image content ImageResizer resizer = new ImageResizer(imageData); // call the resizer method along with the desired height, width and img type byte[] thumbData = resizer.Resize(thumbHeight, thumbWidth, imgResizerEnc); // save the new thumb data for the coreyslist image entity newImage.ThumbContent = thumbData; newImage.ThumbSize = thumbData.Length; newImage.ThumbWidth = thumbWidth; newImage.ThumbHeight = thumbHeight; // connect image to the correct listing through listing ID newImage.ListingID = listingId; newImage.CreatedDate = DateTime.Now; newImage.CreatedBy = System.Web.HttpContext.Current.Session["UserId"].ToString(); // To save file, use SaveAs method db.Images.Add(newImage); db.SaveChanges(); } catch (Exception e) { throw e; } } // Return partial view EditListingImagesViewModel editListingImagesViewModel = new EditListingImagesViewModel(listingId); return(PartialView("~/views/Accounts/_EditListingImages.cshtml", editListingImagesViewModel)); }