public bool lagreBestilling(bestilling innBestilling) { var BestillingDal = new BestillingDBMetoder(); bool lagre = BestillingDal.lagreBestilling(innBestilling); return(lagre); }
public bool lagreBestilling(bestilling innBestilling) { using (var db = new DB()) { Bestilling bestilling = new Bestilling() { fraStasjon = innBestilling.fraStasjon, tilStasjon = innBestilling.tilStasjon, dato = innBestilling.dato, returDato = innBestilling.returDato, avgang = innBestilling.avgang, returAvgang = innBestilling.returAvgang, epost = innBestilling.epost }; try { db.Bestilling.Add(bestilling); db.SaveChanges(); return(true); } catch (Exception) { return(false); } } }
public bool sjekkBestilling(bestilling innBestilling) { var BestillingDal = new BestillingDBMetoder(); bool sjekk = BestillingDal.sjekkBestilling(innBestilling); return(sjekk); }
// -------------------------------------------------------------------------------- //Metoder for å sjekke bestilling og lagre bestilling til database public bool sjekkBestilling(bestilling innBestilling) { var fraStasjon = innBestilling.fraStasjon; var tilStasjon = innBestilling.tilStasjon; var dato = innBestilling.dato; var returDato = innBestilling.returDato; var avgang = innBestilling.avgang; var returAvgang = innBestilling.returAvgang; var epost = innBestilling.epost; List <String> alleStasjonesnavn = new List <String>(); foreach (Stasjon s in db.Stasjon) { alleStasjonesnavn.Add(s.Stasjonsnavn); } //sjekker om fra og til stasjoner finnes i databasen if (!alleStasjonesnavn.Contains(fraStasjon) || !alleStasjonesnavn.Contains(tilStasjon)) { return(false); } //sjekker om til og fra stasjon er lik if (fraStasjon == tilStasjon) { return(false); } //sjekker at dato er på korrekt format DateTime date; bool validDate = DateTime.TryParseExact( dato, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (!validDate) { return(false); } //sjekker at avgang er på korrekt format DateTime time; bool validTime = DateTime.TryParseExact( avgang, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out time); if (!validTime) { return(false); } //hvis det er bestilt retur, sjekker at returdato er på korrekt format if (returDato != null) { DateTime returDate; bool validReturDate = DateTime.TryParseExact( returDato, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out returDate); if (!validReturDate) { return(false); } } //hvis det er bestilr retur, sjekker at returavgang er på korrekt format if (returAvgang != null) { DateTime returTime; bool validReturTime = DateTime.TryParseExact( returAvgang, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out returTime); if (!validReturTime) { return(false); } } if (epost == null || epost == "") { return(false); } else { //sjekker at epost er på korrekt format try { MailAddress m = new MailAddress(epost); } catch (FormatException) { return(false); } return(true); } }
public ActionResult Index(bestilling innBestilling) { var BLL = new VyBLL(); Session["Bestillingen"] = innBestilling; //sjekker om fraStasjon er valgt if (string.IsNullOrEmpty(innBestilling.fraStasjon) || innBestilling.fraStasjon == "Velg stasjon") { log.Info("Ikke valgt 'fra' stasjon"); ModelState.AddModelError("fraStasjon", "Velg stasjon å reise fra"); } //sjekker om tilStasjon er valgt if (string.IsNullOrEmpty(innBestilling.tilStasjon) || innBestilling.tilStasjon == "Velg stasjon") { log.Info("Ikke valgt 'til' stasjon"); ModelState.AddModelError("tilStasjon", "Velg stasjon å reise til "); } //sjekker om dato er valgt og på riktig format DateTime date; bool validDate = DateTime.TryParseExact( innBestilling.dato, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); if (string.IsNullOrEmpty(innBestilling.dato) || innBestilling.dato == "dd.mm.åååå") { log.Info("Ikke valgt dato"); ModelState.AddModelError("dato", "Velg dato"); } else if (!validDate) { log.Info("Dato valgt er feil format"); ModelState.AddModelError("dato", "Dato må være på korrekt format"); } //sjekker om tidspunkt er valgt og på riktig format DateTime time; bool validTime = DateTime.TryParseExact( innBestilling.avgang, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out time); if (string.IsNullOrEmpty(innBestilling.avgang) || innBestilling.avgang == "Velg tidspunkt") { log.Info("Ikke valgt tidspunkt"); ModelState.AddModelError("avgang", "Velg tidspunkt"); } else if (!validTime) { log.Info("Tidspunkt i feil format"); ModelState.AddModelError("avgang", "Tidspunkt må være på korrekt format"); } //sjekker om tur/retur er valgt og kontrollerer at returdato og klokkeslett er valgt if (innBestilling.returKnapp == "tur/retur") { DateTime returDate; bool validReturDate = DateTime.TryParseExact( innBestilling.returDato, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out returDate); if (string.IsNullOrEmpty(innBestilling.returDato) || innBestilling.returDato == "dd.mm.åååå") { log.Info("Ikke valgt returdato"); ModelState.AddModelError("returDato", "Velg returdato"); } else if (!validReturDate) { log.Info("Returdato har feil format"); ModelState.AddModelError("returdato", "Dato må være på korrekt format"); } DateTime returTime; bool validReturTime = DateTime.TryParseExact( innBestilling.returAvgang, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out returTime); if (string.IsNullOrEmpty(innBestilling.returAvgang) || innBestilling.returAvgang == "Velg tidspunkt") { log.Info("Ikke valgt returtidspunkt"); ModelState.AddModelError("returAvgang", "Velg returtidspunkt"); } else if (!validReturTime) { log.Info("Returtidspunkt feil format"); ModelState.AddModelError("returAvgang", "Tidspunkt må være på korrekt format"); } } //hvis alt er i orden, sender videre til bestilling, ellers blir stående på siden if (!ModelState.IsValid) { log.Error("Feil i håndtering av bestillingsvalidering"); } if (ModelState.IsValid) { return(RedirectToAction("Bestilling")); } return(View(innBestilling)); }
public ActionResult Index() { // For å kunne ta vare på bestillingsobjektet Session["Bestillingen"] = new bestilling(); return(View()); }
public ActionResult Bestilling(String epost) { var bestilling = (bestilling)Session["Bestillingen"]; //sjekker om epost er fylt ut på korrekt format if (string.IsNullOrEmpty(epost) || epost == "Skriv epostadresse her" || epost == "") { log.Info("Ikke skrevet inn epostadresse"); ModelState.AddModelError("epost", "Skriv inn epostadresse"); } else { try { MailAddress m = new MailAddress(epost); } catch (FormatException) { log.Info("Epost på feil format"); ModelState.AddModelError("epost", "epost må være på korrekt format"); } } var innBestilling = new bestilling() { fraStasjon = bestilling.fraStasjon, tilStasjon = bestilling.tilStasjon, dato = bestilling.dato, avgang = bestilling.avgang, returDato = bestilling.returDato, returAvgang = bestilling.returAvgang, epost = epost }; var BLL = new VyBLL(); //sjekker at alle bestillingsdata er korrekt før lagring til databasen if (BLL.sjekkBestilling(innBestilling)) { if (BLL.lagreBestilling(innBestilling)) { //sender bekreftelse på epost try { var senderEmail = new MailAddress("*****@*****.**", "VY Oppgave-1"); var receiverEmail = new MailAddress(epost, "Receiver"); var password = "******"; var sub = "Bestillingsbekreftelse"; var body = "Takk for din bestilling!"; body += "\n\nFra Stasjon: " + innBestilling.fraStasjon; body += "\nTil Stasjon: " + innBestilling.tilStasjon; body += "\nDato: " + innBestilling.dato; body += "\nKlokkeslett: " + innBestilling.avgang; if (innBestilling.returAvgang != null) { body += "\nReturdato: " + innBestilling.returDato; body += "\nReturklokkeslett: " + innBestilling.returAvgang; } var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(senderEmail.Address, password) }; using (var mess = new MailMessage(senderEmail, receiverEmail) { Subject = sub, Body = body }) { smtp.Send(mess); } } catch (Exception) { log.Error("Kunne ikke sende bekreftelse på mail"); ViewBag.save = "Kunne ikke sende bekreftelse på mail"; } return(View("Bekreftelse")); } else { log.Error("Kunne ikke lagre kjøp til database"); ViewBag.save = "Kjøp ikke gjennomført, kunne ikke lagre til database"; return(View(innBestilling)); } } else { log.Error("Feil i kjøpsdata"); ViewBag.save = "Kjøp ikke gjennomført, feil i kjøpsdata"; return(View(innBestilling)); } }