Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using WorldBoxV4.DAL;
  10. using WorldBoxV4.Models;
  11.  
  12. namespace WorldBoxV4.Controllers
  13. {
  14.     public class KlientController : Controller
  15.     {
  16.         private ApplicationDbContext db = new ApplicationDbContext();
  17.  
  18.         // GET: Klient
  19.         public ActionResult Index()
  20.         {
  21.             var klienci = db.Klienci.Include(k => k.Adres);
  22.             return View(klienci.ToList());
  23.         }
  24.  
  25.         // GET: Klient/Details/5
  26.         public ActionResult Details(int? id)
  27.         {
  28.             if (id == null)
  29.             {
  30.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  31.             }
  32.             Klient klient = db.Klienci.Find(id);
  33.             if (klient == null)
  34.             {
  35.                 return HttpNotFound();
  36.             }
  37.             return View(klient);
  38.         }
  39.  
  40.         // GET: Klient/Create
  41.         public ActionResult Register()
  42.         {
  43.             //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc");
  44.             return View();
  45.         }
  46.  
  47.         // POST: Klient/Create
  48.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  49.         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  50.         [HttpPost]
  51.         [ValidateAntiForgeryToken]
  52.         public ActionResult Register([Bind(Include = "KlientId,Imie,Nazwisko,Nip,Pesel,Email,Password,ConfirmPassword,Login")] Klient klient,
  53.                                    [Bind(Include = "Miejscowosc,Ulica,NrMieszkania,KodPocztowy")] Adres adres)
  54.         {
  55.             if (db.Klienci.SingleOrDefault(x => x.Login.Equals(klient.Login)) != null)
  56.             {
  57.                 ModelState.AddModelError("Login","Login jest już zajęty!");
  58.             }
  59.             if (db.Klienci.SingleOrDefault(x => x.Email.Equals(klient.Email)) != null)
  60.             {
  61.                 ModelState.AddModelError("Email", "Email jest już zajęty!");
  62.             }
  63.             if (db.Klienci.SingleOrDefault(x => x.Pesel.Equals(klient.Pesel)) != null)
  64.             {
  65.                 ModelState.AddModelError("Pesel", "Pesel jest już zajęty!");
  66.             }
  67.  
  68.             if (ModelState.IsValid)
  69.             {
  70.                 adres.KlientId = klient.KlientId;
  71.                 db.Klienci.Add(klient);
  72.                 db.Adresy.Add(adres);
  73.                 db.SaveChanges();
  74.                 return RedirectToAction("Index","Home");
  75.             }
  76.  
  77.             //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
  78.             return View(klient);
  79.         }
  80.  
  81.         public ActionResult Login()
  82.         {
  83.             if (Session["KlientId"] != null)
  84.                 return RedirectToAction("LoggedIn", "Klient");
  85.             return View();
  86.         }
  87.         [HttpPost]
  88.         public ActionResult Login(Klient klient)
  89.         {
  90.             var user = db.Klienci.FirstOrDefault(k => k.Login == klient.Login && k.Password == klient.Password);
  91.  
  92.             if (user != null)
  93.             {
  94.                 TempData["KlientId"] = user.KlientId;
  95.                 Session["KlientId"] = klient.KlientId;
  96.                 Session["Login"] = klient.Login;
  97.                 Session.Timeout = 20;
  98.                 return Redirect("LoggedIn");
  99.             }
  100.             else
  101.             {
  102.                 ModelState.AddModelError("", "Login lub hasło jest nieprawidłowe.");
  103.             }
  104.  
  105.             return View();
  106.         }
  107.  
  108.         public ActionResult LoggedIn()
  109.         {
  110.             if (Session["KlientId"] != null)
  111.             {
  112.                 return View();
  113.             }
  114.             else
  115.             {
  116.                 return RedirectToAction("Login");
  117.             }
  118.         }
  119.  
  120.         public ActionResult Wyloguj()
  121.         {
  122.             if(Session["KlientId"] != null)
  123.                 Session.Remove("KlientId");
  124.             return RedirectToAction("Index", "Home");
  125.         }
  126.  
  127.         // GET: Klient/Edit/5
  128.         public ActionResult Edit(int? id)
  129.         {
  130.             if (id == null)
  131.             {
  132.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  133.             }
  134.             Klient klient = db.Klienci.Find(id);
  135.             if (klient == null)
  136.             {
  137.                 return HttpNotFound();
  138.             }
  139.             //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
  140.             return View(klient);
  141.         }
  142.  
  143.         // POST: Klient/Edit/5
  144.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  145.         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  146.         [HttpPost]
  147.         [ValidateAntiForgeryToken]
  148.         public ActionResult Edit([Bind(Include = "KlientId,Imie,Nazwisko,Nip,Pesel,Email,Password,ConfirmPassword,Login")] Klient klient,
  149.                                  [Bind(Include = "Miejscowosc,Ulica,NrMieszkania,KodPocztowy")] Adres adres)
  150.         {
  151.             if (ModelState.IsValid)
  152.             {
  153.                 db.Entry(klient).State = EntityState.Modified;
  154.                 db.Entry(adres).State = EntityState.Modified;
  155.                 db.SaveChanges();
  156.                 return RedirectToAction("Index");
  157.             }
  158.             //ViewBag.KlientId = new SelectList(db.Adresy, "KlientId", "Miejscowosc", klient.KlientId);
  159.             return View(klient);
  160.         }
  161.  
  162.         // GET: Klient/Delete/5
  163.         public ActionResult Delete(int? id)
  164.         {
  165.             if (id == null)
  166.             {
  167.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  168.             }
  169.             Klient klient = db.Klienci.Find(id);
  170.             if (klient == null)
  171.             {
  172.                 return HttpNotFound();
  173.             }
  174.             return View(klient);
  175.         }
  176.  
  177.         // POST: Klient/Delete/5
  178.         [HttpPost, ActionName("Delete")]
  179.         [ValidateAntiForgeryToken]
  180.         public ActionResult DeleteConfirmed(int id)
  181.         {
  182.             Klient klient = db.Klienci.Find(id);
  183.             db.Klienci.Remove(klient);
  184.             db.SaveChanges();
  185.             return RedirectToAction("Index");
  186.         }
  187.  
  188.         protected override void Dispose(bool disposing)
  189.         {
  190.             if (disposing)
  191.             {
  192.                 db.Dispose();
  193.             }
  194.             base.Dispose(disposing);
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement