public IHttpActionResult PutComp(int id, Competition competition) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != competition.ID) { return(BadRequest("No Competition exists with that ID")); } // commented line for unit tests and added the mark as modified, reverse before deploying //db.MarkAsModified(competition); db.Entry(competition).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CompetitionExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Name")] Competition comp) { try { if (ModelState.IsValid) { var foundName = db.Competitions.FirstOrDefault(i => i.Name == comp.Name); if (foundName != null) { ModelState.AddModelError(string.Empty, "Competition already exists."); } else { db.Competitions.Add(comp); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(comp)); }
public IHttpActionResult PutGolfer(int id, Golfer golfer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != golfer.ID) { return(BadRequest("Golfer with ID doesn't exist")); } // commented line for unit tests and added the mark as modified, reverse before deploying db.Entry(golfer).State = EntityState.Modified; //db.MarkAsModified(golfer); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!GolferExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Surname, Firstname")] Golfer golfer) { try { if (ModelState.IsValid) { var foundName = db.Golfers.FirstOrDefault(i => i.Firstname == golfer.Firstname && i.Surname == golfer.Surname); if (foundName != null) { ModelState.AddModelError(string.Empty, "Golfer already exists."); } else { db.Golfers.Add(golfer); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(golfer)); }
public ActionResult Create([Bind(Include = "ProductID,Name,Price,Quantity,SubDepartment,ItemCategory")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public ActionResult Create([Bind(Include = "TournamentID,TournamentName,TournamentDate")] Tournament tourney) { if (ModelState.IsValid) { db.Tournaments.Add(tourney); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tourney)); }
public ActionResult Create([Bind(Include = "EventTypeID,Type")] EventType eventType) { if (ModelState.IsValid) { db.EventTypes.Add(eventType); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(eventType)); }
public ActionResult Create([Bind(Include = "CompResultID,CompetitionID,StartDate,EndDate,GolferID,Position,Season,GolferScore")] Comp_Result comp_Result) // Bind makes sure only the variables mentioned are entered { if (ModelState.IsValid) { db.Comp_Results.Add(comp_Result); db.SaveChanges(); return(RedirectToAction("ResultUploaded")); //return RedirectToAction("Index"); } ViewBag.CompetitionID = new SelectList(db.Competitions, "ID", "Name", comp_Result.CompetitionID); ViewBag.GolferID = new SelectList(db.Golfers, "ID", "Firstname", comp_Result.GolferID); return(View(comp_Result)); }
public ActionResult Index(HttpPostedFileBase CSVName) { string path = null; var ProductsToDisplay = new List <Product>(); try { if (CSVName.ContentLength > 0) { var fileName = Path.GetFileName(CSVName.FileName); path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName; CSVName.SaveAs(path); var csv = new CsvReader(new StreamReader(path)); var db = new GolfContext(); int count = 0; while (csv.Read()) { Product product = new Product(); product.Name = csv.GetField <string>(0); product.ProductID = int.Parse(csv.GetField <string>(1)); product.Price = decimal.Parse(csv.GetField <string>(2)); product.Quantity = int.Parse(csv.GetField <string>(3)); product.SubDepartment = csv.GetField <string>(7); product.ItemCategory = csv.GetField <string>(8); ProductsToDisplay.Add(product); var exists = db.Products.Where(i => i.ProductID == product.ProductID).SingleOrDefault(); if (exists == null) { db.Products.Add(product); db.SaveChanges(); count += 1; } } ViewBag.RecordsUploaded = ProductsToDisplay.Count(); ViewBag.RecordsSaved = count; } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } return(View(ProductsToDisplay)); }
public void Create(Hole newEntry) { holes.Add(newEntry); context.SaveChanges(); }
public void Create(Course newEntry) { courses.Add(newEntry); context.SaveChanges(); }
public ActionResult UploadGolfers(HttpPostedFileBase postedFile) { if (postedFile != null) { try { string fileExtension = Path.GetExtension(postedFile.FileName); //Validate uploaded file and return error. if (fileExtension != ".csv") { ViewBag.Message = "Please select the csv file with .csv extension"; return(View()); } var golfers = new List <Golfer>(); using (var sreader = new StreamReader(postedFile.InputStream)) { //First line is header. If header is not passed in csv then we can neglect the below line. string[] headers = sreader.ReadLine().Split(','); //Loop through the records while (!sreader.EndOfStream) { string[] rows = sreader.ReadLine().Split(','); golfers.Add(new Golfer { ID = int.Parse(rows[0].ToString()), // dont need Firstname = rows[1].ToString(), Surname = rows[2].ToString(), //FullName = int.Parse(rows[3].ToString()) // dont need }); } } foreach (Golfer golfer in golfers.ToList()) { // here we are checking if golfer in our upload list is already in the db var foundName = db.Golfers.FirstOrDefault(i => i.Firstname == golfer.Firstname && i.Surname == golfer.Surname); //var foundSName = db.Golfers.FirstOrDefault(i => i.Surname == golfer.Surname); if (foundName != null) { golfers.Remove(golfer); //... and if golfer is already in db we just remove from the list before uploading } else { db.Golfers.Add(golfer); db.SaveChanges(); } } return(RedirectToAction("Index", "Golfer")); } catch (Exception ex) { ViewBag.Message = ex.Message; } } else { ViewBag.Message = "Please select the file first to upload."; } return(View()); }
public void Create(Player newEntry) { players.Add(newEntry); context.SaveChanges(); }