2011-02-12 23:53:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2011-02-14 08:55:54 +00:00
|
|
|
|
using System.Net;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using System.Web.Mvc;
|
2011-02-14 08:55:54 +00:00
|
|
|
|
using Portoa.Persistence;
|
2011-02-26 01:08:38 +00:00
|
|
|
|
using Portoa.Search;
|
2011-02-19 09:41:09 +00:00
|
|
|
|
using Portoa.Validation.DataAnnotations;
|
2011-02-14 02:50:33 +00:00
|
|
|
|
using Portoa.Web.Controllers;
|
2011-02-28 01:03:49 +00:00
|
|
|
|
using Portoa.Web.Filters;
|
2011-02-27 01:14:56 +00:00
|
|
|
|
using Portoa.Web.Models;
|
2011-02-14 08:55:54 +00:00
|
|
|
|
using Portoa.Web.Results;
|
2011-02-26 04:29:21 +00:00
|
|
|
|
using Portoa.Web.Security;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
|
using VideoGameQuotes.Web.Security;
|
|
|
|
|
using VideoGameQuotes.Web.Services;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
|
|
|
public class QuoteController : Controller {
|
|
|
|
|
private readonly IQuoteService quoteService;
|
2011-02-21 23:59:06 +00:00
|
|
|
|
private readonly ICurrentUserProvider<User> currentUserProvider;
|
2011-03-02 20:18:33 +00:00
|
|
|
|
private readonly ISearcher<Quote, int> quoteSearcher;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
2011-03-02 20:18:33 +00:00
|
|
|
|
public QuoteController(IQuoteService quoteService, ICurrentUserProvider<User> currentUserProvider, ISearcher<Quote, int> quoteSearcher) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
this.quoteService = quoteService;
|
|
|
|
|
this.currentUserProvider = currentUserProvider;
|
2011-02-17 05:30:31 +00:00
|
|
|
|
this.quoteSearcher = quoteSearcher;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 23:41:42 +00:00
|
|
|
|
protected new ActionResult Json(object data) {
|
|
|
|
|
return this.SerializeToJson(data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-04 08:20:20 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
|
|
|
|
public ActionResult Delete(int id) {
|
|
|
|
|
try {
|
|
|
|
|
quoteService.Delete(id);
|
|
|
|
|
return Json(this.CreateJsonResponse());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 01:14:56 +00:00
|
|
|
|
public ActionResult Browse(BrowseModel model, int page = 1) {
|
|
|
|
|
if (page < 1) {
|
|
|
|
|
return new StatusOverrideResult(View("BadPaging")) { StatusCode = HttpStatusCode.BadRequest };
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-16 10:11:55 +00:00
|
|
|
|
if (model.IsEmpty) {
|
2011-03-04 08:20:20 +00:00
|
|
|
|
return View("DefaultBrowse", quoteService.GetDefaultBrowseModel());
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 01:14:56 +00:00
|
|
|
|
model.CurrentUser = currentUserProvider.CurrentUser;
|
|
|
|
|
var pagingModel = new PagedModel<Quote> {
|
|
|
|
|
CurrentPage = page,
|
|
|
|
|
PageSize = 10
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int totalCount;
|
|
|
|
|
var quotes = quoteService.GetBrowsableQuotes(model, pagingModel.Start, pagingModel.End, out totalCount);
|
|
|
|
|
pagingModel.Records = quotes;
|
|
|
|
|
pagingModel.TotalCount = totalCount;
|
|
|
|
|
|
|
|
|
|
return View("QualifiedBrowse", new QualifiedBrowseModel(model) { PagedModel = pagingModel });
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Flag(ReportModel model) {
|
2011-02-15 01:18:23 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var quote = quoteService.GetQuote(model.QuoteId);
|
|
|
|
|
quote.AddFlag(model.Comment, model.FlagType, currentUserProvider.CurrentUser);
|
|
|
|
|
quoteService.SaveQuote(quote);
|
|
|
|
|
return Json(this.CreateJsonResponse());
|
2011-02-21 23:59:06 +00:00
|
|
|
|
} catch {
|
2011-02-15 01:18:23 +00:00
|
|
|
|
return Json(this.CreateJsonErrorResponse("Unable to create your report"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Vote(VoteModel model) {
|
2011-02-15 00:14:24 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2011-02-23 09:19:32 +00:00
|
|
|
|
var quote = quoteService.SaveQuote(quoteService.GetQuote(model.QuoteId).VoteFor(currentUserProvider.CurrentUser, model.Direction));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
var data = new {
|
|
|
|
|
score = quote.Score,
|
|
|
|
|
upVotes = quote.UpVotes,
|
|
|
|
|
downVotes = quote.DownVotes
|
2011-02-15 00:14:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Json(this.CreateJsonResponse(null, data));
|
2011-02-27 01:41:07 +00:00
|
|
|
|
} catch (CannotVoteTwiceException) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("You have already voted for this quote; you can reverse your vote by voting in the other direction"));
|
2011-02-15 00:14:24 +00:00
|
|
|
|
} catch {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("An error occurred while trying to process your vote"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 22:45:29 +00:00
|
|
|
|
public ActionResult Recent(int page) {
|
|
|
|
|
if (page < 1) {
|
|
|
|
|
return new StatusOverrideResult(View("BadPaging")) { StatusCode = HttpStatusCode.BadRequest };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int pageSize = 10;
|
|
|
|
|
int totalCount;
|
|
|
|
|
var model = new PagedModelWithUser<Quote> {
|
|
|
|
|
CurrentUser = currentUserProvider.CurrentUser,
|
|
|
|
|
CurrentPage = page,
|
|
|
|
|
PageSize = pageSize
|
|
|
|
|
};
|
|
|
|
|
|
2011-02-27 01:14:56 +00:00
|
|
|
|
model.Records = quoteService.GetRecentQuotes(model.Start, model.End, out totalCount);
|
2011-02-24 22:45:29 +00:00
|
|
|
|
model.TotalCount = totalCount;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
2011-02-14 11:01:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 04:05:23 +00:00
|
|
|
|
public ActionResult Best(int page) {
|
|
|
|
|
if (page < 1) {
|
2011-02-14 19:48:36 +00:00
|
|
|
|
return new StatusOverrideResult(View("BadPaging")) { StatusCode = HttpStatusCode.BadRequest };
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 04:05:23 +00:00
|
|
|
|
const int pageSize = 10;
|
|
|
|
|
int totalCount;
|
|
|
|
|
var model = new PagedModelWithUser<Quote> {
|
2011-02-21 23:59:06 +00:00
|
|
|
|
CurrentUser = currentUserProvider.CurrentUser,
|
2011-02-22 04:05:23 +00:00
|
|
|
|
CurrentPage = page,
|
|
|
|
|
PageSize = pageSize
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
model.Records = quoteService.GetBestQuotes(model.Start, model.End, out totalCount);
|
|
|
|
|
model.TotalCount = totalCount;
|
|
|
|
|
|
|
|
|
|
return View(model);
|
2011-02-14 19:48:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-14 19:14:13 +00:00
|
|
|
|
public ActionResult Random() {
|
|
|
|
|
var quote = quoteService.GetRandomQuote();
|
|
|
|
|
if (quote == null) {
|
|
|
|
|
return View("NoQuotes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Quote", new { id = quote.Id, text = quote.GetUrlFriendlyText() });
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 01:03:49 +00:00
|
|
|
|
[PrivateAction]
|
2011-02-28 00:38:47 +00:00
|
|
|
|
public ActionResult QuoteOfTheDay() {
|
|
|
|
|
var today = DateTime.UtcNow.DayOfYear;
|
|
|
|
|
var quote = quoteService.GetQuoteForDayOfYear(today);
|
|
|
|
|
return PartialView("QuoteOfTheDay", quote);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public ActionResult DismissFlag([GreaterThanZero]int quoteId, [GreaterThanZero]int flagId) {
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("quote/flag is invalid"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var quote = quoteService.GetQuote(quoteId);
|
|
|
|
|
var flag = quote.Flags.FirstOrDefault(f => f.Id == flagId);
|
|
|
|
|
if (flag == null) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(string.Format("Flag {0} not found for quote {1}", flagId, quote.Id)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quote.RemoveFlag(flag);
|
|
|
|
|
quoteService.SaveQuote(quote);
|
|
|
|
|
return Json(this.CreateJsonResponse());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpGet, VerifyUser(Group = UserGroup.Admin)]
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public ActionResult Edit(int id) {
|
2011-02-23 09:19:32 +00:00
|
|
|
|
if (id < 1) {
|
|
|
|
|
return View("QuoteNotFound");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
try {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
return View(ResetEditQuoteModel(new EditQuoteModel(quoteService.GetQuote(id))));
|
2011-02-19 09:41:09 +00:00
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return View("QuoteNotFound");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-03-05 22:24:50 +00:00
|
|
|
|
public ActionResult Edit(EditQuoteModel model) {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
return CreateOrEditQuote(model, "Edit");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[VerifyUser]
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public ActionResult Submit() {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
return View(ResetEditQuoteModel(new SubmitQuoteModel()));
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-02-28 10:34:46 +00:00
|
|
|
|
public ActionResult Submit(SubmitQuoteModel model) {
|
|
|
|
|
if (CaptchaUtil.Hash(model.CaptchaAnswer) != model.HashedCaptchaAnswer) {
|
|
|
|
|
ModelState.AddModelError("CaptchaAnswer", "You are not human");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
return CreateOrEditQuote(model, "Submit");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 10:34:46 +00:00
|
|
|
|
private ActionResult CreateOrEditQuote(EditQuoteModelBase model, string viewName) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
model = ResetEditQuoteModel(model);
|
2011-02-19 09:41:09 +00:00
|
|
|
|
return View(viewName, model);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
var editModel = model as EditQuoteModel;
|
|
|
|
|
|
|
|
|
|
var quote = editModel != null && editModel.QuoteId > 0
|
|
|
|
|
? quoteService.GetQuote(editModel.QuoteId)
|
2011-02-27 01:41:07 +00:00
|
|
|
|
: new Quote { Creator = currentUserProvider.CurrentUser };
|
2011-02-19 09:41:09 +00:00
|
|
|
|
|
2011-02-28 10:34:46 +00:00
|
|
|
|
quote.Game = new Game { Id = model.GameId };
|
2011-02-19 09:41:09 +00:00
|
|
|
|
quote.Text = model.QuoteText;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
2011-02-14 07:39:45 +00:00
|
|
|
|
if (model.CategoryIds != null && model.CategoryIds.Count > 0) {
|
2011-02-14 02:50:33 +00:00
|
|
|
|
quote.ClearCategories();
|
2011-02-14 07:39:45 +00:00
|
|
|
|
foreach (var categoryId in model.CategoryIds) {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
quote.AddCategory(new Category { Id = categoryId });
|
2011-02-14 02:50:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
if (quote.Game == null) {
|
2011-02-28 10:34:46 +00:00
|
|
|
|
return View(viewName, ResetEditQuoteModel(model));
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quote = quoteService.SaveQuote(quote);
|
|
|
|
|
|
2011-02-27 01:41:07 +00:00
|
|
|
|
return RedirectToAction("Quote", new { id = quote.Id, text = quote.GetUrlFriendlyText() });
|
2011-02-12 23:53:43 +00:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ModelState.AddModelError("save", e.Message);
|
2011-02-28 10:34:46 +00:00
|
|
|
|
return View(viewName, ResetEditQuoteModel(model));
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 10:34:46 +00:00
|
|
|
|
private EditQuoteModelBase ResetEditQuoteModel(EditQuoteModelBase model) {
|
2011-02-23 09:19:32 +00:00
|
|
|
|
model.CurrentUser = currentUserProvider.CurrentUser;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
model.AllGames = quoteService.GetAllGames().OrderBy(game => game.Name);
|
|
|
|
|
model.AllSystems = quoteService.GetAllSystems().OrderBy(system => system.ReleaseDate);
|
2011-02-19 09:41:09 +00:00
|
|
|
|
model.AllPublishers = quoteService.GetAllPublishers().OrderBy(publisher => publisher.Name);
|
|
|
|
|
model.AllCategories = quoteService.GetAllCategories().OrderBy(category => category.Name);
|
2011-02-28 10:34:46 +00:00
|
|
|
|
|
|
|
|
|
var submitModel = model as SubmitQuoteModel;
|
|
|
|
|
if (submitModel != null) {
|
|
|
|
|
submitModel.UnhashedCaptchaAnswer = CaptchaUtil.GetRandomAnswer();
|
|
|
|
|
submitModel.HashedCaptchaAnswer = CaptchaUtil.Hash(submitModel.UnhashedCaptchaAnswer);
|
|
|
|
|
submitModel.CaptchaAnswer = null;
|
|
|
|
|
return submitModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Quote(int id, string text = null) {
|
2011-02-14 08:55:54 +00:00
|
|
|
|
try {
|
2011-02-14 11:01:31 +00:00
|
|
|
|
var model = new QuoteModel {
|
|
|
|
|
Quote = quoteService.GetQuote(id),
|
|
|
|
|
User = currentUserProvider.CurrentUser
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(model);
|
2011-02-14 08:55:54 +00:00
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return new StatusOverrideResult(View("QuoteNotFound")) { StatusCode = HttpStatusCode.NotFound };
|
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
2011-02-14 02:50:33 +00:00
|
|
|
|
|
2011-02-21 23:59:06 +00:00
|
|
|
|
[VerifyUser(Group = UserGroup.Admin)]
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public ActionResult Flags(int id) {
|
|
|
|
|
try {
|
|
|
|
|
var model = new QuoteModel {
|
|
|
|
|
Quote = quoteService.GetQuote(id),
|
|
|
|
|
User = currentUserProvider.CurrentUser
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return new StatusOverrideResult(View("QuoteNotFound")) { StatusCode = HttpStatusCode.NotFound };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-17 05:30:31 +00:00
|
|
|
|
public ActionResult Search(string searchQuery) {
|
|
|
|
|
var model = new SearchModel {
|
|
|
|
|
User = currentUserProvider.CurrentUser,
|
|
|
|
|
Results = quoteSearcher.Search(searchQuery),
|
|
|
|
|
SearchQuery = searchQuery
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|