PageRenderTime 38ms CodeModel.GetById 17ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

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

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