Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smashdevcode/3da13530a3d9f5a6860fa1dccffacbdb to your computer and use it in GitHub Desktop.
Save smashdevcode/3da13530a3d9f5a6860fa1dccffacbdb to your computer and use it in GitHub Desktop.
using ComicBookLibraryManagerWebApp.ViewModels;
using ComicBookShared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
namespace ComicBookLibraryManagerWebApp.Controllers
{
public class ComicBookArtistsController_QueriesAndCommands : BaseController
{
public ActionResult Add(int comicBookId)
{
var comicBook = Context.ComicBooks
.Include(cb => cb.Series)
.Include(cb => cb.Artists.Select(a => a.Artist))
.Include(cb => cb.Artists.Select(a => a.Role))
.Where(cb => cb.Id == comicBookId)
.SingleOrDefault();
if (comicBook == null)
{
return HttpNotFound();
}
var viewModel = new ComicBookArtistsAddViewModel()
{
ComicBook = comicBook
};
viewModel.Init(Repository);
return View(viewModel);
}
[HttpPost]
public ActionResult Add(ComicBookArtistsAddViewModel viewModel)
{
ValidateComicBookArtist(viewModel);
if (ModelState.IsValid)
{
var comicBookArtist = new ComicBookArtist()
{
ComicBookId = viewModel.ComicBookId,
ArtistId = viewModel.ArtistId,
RoleId = viewModel.RoleId
};
Context.ComicBookArtists.Add(comicBookArtist);
Context.SaveChanges();
TempData["Message"] = "Your artist was successfully added!";
return RedirectToAction("Detail", "ComicBooks", new { id = viewModel.ComicBookId });
}
viewModel.ComicBook = Context.ComicBooks
.Include(cb => cb.Series)
.Include(cb => cb.Artists.Select(a => a.Artist))
.Include(cb => cb.Artists.Select(a => a.Role))
.Where(cb => cb.Id == viewModel.ComicBookId)
.SingleOrDefault();
viewModel.Init(Repository);
return View(viewModel);
}
public ActionResult Delete(int comicBookId, int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var comicBookArtist = Context.ComicBookArtists
.Include(cba => cba.Artist)
.Include(cba => cba.Role)
.Include(cba => cba.ComicBook.Series)
.Where(cba => cba.Id == id)
.SingleOrDefault();
if (comicBookArtist == null)
{
return HttpNotFound();
}
return View(comicBookArtist);
}
[HttpPost]
public ActionResult Delete(int comicBookId, int id)
{
var comicBookArtist = new ComicBookArtist() { Id = id };
Context.Entry(comicBookArtist).State = EntityState.Deleted;
Context.SaveChanges();
TempData["Message"] = "Your artist was successfully deleted!";
return RedirectToAction("Detail", "ComicBooks", new { id = comicBookId });
}
private void ValidateComicBookArtist(ComicBookArtistsAddViewModel viewModel)
{
// If there aren't any "ArtistId" and "RoleId" field validation errors...
if (ModelState.IsValidField("ArtistId") &&
ModelState.IsValidField("RoleId"))
{
// Then make sure that this artist and role combination
// doesn't already exist for this comic book.
if (Context.ComicBookArtists
.Any(cba => cba.ComicBookId == viewModel.ComicBookId &&
cba.ArtistId == viewModel.ArtistId &&
cba.RoleId == viewModel.RoleId))
{
ModelState.AddModelError("ArtistId",
"This artist and role combination already exists for this comic book.");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment