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-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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public QuoteController(IQuoteService quoteService, ICurrentUserProvider currentUserProvider) {
|
|
|
|
|
this.quoteService = quoteService;
|
|
|
|
|
this.currentUserProvider = currentUserProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-14 11:01:31 +00:00
|
|
|
|
public ActionResult Recent() {
|
|
|
|
|
//get last 10 submitted quotes
|
|
|
|
|
var quotes = quoteService.GetMostRecentQuotes(10);
|
|
|
|
|
return View(new QuoteCollectionModel { Quotes = quotes, User = currentUserProvider.CurrentUser });
|
|
|
|
|
}
|
|
|
|
|
|
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-12 23:53:43 +00:00
|
|
|
|
[IsValidUser]
|
|
|
|
|
public ActionResult Submit() {
|
|
|
|
|
var model = new QuoteSubmitModel();
|
|
|
|
|
ResetModel(model);
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, IsValidUser]
|
|
|
|
|
public ActionResult Submit(QuoteSubmitModel model) {
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
ResetModel(model);
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var quote = new Quote {
|
|
|
|
|
Creator = currentUserProvider.CurrentUser,
|
|
|
|
|
Game = GetGameFromModelData(model),
|
|
|
|
|
Text = model.QuoteText
|
|
|
|
|
};
|
|
|
|
|
|
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) {
|
|
|
|
|
ResetModel(model);
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quote = quoteService.SaveQuote(quote);
|
|
|
|
|
|
2011-02-14 07:39:45 +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);
|
|
|
|
|
ResetModel(model);
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
|
private Game GetGameFromModelData(QuoteSubmitModel model) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetModel(QuoteSubmitModel model) {
|
|
|
|
|
model.AllGames = quoteService.GetAllGames().OrderBy(game => game.Name);
|
|
|
|
|
model.AllSystems = quoteService.GetAllSystems().OrderBy(system => system.ReleaseDate);
|
|
|
|
|
model.AllPublishers = quoteService.GetAllPublishers();
|
2011-02-13 09:55:01 +00:00
|
|
|
|
model.AllCategories = quoteService.GetAllCategories();
|
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
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
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-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|