/src/MM.Web/Controllers/IndexController.cs
C# | 152 lines | 105 code | 31 blank | 16 comment | 4 complexity | c04454c58df618599294a317a8efb93b MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MM.Domain;
- using MM.Contract.Movies;
- using MM.Web.Models;
-
- namespace MM.Web.Controllers
- {
- public class IndexController : Controller, ICustomerController
- {
- IMovieService _movieService;
-
- public IndexController(IMovieService movieservice) {
- _movieService = movieservice;
- }
-
- //
- // GET: /Index/
-
-
- public ActionResult Index()
- {
-
- return View(_movieService.GetAllMovie());
- }
-
- //
- // GET: /Index/Create
-
- public ActionResult Create()
- {
- var viewmodel = new MovieViewModel {
- Categorias = _movieService.GetAllCategorias()
- };
- return View(viewmodel);
- }
-
- //
- // POST: /Index/Create
- private Movie Map (MovieViewModel viewModel) {
- Movie movie;
-
- if ( !viewModel.IdMovie.Equals(0) ) {
- movie = _movieService.GetMovieById(viewModel.IdMovie);
- }
- else {
- movie = new Movie();
- }
-
- movie.Name = viewModel.Nombre;
- movie.Synopsis = viewModel.Sinopsys;
- movie.Categoria = _movieService.GetCategoriaById(viewModel.IdCategoria);
- movie.Year = viewModel.Fecha;
-
- return movie;
-
- }
-
- [HttpPost]
- public ActionResult Create(MovieViewModel movie)
- {
- if (ModelState.IsValid ) {
- try {
-
- _movieService.AddMovie(Map(movie));
- return RedirectToAction("Index");
- }
- catch {
- movie.Categorias = _movieService.GetAllCategorias();
- return View(movie);
- }
- }
- movie.Categorias = _movieService.GetAllCategorias();
- return View(movie);
- }
-
-
- // GET: /Index/Edit/5
-
- public ActionResult Edit(int id)
- {
-
- MovieViewModel viewModel = new MovieViewModel {
- Nombre = _movieService.GetMovieById(id).Name,
- IdMovie = id,
- Sinopsys = _movieService.GetMovieById(id).Synopsis,
- Fecha = _movieService.GetMovieById(id).Year,
- Categorias = _movieService.GetAllCategorias(),
- IdCategoria = _movieService.GetMovieById(id).Categoria.Id
-
- };
- return View(viewModel);
- }
-
- //
- // POST: /Index/Edit/5
-
- [HttpPost]
- public ActionResult Edit(int id, MovieViewModel collection)
- {
- //Movie movie = _movieService.GetMovieById(id);
- if (ModelState.IsValid) {
- try {
-
- //UpdateModel(movie);
-
- _movieService.AddMovie(Map(collection));
- return RedirectToAction("Index");
- }
- catch {
- collection.Categorias = _movieService.GetAllCategorias();
- return View(collection);
-
- }
- }
- collection.Categorias = _movieService.GetAllCategorias();
- return View(collection);
- }
-
- //
- // GET: /Index/Delete/5
-
- public ActionResult Delete(int id)
- {
-
- return View(_movieService.GetMovieById(id));
- }
-
- //
- // POST: /Index/Delete/5
-
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
-
- {
- try
- {
- // TODO: Add delete logic here
- Movie movie = _movieService.GetMovieById(id);
- _movieService.DeleteMovie(movie);
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- }
- }