Custom logging
[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         [Authorize(Roles = "admin")]
40         public ActionResult Create()
41         {
42             ViewBag.user_id = new SelectList(db.Users, "id", "login");
43             return View();
44         }
45
46         // POST: Projects/Create
47         // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
48         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
49         [Authorize(Roles = "admin")]
50         [HttpPost]
51         [ValidateAntiForgeryToken]
52         public ActionResult Create([Bind(Include = "id,user_id,name,description")] Project project)
53         {
54             if (ModelState.IsValid)
55             {
56                 project.created_at = DateTime.Now;
57                 project.updated_at = DateTime.Now;
58                 db.Projects.Add(project);
59                 db.SaveChanges();
60                 return RedirectToAction("Index");
61             }
62
63             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
64             return View(project);
65         }
66
67         // GET: Projects/Edit/5
68         [Authorize(Roles = "admin")]
69         public ActionResult Edit(int? id)
70         {
71             if (id == null)
72             {
73                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
74             }
75             Project project = db.Projects.Find(id);
76             if (project == null)
77             {
78                 return HttpNotFound();
79             }
80             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
81             return View(project);
82         }
83
84         // POST: Projects/Edit/5
85         // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
86         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
87         [Authorize(Roles = "admin")]
88         [HttpPost]
89         [ValidateAntiForgeryToken]
90         public ActionResult Edit([Bind(Include = "id,user_id,name,description,created_at")] Project project)
91         {
92             if (ModelState.IsValid)
93             {
94                 project.updated_at = DateTime.Now;
95                 db.Entry(project).State = EntityState.Modified;
96                 db.SaveChanges();
97                 return RedirectToAction("Index");
98             }
99             ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
100             return View(project);
101         }
102
103         // GET: Projects/Delete/5
104         [Authorize(Roles = "admin")]
105         public ActionResult Delete(int? id)
106         {
107             if (id == null)
108             {
109                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
110             }
111             Project project = db.Projects.Find(id);
112             if (project == null)
113             {
114                 return HttpNotFound();
115             }
116             return View(project);
117         }
118
119         // POST: Projects/Delete/5
120         [Authorize(Roles = "admin")]
121         [HttpPost, ActionName("Delete")]
122         [ValidateAntiForgeryToken]
123         public ActionResult DeleteConfirmed(int id)
124         {
125             Project project = db.Projects.Find(id);
126             db.Projects.Remove(project);
127             db.SaveChanges();
128             return RedirectToAction("Index");
129         }
130
131         protected override void Dispose(bool disposing)
132         {
133             if (disposing)
134             {
135                 db.Dispose();
136             }
137             base.Dispose(disposing);
138         }
139     }
140 }