PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MM.Web/Controllers/IndexController.cs

#
C# | 152 lines | 105 code | 31 blank | 16 comment | 4 complexity | c04454c58df618599294a317a8efb93b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MM.Domain;
  7. using MM.Contract.Movies;
  8. using MM.Web.Models;
  9. namespace MM.Web.Controllers
  10. {
  11. public class IndexController : Controller, ICustomerController
  12. {
  13. IMovieService _movieService;
  14. public IndexController(IMovieService movieservice) {
  15. _movieService = movieservice;
  16. }
  17. //
  18. // GET: /Index/
  19. public ActionResult Index()
  20. {
  21. return View(_movieService.GetAllMovie());
  22. }
  23. //
  24. // GET: /Index/Create
  25. public ActionResult Create()
  26. {
  27. var viewmodel = new MovieViewModel {
  28. Categorias = _movieService.GetAllCategorias()
  29. };
  30. return View(viewmodel);
  31. }
  32. //
  33. // POST: /Index/Create
  34. private Movie Map (MovieViewModel viewModel) {
  35. Movie movie;
  36. if ( !viewModel.IdMovie.Equals(0) ) {
  37. movie = _movieService.GetMovieById(viewModel.IdMovie);
  38. }
  39. else {
  40. movie = new Movie();
  41. }
  42. movie.Name = viewModel.Nombre;
  43. movie.Synopsis = viewModel.Sinopsys;
  44. movie.Categoria = _movieService.GetCategoriaById(viewModel.IdCategoria);
  45. movie.Year = viewModel.Fecha;
  46. return movie;
  47. }
  48. [HttpPost]
  49. public ActionResult Create(MovieViewModel movie)
  50. {
  51. if (ModelState.IsValid ) {
  52. try {
  53. _movieService.AddMovie(Map(movie));
  54. return RedirectToAction("Index");
  55. }
  56. catch {
  57. movie.Categorias = _movieService.GetAllCategorias();
  58. return View(movie);
  59. }
  60. }
  61. movie.Categorias = _movieService.GetAllCategorias();
  62. return View(movie);
  63. }
  64. // GET: /Index/Edit/5
  65. public ActionResult Edit(int id)
  66. {
  67. MovieViewModel viewModel = new MovieViewModel {
  68. Nombre = _movieService.GetMovieById(id).Name,
  69. IdMovie = id,
  70. Sinopsys = _movieService.GetMovieById(id).Synopsis,
  71. Fecha = _movieService.GetMovieById(id).Year,
  72. Categorias = _movieService.GetAllCategorias(),
  73. IdCategoria = _movieService.GetMovieById(id).Categoria.Id
  74. };
  75. return View(viewModel);
  76. }
  77. //
  78. // POST: /Index/Edit/5
  79. [HttpPost]
  80. public ActionResult Edit(int id, MovieViewModel collection)
  81. {
  82. //Movie movie = _movieService.GetMovieById(id);
  83. if (ModelState.IsValid) {
  84. try {
  85. //UpdateModel(movie);
  86. _movieService.AddMovie(Map(collection));
  87. return RedirectToAction("Index");
  88. }
  89. catch {
  90. collection.Categorias = _movieService.GetAllCategorias();
  91. return View(collection);
  92. }
  93. }
  94. collection.Categorias = _movieService.GetAllCategorias();
  95. return View(collection);
  96. }
  97. //
  98. // GET: /Index/Delete/5
  99. public ActionResult Delete(int id)
  100. {
  101. return View(_movieService.GetMovieById(id));
  102. }
  103. //
  104. // POST: /Index/Delete/5
  105. [HttpPost]
  106. public ActionResult Delete(int id, FormCollection collection)
  107. {
  108. try
  109. {
  110. // TODO: Add delete logic here
  111. Movie movie = _movieService.GetMovieById(id);
  112. _movieService.DeleteMovie(movie);
  113. return RedirectToAction("Index");
  114. }
  115. catch
  116. {
  117. return View();
  118. }
  119. }
  120. }
  121. }