forked from Manatee777/MessagingPlatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagesController.cs
178 lines (162 loc) · 5.88 KB
/
MessagesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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 MManatee.Models;
namespace MManatee.Controllers
{
public class MessagesController : Controller
{
private MManateeEntities db = new MManateeEntities();
// GET: Messages
public ActionResult Index()
{
var messageSet = db.MessageSet.Include(m => m.Type).Where(u => u.OwnedBy == User.Identity.Name).Where(t => t.Type.Name == "Inbox").OrderByDescending(o => o.DateTime); //include vyhodí více výsledků, where je profiltruje
return View(messageSet.ToList());
}
// GET: Messages/Details/5
public ActionResult Details(int? id) //nullovatelný typ
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Message message = db.MessageSet.Find(id);
message.ReadState = true;
db.SaveChanges();
if (message == null)
{
return HttpNotFound();
}
return View(message);
}
// GET: Messages/Create
[Authorize]
public ActionResult Create()
{
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name");
return View();
}
// POST: Messages/Create
// Chcete-li zajistit ochranu před útoky typu OVERPOST, povolte konkrétní vlastnosti, k nimž chcete vytvořit vazbu.
// Další informace viz http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult Create([Bind(Include = "MessageID,Subject,Body,OwnedBy,CreatedBy,DateTime,ReadState,TypeID")] Message message)
{
if (ModelState.IsValid)
{
message.CreatedBy = User.Identity.Name;
message.DateTime = DateTime.Now;
message.ReadState = false;
message.TypeID= 2;
db.MessageSet.Add(message);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name", message.TypeID);
return View(message);
}
// GET: Messages/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Message message = db.MessageSet.Find(id);
if (message == null)
{
return HttpNotFound();
}
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name", message.TypeID);
return View(message);
}
// POST: Messages/Edit/5
// Chcete-li zajistit ochranu před útoky typu OVERPOST, povolte konkrétní vlastnosti, k nimž chcete vytvořit vazbu.
// Další informace viz http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "MessageID,Subject,Body,OwnedBy,CreatedBy,DateTime,ReadState,TypeID")] Message message)
{
if (ModelState.IsValid)
{
db.Entry(message).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name", message.TypeID);
return View(message);
}
// GET: Messages/Delete/5
[Authorize]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Message message = db.MessageSet.Find(id);
if (message == null)
{
return HttpNotFound();
}
return View(message);
}
// POST: Messages/Delete/5
[HttpPost, ActionName("Delete")]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Message message = db.MessageSet.Find(id);
db.MessageSet.Remove(message);
db.SaveChanges();
return RedirectToAction("Index");
}
[Authorize]
public ActionResult Response(int? id)
{
var message = db.MessageSet.Find(id);
message.Subject = "";
message.Body = "";
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name");
return View(message);
}
[HttpPost, ActionName("Response")] //actionname dodaná kvůli shodě actionresults
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult ResponseConfirmed([Bind(Include = "MessageID,Subject,Body,OwnedBy,CreatedBy,DateTime,ReadState,TypeID")] Message message, string over)
{
if (ModelState.IsValid)
{
message.CreatedBy = User.Identity.Name;
message.DateTime = DateTime.Now;
message.ReadState = false;
message.TypeID = 1;
db.MessageSet.Add(message);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.TypeID = new SelectList(db.TypeSet, "TypeID", "Name", message.TypeID);
return View(message);
}
public ActionResult SwitchBox()
{
return RedirectToAction("Index", "Notify");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}