Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using WorldBoxV4.DAL;
- using WorldBoxV4.Models;
- namespace WorldBoxV4.Controllers
- {
- public class KlientController : Controller
- {
- private ApplicationDbContext db = new ApplicationDbContext();
- // GET: Klient
- public ActionResult Index()
- {
- var klienci = db.Klienci.Include(k => k.Adres);
- return View(klienci.ToList());
- }
- // GET: Klient/Details/5
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Klient klient = db.Klienci.Find(id);
- if (klient == null)
- {
- return HttpNotFound();
- }
- return View(klient);
- }
- // GET: Klient/Create
- public ActionResult Register()
- {
- //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc");
- return View();
- }
- // POST: Klient/Create
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Register([Bind(Include = "KlientId,Imie,Nazwisko,Nip,Pesel,Email,Password,ConfirmPassword,Login")] Klient klient,
- [Bind(Include = "Miejscowosc,Ulica,NrMieszkania,KodPocztowy")] Adres adres)
- {
- if (db.Klienci.SingleOrDefault(x => x.Login.Equals(klient.Login)) != null)
- {
- ModelState.AddModelError("Login","Login jest już zajęty!");
- }
- if (db.Klienci.SingleOrDefault(x => x.Email.Equals(klient.Email)) != null)
- {
- ModelState.AddModelError("Email", "Email jest już zajęty!");
- }
- if (db.Klienci.SingleOrDefault(x => x.Pesel.Equals(klient.Pesel)) != null)
- {
- ModelState.AddModelError("Pesel", "Pesel jest już zajęty!");
- }
- if (ModelState.IsValid)
- {
- adres.KlientId = klient.KlientId;
- db.Klienci.Add(klient);
- db.Adresy.Add(adres);
- db.SaveChanges();
- return RedirectToAction("Index","Home");
- }
- //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
- return View(klient);
- }
- public ActionResult Login()
- {
- if (Session["KlientId"] != null)
- return RedirectToAction("LoggedIn", "Klient");
- return View();
- }
- [HttpPost]
- public ActionResult Login(Klient klient)
- {
- var user = db.Klienci.FirstOrDefault(k => k.Login == klient.Login && k.Password == klient.Password);
- if (user != null)
- {
- TempData["KlientId"] = user.KlientId;
- Session["KlientId"] = klient.KlientId;
- Session["Login"] = klient.Login;
- Session.Timeout = 20;
- return Redirect("LoggedIn");
- }
- else
- {
- ModelState.AddModelError("", "Login lub hasło jest nieprawidłowe.");
- }
- return View();
- }
- public ActionResult LoggedIn()
- {
- if (Session["KlientId"] != null)
- {
- return View();
- }
- else
- {
- return RedirectToAction("Login");
- }
- }
- public ActionResult Wyloguj()
- {
- if(Session["KlientId"] != null)
- Session.Remove("KlientId");
- return RedirectToAction("Index", "Home");
- }
- // GET: Klient/Edit/5
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Klient klient = db.Klienci.Find(id);
- if (klient == null)
- {
- return HttpNotFound();
- }
- //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
- return View(klient);
- }
- // POST: Klient/Edit/5
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include = "KlientId,Imie,Nazwisko,Nip,Pesel,Email,Password,ConfirmPassword,Login")] Klient klient,
- [Bind(Include = "Miejscowosc,Ulica,NrMieszkania,KodPocztowy")] Adres adres)
- {
- if (ModelState.IsValid)
- {
- db.Entry(klient).State = EntityState.Modified;
- db.Entry(adres).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
- return View(klient);
- }
- // GET: Klient/Delete/5
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Klient klient = db.Klienci.Find(id);
- if (klient == null)
- {
- return HttpNotFound();
- }
- return View(klient);
- }
- // POST: Klient/Delete/5
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(int id)
- {
- Klient klient = db.Klienci.Find(id);
- db.Klienci.Remove(klient);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement