using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
System.Web.Mvc.Ajax;
using
MvcApplication.Models;
using
MvcApplication.Repositories;
namespace
MvcApplication.Controllers {
public
class
GuestbookController : Controller {
private
IGuestbookRepository gr;
public
GuestbookController(IGuestbookRepository repository) {
gr = repository;
}
public
ActionResult Index(
int
? page) {
const
int
pageSize = 5;
var
guestbook = gr.GetAllMessages();
var
pagedGuestbook =
new
PaginatedList<mvc_guestbook>(guestbook,
page ?? 0,
pageSize);
return
View(pagedGuestbook);
}
public
ActionResult AjaxIndex(
int
? page) {
const
int
pageSize = 5;
var
guestbook = gr.GetAllMessages();
var
pagedGuestbook =
new
PaginatedList<mvc_guestbook>(guestbook,
page ?? 0,
pageSize);
return
PartialView(
"Messages"
, pagedGuestbook);
}
[AcceptVerbs(HttpVerbs.Get)]
public
ActionResult Create() {
if
(Session[Request.ServerVariables[
"REMOTE_ADDR"
].ToString()] !=
null
? DateTime.Now.Subtract(Convert.ToDateTime(Session[Request.ServerVariables[
"REMOTE_ADDR"
].ToString()])).Minutes >= 5 :
true
) {
mvc_Guestbook guestbook =
new
mvc_Guestbook();
return
View(guestbook);
}
else
{
Session[
"DELAY"
] = (5 + Convert.ToDateTime(Session[Request.ServerVariables[
"REMOTE_ADDR"
].ToString()]).Subtract(DateTime.Now).Minutes).ToString();
return
View(
"Denied"
);
}
}
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Create(mvc_Guestbook guestbook) {
if
(ModelState.IsValid) {
try
{
guestbook.MessageDate = DateTime.Now;
gr.Save(guestbook);
Session[Request.ServerVariables[
"REMOTE_ADDR"
].ToString()] = DateTime.Now.ToString();
return
RedirectToAction(
"Details"
,
new
{ id = guestbook.GuestbookID });
}
catch
{
ModelState.AddModelError(
"Error"
,
"Error saving message"
);
}
}
return
View();
}
[Authorize]
[AcceptVerbs(HttpVerbs.Get)]
public
ActionResult Edit(
int
id) {
mvc_Guestbook guestbook = gr.GetMessage(id);
if
(guestbook ==
null
) {
return
View(
"Not Found"
);
}
else
{
return
View(guestbook);
}
}
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Edit(
int
id, FormCollection collection) {
mvc_Guestbook guestbook = gr.GetMessage(id);
try
{
UpdateModel(guestbook);
gr.Update(guestbook);
return
RedirectToAction(
"Details"
,
new
{ id = guestbook.GuestbookID });
}
catch
{
ModelState.AddModelError(
"Error"
,
"Error saving message"
);
return
View(guestbook);
}
}
[Authorize]
[AcceptVerbs(HttpVerbs.Get)]
public
ActionResult Delete(
int
id) {
mvc_Guestbook guestbook = gr.GetMessage(id);
if
(guestbook ==
null
) {
return
View(
"Not Found"
);
}
else
{
return
View(guestbook);
}
}
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Delete(
int
id,
string
confirmButton) {
mvc_Guestbook guestbook = gr.GetMessage(id);
if
(guestbook ==
null
) {
return
View(
"Not Found"
);
}
else
{
gr.Delete(guestbook);
return
View(
"Deleted"
);
}
}
public
ActionResult Details(
int
id) {
mvc_Guestbook guestbook = gr.GetMessage(id);
if
(guestbook ==
null
) {
return
View(
"NotFound"
);
}
else
{
return
View(guestbook);
}
}
}
}