b6f57e603962ab524df4f0eeb5fcf27c5cfd70eb
[wsti_pai.git] / Projects / Controllers / ProjectsController.cs
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
10 namespace Projects.Models
11 {
12     public class ProjectsController : Controller
13     {
14         private ProjectsDBEntities db = new ProjectsDBEntities();
15
16         // GET: Projects
17         public ActionResult Index()
18         {
19             var projects = db.Projects.Include(p => p.User);
20             return View(projects.ToList());
21         }
22
23         // GET: Projects/Details/5
24         public ActionResult Details(int? id)
25         {
26             if (id == null)
27             {
28                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
29             }
30             Project project = db.Projects.Find(id);
31             if (project == null)
32             {
33                 return HttpNotFound();
34             }
35             return View(project);
36         }
37
38         // GET: Projects/Create
39         public ActionResult Create()
40         {
41             ViewBag.user_id = new SelectList(db.Users, "id", "login");
42             return View();
43         }
44
45         // POST: Projects/Create
46         // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
47         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
48         [HttpPost]
49         [ValidateAntiForgeryToken]
50         public ActionResult Create([Bind(Include = "id,user_id,name,description")] Project project)
51         {
52             if (ModelState.IsValid)
53             {
54                 project.created_at = DateTime.Now;
55                 project.updated_at = DateTime.Now;
56                 db.Projects.Add(project);
57                 db.SaveChanges();
58                 return RedirectToAction("Index");
59             }
60
61             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
62             return View(project);
63         }
64
65         // GET: Projects/Edit/5
66         public ActionResult Edit(int? id)
67         {
68             if (id == null)
69             {
70                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
71             }
72             Project project = db.Projects.Find(id);
73             if (project == null)
74             {
75                 return HttpNotFound();
76             }
77             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
78             return View(project);
79         }
80
81         // POST: Projects/Edit/5
82         // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
83         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
84         [HttpPost]
85         [ValidateAntiForgeryToken]
86         public ActionResult Edit([Bind(Include = "id,user_id,name,description,created_at")] Project project)
87         {
88             if (ModelState.IsValid)
89             {
90                 project.updated_at = DateTime.Now;
91                 db.Entry(project).State = EntityState.Modified;
92                 db.SaveChanges();
93                 return RedirectToAction("Index");
94             }
95             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
96             return View(project);
97         }
98
99         // GET: Projects/Delete/5
100         public ActionResult Delete(int? id)
101         {
102             if (id == null)
103             {
104                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
105             }
106             Project project = db.Projects.Find(id);
107             if (project == null)
108             {
109                 return HttpNotFound();
110             }
111             return View(project);
112         }
113
114         // POST: Projects/Delete/5
115         [HttpPost, ActionName("Delete")]
116         [ValidateAntiForgeryToken]
117         public ActionResult DeleteConfirmed(int id)
118         {
119             Project project = db.Projects.Find(id);
120             db.Projects.Remove(project);
121             db.SaveChanges();
122             return RedirectToAction("Index");
123         }
124
125         protected override void Dispose(bool disposing)
126         {
127             if (disposing)
128             {
129                 db.Dispose();
130             }
131             base.Dispose(disposing);
132         }
133     }
134 }