2011-02-12 23:53:43 +00:00
|
|
|
|
using System;
|
2011-02-14 02:50:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
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;
|
|
|
|
|
using JetBrains.Annotations;
|
2011-02-14 08:55:54 +00:00
|
|
|
|
using Portoa.Persistence;
|
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-14 08:55:54 +00:00
|
|
|
|
using Portoa.Web.Results;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
2011-02-17 05:30:31 +00:00
|
|
|
|
using VideoGameQuotes.Api.Search;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
|
using VideoGameQuotes.Web.Security;
|
|
|
|
|
using VideoGameQuotes.Web.Services;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
|
|
|
public class QuoteController : Controller {
|
|
|
|
|
private readonly IQuoteService quoteService;
|
|
|
|
|
private readonly ICurrentUserProvider currentUserProvider;
|
2011-02-17 05:30:31 +00:00
|
|
|
|
private readonly IQuoteSearcher quoteSearcher;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
2011-02-17 05:30:31 +00:00
|
|
|
|
public QuoteController(IQuoteService quoteService, ICurrentUserProvider currentUserProvider, IQuoteSearcher 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-02-16 02:48:11 +00:00
|
|
|
|
public ActionResult Browse(BrowseModel model) {
|
2011-02-16 10:11:55 +00:00
|
|
|
|
if (model.IsEmpty) {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
return View("DefaultBrowse");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
return View("QualifiedBrowse", new QualifiedBrowseModel(model) {
|
|
|
|
|
Quotes = quoteService.GetBrowsableQuotes(model),
|
|
|
|
|
User = currentUserProvider.CurrentUser
|
2011-02-17 05:30:31 +00:00
|
|
|
|
});
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-15 01:18:23 +00:00
|
|
|
|
[HttpPost, IsValidUser]
|
|
|
|
|
public JsonResult Report(ReportModel model) {
|
|
|
|
|
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-19 09:41:09 +00:00
|
|
|
|
} catch (Exception e) {
|
2011-02-15 01:18:23 +00:00
|
|
|
|
return Json(this.CreateJsonErrorResponse("Unable to create your report"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-15 00:14:24 +00:00
|
|
|
|
[HttpPost, IsValidUser]
|
|
|
|
|
public JsonResult Vote(VoteModel model) {
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var vote = quoteService.GetVoteOrCreateNew(quoteService.GetQuote(model.QuoteId), currentUserProvider.CurrentUser);
|
|
|
|
|
vote.Direction = model.Direction;
|
2011-02-16 02:48:11 +00:00
|
|
|
|
|
2011-02-15 00:14:24 +00:00
|
|
|
|
vote = quoteService.SaveVote(vote);
|
|
|
|
|
|
|
|
|
|
var data = new Dictionary<string, string> {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
{ "netVotes", vote.Quote.Score.ToString() },
|
2011-02-15 00:14:24 +00:00
|
|
|
|
{ "upVotes", vote.Quote.UpVotes.ToString() },
|
|
|
|
|
{ "downVotes", vote.Quote.DownVotes.ToString() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Json(this.CreateJsonResponse(null, data));
|
|
|
|
|
} catch {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("An error occurred while trying to process your vote"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-14 11:01:31 +00:00
|
|
|
|
public ActionResult Recent() {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
return View(new QuoteCollectionModel {
|
|
|
|
|
Quotes = quoteService.GetMostRecentQuotes(10),
|
|
|
|
|
User = currentUserProvider.CurrentUser
|
2011-02-15 00:18:29 +00:00
|
|
|
|
});
|
2011-02-14 11:01:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-14 19:48:36 +00:00
|
|
|
|
public ActionResult Best(int start = 0, int end = 19) {
|
|
|
|
|
if (start < 0 || end <= 0 || start > end) {
|
|
|
|
|
return new StatusOverrideResult(View("BadPaging")) { StatusCode = HttpStatusCode.BadRequest };
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-16 02:48:11 +00:00
|
|
|
|
return View(new PagedQuoteCollectionModel {
|
|
|
|
|
Quotes = quoteService.GetBestQuotes(start, end),
|
2011-02-14 19:48:36 +00:00
|
|
|
|
User = currentUserProvider.CurrentUser,
|
|
|
|
|
Start = start,
|
|
|
|
|
End = end
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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-19 09:41:09 +00:00
|
|
|
|
[HttpPost, IsValidUser(Group = UserGroup.Admin)]
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet, IsValidUser(Group = UserGroup.Admin)]
|
|
|
|
|
public ActionResult Edit(int id) {
|
|
|
|
|
try {
|
|
|
|
|
var model = new EditQuoteModel(quoteService.GetQuote(id));
|
|
|
|
|
ResetEditQuoteModel(model);
|
|
|
|
|
return View(model);
|
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return View("QuoteNotFound");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, IsValidUser(Group = UserGroup.Admin)]
|
|
|
|
|
public ActionResult Edit(EditQuoteModel model) {
|
|
|
|
|
return CreateOrEditQuote(model, "Edit");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
[IsValidUser]
|
|
|
|
|
public ActionResult Submit() {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
var model = new EditQuoteModel();
|
|
|
|
|
ResetEditQuoteModel(model);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, IsValidUser]
|
2011-02-19 09:41:09 +00:00
|
|
|
|
public ActionResult Submit(EditQuoteModel model) {
|
|
|
|
|
return CreateOrEditQuote(model, "Submit");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ActionResult CreateOrEditQuote(EditQuoteModel model, string viewName) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
ResetEditQuoteModel(model);
|
|
|
|
|
return View(viewName, model);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
var quote = model.QuoteId > 0
|
|
|
|
|
? quoteService.GetQuote(model.QuoteId)
|
|
|
|
|
: new Quote {Creator = currentUserProvider.CurrentUser};
|
|
|
|
|
|
|
|
|
|
quote.Game = GetGameFromModelData(model);
|
|
|
|
|
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) {
|
|
|
|
|
quote.AddCategory(quoteService.GetCategory(categoryId));
|
2011-02-14 02:50:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
if (quote.Game == null) {
|
2011-02-19 09:41:09 +00:00
|
|
|
|
ResetEditQuoteModel(model);
|
|
|
|
|
return View(viewName, model);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quote = quoteService.SaveQuote(quote);
|
|
|
|
|
|
2011-02-19 09:41:09 +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-19 09:41:09 +00:00
|
|
|
|
ResetEditQuoteModel(model);
|
|
|
|
|
return View(viewName, model);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CanBeNull]
|
2011-02-19 09:41:09 +00:00
|
|
|
|
private Game GetGameFromModelData(EditQuoteModel model) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
if (model.GameId > 0) {
|
|
|
|
|
try {
|
|
|
|
|
return quoteService.GetGame(model.GameId);
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Exception("Unable to find game with ID " + model.GameId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.GameName)) {
|
|
|
|
|
ModelState.AddModelError("GameName", "Game name must be non-empty");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//construct game from model data
|
|
|
|
|
var game = new Game {
|
|
|
|
|
Name = model.GameName,
|
2011-02-13 09:55:01 +00:00
|
|
|
|
Region = model.GameRegions,
|
2011-02-12 23:53:43 +00:00
|
|
|
|
Website = model.GameWebsite,
|
|
|
|
|
Creator = currentUserProvider.CurrentUser
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//publishers
|
|
|
|
|
if ((model.PublisherIds != null && model.PublisherIds.Count > 0) || !string.IsNullOrEmpty(model.PublisherName)) {
|
|
|
|
|
//modifying publishers, so remove errthing
|
|
|
|
|
game.ClearPublishers();
|
|
|
|
|
|
|
|
|
|
if (model.PublisherIds != null && model.PublisherIds.Count > 0) {
|
|
|
|
|
foreach (var publisherId in model.PublisherIds) {
|
|
|
|
|
game.AddPublisher(quoteService.GetPublisher(publisherId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.PublisherName)) {
|
|
|
|
|
game.AddPublisher(new Publisher {
|
|
|
|
|
Name = model.PublisherName,
|
|
|
|
|
Website = model.PublisherWebsite
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//systems
|
|
|
|
|
if ((model.SystemIds != null && model.SystemIds.Count > 0) || !string.IsNullOrEmpty(model.SystemName)) {
|
|
|
|
|
//modifying systems, so remove errthing
|
|
|
|
|
game.ClearSystems();
|
|
|
|
|
|
|
|
|
|
if (model.SystemIds != null && model.SystemIds.Count > 0) {
|
|
|
|
|
foreach (var systemId in model.SystemIds) {
|
|
|
|
|
game.AddSystem(quoteService.GetSystem(systemId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.SystemName)) {
|
|
|
|
|
game.AddSystem(new GamingSystem {
|
|
|
|
|
Name = model.SystemName,
|
|
|
|
|
Abbreviation = model.SystemAbbreviation,
|
|
|
|
|
ReleaseDate = model.SystemReleaseDate
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return game;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 09:41:09 +00:00
|
|
|
|
private void ResetEditQuoteModel(EditQuoteModel model) {
|
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-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Quote(int id) {
|
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-19 09:41:09 +00:00
|
|
|
|
[IsValidUser(Group = UserGroup.Admin)]
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, IsValidUser]
|
2011-02-14 02:50:33 +00:00
|
|
|
|
public JsonResult CreateCategory(Category category) {
|
|
|
|
|
try {
|
|
|
|
|
category = quoteService.SaveCategory(category);
|
|
|
|
|
var data = new Dictionary<string, string> { { "categoryId", category.Id.ToString() }, { "categoryName", category.Name } };
|
|
|
|
|
return Json(this.CreateJsonResponse(null, data));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|