Tuesday, April 3, 2012

Entity Framework ITHotList Controller

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using ITHotList.Models;
  9. using System.Web.Security;
  10.  
  11. namespace ITHotList.Controllers
  12. {
  13.     public class HotListController : Controller
  14.     {
  15.         private HotListEntities db = new HotListEntities();
  16.  
  17.         //
  18.         // GET: /HotList/
  19.  
  20.         [Authorize]
  21.         public ViewResult Index()
  22.         {
  23.  
  24.             MembershipUser user = Membership.GetUser();
  25.             Guid userId = (Guid)user.ProviderUserKey;
  26.  
  27.             return View(db.HotLists.Where(hotlist => hotlist.UserId == userId).OrderByDescending(hotlist => hotlist.CreateDate).ToList());
  28.         }
  29.  
  30.         //
  31.         // GET: /HotList/Details/5
  32.         [Authorize]
  33.         public ViewResult Details(int id)
  34.         {
  35.             HotList hotlist = db.HotLists.Find(id);
  36.             return View(hotlist);
  37.         }
  38.  
  39.         //
  40.         // GET: /HotList/Create
  41.         [Authorize]
  42.         public ActionResult Create()
  43.         {
  44.  
  45.             HotList hotlistModel = new HotList();
  46.             hotlistModel.Name = "Name";
  47.             hotlistModel.Resources.Add(new Resource() { Name = "", Availability= "", CurrentLocation="", ImmigrationStatus ="", JobRole="", PreferredLocation="", Rate ="", LinkToResume="", Skill="", YearsOfExp = 0   });
  48.             return View(hotlistModel);
  49.  
  50.         }
  51.  
  52.         //
  53.         // POST: /HotList/Create
  54.  
  55.         [Authorize]
  56.         [HttpPost]
  57.         public JsonResult Create(HotList receivedhotlist)
  58.         {
  59.  
  60.             // never trust what has come from ui.
  61.  
  62.             string message = string.Empty;
  63.  
  64.             try
  65.             {
  66.                 HotList hotlist = new HotList();
  67.  
  68.                 if (ModelState.IsValid)
  69.                 {
  70.                     MembershipUser user = Membership.GetUser();
  71.                     Guid userId = (Guid)user.ProviderUserKey;
  72.                     hotlist.UserId = userId;
  73.  
  74.                     try
  75.                     {
  76.  
  77.                         hotlist.Active = true;
  78.                         hotlist.Name = receivedhotlist.Name;
  79.                         hotlist.CreateDate = DateTime.Now;
  80.  
  81.                         var profile = Profile.GetProfile(user.UserName);
  82.  
  83.                         if (profile.CompanyName != null)
  84.                         {
  85.                             hotlist.CompanyName = profile.CompanyName;
  86.                         }
  87.  
  88.                         if (user.Email != null)
  89.                         {
  90.                             hotlist.Email = user.Email;
  91.                         }
  92.  
  93.                         if (profile.PrimaryPhoneNumber != null)
  94.                         {
  95.                             hotlist.PrimaryPhoneNumber = profile.PrimaryPhoneNumber;
  96.                         }
  97.  
  98.                         if (profile.PrimaryExt != null)
  99.                         {
  100.                             hotlist.PrimaryExt = profile.PrimaryExt;
  101.                         }
  102.  
  103.                         if (profile.SecondaryPhoneNumber != null)
  104.                         {
  105.                             hotlist.SecondaryPhoneNumber = profile.SecondaryPhoneNumber;
  106.                         }
  107.  
  108.                         if (profile.SecondaryExt != null)
  109.                         {
  110.                             hotlist.SecondaryExt = profile.SecondaryExt;
  111.                         }
  112.  
  113.  
  114.                         if (profile.Fax != null)
  115.                         {
  116.                             hotlist.Fax = profile.Fax;
  117.                         }
  118.  
  119.                         // Error is happening here.. need to fix it.
  120.  
  121.                     }
  122.                     catch { }
  123.  
  124.                     foreach (Resource receivedresource in receivedhotlist.Resources)
  125.                     {
  126.                         Resource resource = new Resource();
  127.  
  128.                         resource.Active = true;
  129.                         resource.UserId = userId;
  130.                         resource.JobRole = receivedresource.JobRole;
  131.                         resource.Availability = receivedresource.Availability;
  132.  
  133.                         resource.CurrentLocation = receivedresource.CurrentLocation;
  134.                         resource.ImmigrationStatus = receivedresource.ImmigrationStatus;
  135.                         resource.LinkToResume = receivedresource.LinkToResume;
  136.                         resource.Name = receivedresource.Name;
  137.                         resource.PreferredLocation = receivedresource.PreferredLocation;
  138.                         resource.Rate = receivedresource.Rate;
  139.                         resource.Skill = receivedresource.Skill;
  140.                         resource.YearsOfExp = receivedresource.YearsOfExp;
  141.  
  142.                         resource.HotLists.Add(hotlist);
  143.                         hotlist.Resources.Add(resource);
  144.  
  145.                         db.Resources.Add(resource);
  146.  
  147.                     }
  148.  
  149.                     db.HotLists.Add(hotlist);
  150.  
  151.                     db.SaveChanges();
  152.  
  153.                     //do the persistence logic here
  154.                     message = "SUCCESS";
  155.  
  156.                 }
  157.                 else
  158.                 {
  159.                     message = "modelstate is invalid";
  160.                 }
  161.  
  162.                
  163.             }
  164.             catch (Exception ex)
  165.             {
  166.                 message = ex.Message.ToString();
  167.             }
  168.             
  169.             return Json(message);
  170.         }
  171.  
  172.  
  173.  
  174.         //[HttpPost]
  175.         //public ActionResult Create(HotList hotlist)
  176.         //{
  177.         //    if (ModelState.IsValid)
  178.         //    {
  179.  
  180.         //        MembershipUser user = Membership.GetUser();
  181.         //        Guid userId = (Guid)user.ProviderUserKey;
  182.         //        hotlist.UserId = userId;
  183.         //        hotlist.CreateDate = DateTime.Now;
  184.         //        db.HotLists.Add(hotlist);
  185.         //        db.SaveChanges();
  186.         //        return RedirectToAction("Index");
  187.         //    }
  188.  
  189.         //    return View(hotlist);
  190.         //}
  191.  
  192.         //
  193.         // GET: /HotList/Edit/5
  194.         [Authorize]
  195.         public ActionResult Edit(int id)
  196.         {
  197.             HotList hotlist = db.HotLists.Find(id);
  198.             return View(hotlist);
  199.         }
  200.  
  201.         //
  202.         // POST: /HotList/Edit/5
  203.         [Authorize]
  204.         [HttpPost]
  205.         public ActionResult Edit(HotList hotlist)
  206.         {
  207.             if (ModelState.IsValid)
  208.             {
  209.                 db.Entry(hotlist).State = EntityState.Modified;
  210.                 db.SaveChanges();
  211.                 return RedirectToAction("Index");
  212.             }
  213.             return View(hotlist);
  214.         }
  215.  
  216.         //
  217.         // GET: /HotList/Delete/5
  218.         [Authorize]
  219.         public ActionResult Delete(int id)
  220.         {
  221.             HotList hotlist = db.HotLists.Find(id);
  222.             return View(hotlist);
  223.         }
  224.  
  225.         //
  226.         // POST: /HotList/Delete/5
  227.         [Authorize]
  228.         [HttpPost, ActionName("Delete")]
  229.         public ActionResult DeleteConfirmed(int id)
  230.         {
  231.             HotList hotlist = db.HotLists.Find(id);
  232.             db.HotLists.Remove(hotlist);
  233.             db.SaveChanges();
  234.             return RedirectToAction("Index");
  235.         }
  236.  
  237.         protected override void Dispose(bool disposing)
  238.         {
  239.             db.Dispose();
  240.             base.Dispose(disposing);
  241.         }
  242.     }
  243. }

No comments:

Post a Comment