vgquotes/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs

257 lines
7.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2011-02-14 08:55:54 +00:00
using System.Net;
using System.Web.Mvc;
2011-02-14 08:55:54 +00:00
using Portoa.Persistence;
using Portoa.Validation.DataAnnotations;
using Portoa.Web;
using Portoa.Web.Controllers;
2011-02-14 08:55:54 +00:00
using Portoa.Web.Results;
using VideoGameQuotes.Api;
2011-02-17 05:30:31 +00:00
using VideoGameQuotes.Api.Search;
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<User> currentUserProvider;
2011-02-17 05:30:31 +00:00
private readonly IQuoteSearcher quoteSearcher;
public QuoteController(IQuoteService quoteService, ICurrentUserProvider<User> currentUserProvider, IQuoteSearcher quoteSearcher) {
this.quoteService = quoteService;
this.currentUserProvider = currentUserProvider;
2011-02-17 05:30:31 +00:00
this.quoteSearcher = quoteSearcher;
}
public ActionResult Browse(BrowseModel model) {
if (model.IsEmpty) {
return View("DefaultBrowse");
}
return View("QualifiedBrowse", new QualifiedBrowseModel(model) {
Quotes = quoteService.GetBrowsableQuotes(model),
User = currentUserProvider.CurrentUser
2011-02-17 05:30:31 +00:00
});
}
[HttpPost, VerifyUser]
2011-02-15 01:18:23 +00:00
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());
} catch {
2011-02-15 01:18:23 +00:00
return Json(this.CreateJsonErrorResponse("Unable to create your report"));
}
}
[HttpPost, VerifyUser]
2011-02-15 00:14:24 +00:00
public JsonResult Vote(VoteModel model) {
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Invalid request"));
}
try {
var quote = quoteService.SaveQuote(quoteService.GetQuote(model.QuoteId).VoteFor(currentUserProvider.CurrentUser, model.Direction));
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));
} catch {
return Json(this.CreateJsonErrorResponse("An error occurred while trying to process your vote"));
}
}
public ActionResult Recent() {
return View(new QuoteCollectionModel {
Quotes = quoteService.GetMostRecentQuotes(10),
User = currentUserProvider.CurrentUser
2011-02-15 00:18:29 +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> {
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() });
}
[HttpPost, VerifyUser(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, VerifyUser(Group = UserGroup.Admin)]
public ActionResult Edit(int id) {
if (id < 1) {
return View("QuoteNotFound");
}
try {
var model = new EditQuoteModel(quoteService.GetQuote(id));
ResetEditQuoteModel(model);
return View(model);
} catch (EntityNotFoundException) {
return View("QuoteNotFound");
}
}
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public ActionResult Edit(EditQuoteModel model) {
return CreateOrEditQuote(model, "Edit");
}
[VerifyUser]
public ActionResult Submit() {
var model = new EditQuoteModel();
ResetEditQuoteModel(model);
return View(model);
}
[HttpPost, VerifyUser]
public ActionResult Submit(EditQuoteModel model) {
return CreateOrEditQuote(model, "Submit");
}
private ActionResult CreateOrEditQuote(EditQuoteModel model, string viewName) {
if (!ModelState.IsValid) {
ResetEditQuoteModel(model);
return View(viewName, model);
}
try {
var quote = model.QuoteId > 0
? quoteService.GetQuote(model.QuoteId)
: new Quote {Creator = currentUserProvider.CurrentUser};
quote.Game = quoteService.GetGame(model.GameId);
quote.Text = model.QuoteText;
if (model.CategoryIds != null && model.CategoryIds.Count > 0) {
quote.ClearCategories();
foreach (var categoryId in model.CategoryIds) {
quote.AddCategory(quoteService.GetCategory(categoryId));
}
}
if (quote.Game == null) {
ResetEditQuoteModel(model);
return View(viewName, model);
}
quote = quoteService.SaveQuote(quote);
return RedirectToAction("Quote", new {id = quote.Id, text = quote.GetUrlFriendlyText()});
} catch (Exception e) {
ModelState.AddModelError("save", e.Message);
ResetEditQuoteModel(model);
return View(viewName, model);
}
}
private void ResetEditQuoteModel(EditQuoteModel model) {
model.CurrentUser = currentUserProvider.CurrentUser;
model.AllGames = quoteService.GetAllGames().OrderBy(game => game.Name);
model.AllSystems = quoteService.GetAllSystems().OrderBy(system => system.ReleaseDate);
model.AllPublishers = quoteService.GetAllPublishers().OrderBy(publisher => publisher.Name);
model.AllCategories = quoteService.GetAllCategories().OrderBy(category => category.Name);
}
public ActionResult Quote(int id) {
2011-02-14 08:55:54 +00:00
try {
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 };
}
}
[VerifyUser(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, VerifyUser]
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);
}
}
}