-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoviesController.cs
More file actions
138 lines (112 loc) · 4.97 KB
/
MoviesController.cs
File metadata and controls
138 lines (112 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using eTickets.Data;
using eTickets.Data.Services;
using eTickets.Data.Static;
using eTickets.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace eTickets.Controllers
{
[Authorize(Roles = UserRoles.Admin)]
public class MoviesController : Controller
{
private readonly IMoviesService _service;
public MoviesController(IMoviesService service)
{
_service = service;
}
[AllowAnonymous]
public async Task<IActionResult> Index()
{
var allMovies = await _service.GetAllAsync(n => n.Cinema);
return View(allMovies);
}
[AllowAnonymous]
public async Task<IActionResult> Filter(string searchString)
{
var allMovies = await _service.GetAllAsync(n => n.Cinema);
if (!string.IsNullOrEmpty(searchString))
{
//var filteredResult = allMovies.Where(n => n.Name.ToLower().Contains(searchString.ToLower()) || n.Description.ToLower().Contains(searchString.ToLower())).ToList();
var filteredResultNew = allMovies.Where(n => string.Equals(n.Name, searchString, StringComparison.CurrentCultureIgnoreCase) || string.Equals(n.Description, searchString, StringComparison.CurrentCultureIgnoreCase)).ToList();
return View("Index", filteredResultNew);
}
return View("Index", allMovies);
}
//GET: Movies/Details/1
[AllowAnonymous]
public async Task<IActionResult> Details(int id)
{
var movieDetail = await _service.GetMovieByIdAsync(id);
return View(movieDetail);
}
//GET: Movies/Create
public async Task<IActionResult> Create()
{
var movieDropdownsData = await _service.GetNewMovieDropdownsValues();
ViewBag.Cinemas = new SelectList(movieDropdownsData.Cinemas, "Id", "Name");
ViewBag.Producers = new SelectList(movieDropdownsData.Producers, "Id", "FullName");
ViewBag.Actors = new SelectList(movieDropdownsData.Actors, "Id", "FullName");
return View();
}
[HttpPost]
public async Task<IActionResult> Create(NewMovieVM movie)
{
if (!ModelState.IsValid)
{
var movieDropdownsData = await _service.GetNewMovieDropdownsValues();
ViewBag.Cinemas = new SelectList(movieDropdownsData.Cinemas, "Id", "Name");
ViewBag.Producers = new SelectList(movieDropdownsData.Producers, "Id", "FullName");
ViewBag.Actors = new SelectList(movieDropdownsData.Actors, "Id", "FullName");
return View(movie);
}
await _service.AddNewMovieAsync(movie);
return RedirectToAction(nameof(Index));
}
//GET: Movies/Edit/1
public async Task<IActionResult> Edit(int id)
{
var movieDetails = await _service.GetMovieByIdAsync(id);
if (movieDetails == null) return View("NotFound");
var response = new NewMovieVM()
{
Id = movieDetails.Id,
Name = movieDetails.Name,
Description = movieDetails.Description,
Price = movieDetails.Price,
StartDate = movieDetails.StartDate,
EndDate = movieDetails.EndDate,
ImageURL = movieDetails.ImageURL,
MovieCategory = movieDetails.MovieCategory,
CinemaId = movieDetails.CinemaId,
ProducerId = movieDetails.ProducerId,
ActorIds = movieDetails.Actors_Movies.Select(n => n.ActorId).ToList(),
};
var movieDropdownsData = await _service.GetNewMovieDropdownsValues();
ViewBag.Cinemas = new SelectList(movieDropdownsData.Cinemas, "Id", "Name");
ViewBag.Producers = new SelectList(movieDropdownsData.Producers, "Id", "FullName");
ViewBag.Actors = new SelectList(movieDropdownsData.Actors, "Id", "FullName");
return View(response);
}
[HttpPost]
public async Task<IActionResult> Edit(int id, NewMovieVM movie)
{
if (id != movie.Id) return View("NotFound");
if (!ModelState.IsValid)
{
var movieDropdownsData = await _service.GetNewMovieDropdownsValues();
ViewBag.Cinemas = new SelectList(movieDropdownsData.Cinemas, "Id", "Name");
ViewBag.Producers = new SelectList(movieDropdownsData.Producers, "Id", "FullName");
ViewBag.Actors = new SelectList(movieDropdownsData.Actors, "Id", "FullName");
return View(movie);
}
await _service.UpdateMovieAsync(movie);
return RedirectToAction(nameof(Index));
}
}
}