public IActionResult Upsert(int?id) { IEnumerable <Movie> movieList = _unitOfWork.Movie.GetAll(); IEnumerable <Auditorium> auditoriumList = _unitOfWork.Auditorium.GetAll(); UpsertVM screeningVM = new UpsertVM() { Screening = new Screening(), MovieList = movieList.Select(m => new SelectListItem { Text = m.Title, Value = m.Id.ToString() }), AuditoriumList = auditoriumList.Select(a => new SelectListItem { Text = a.Name, Value = a.Id.ToString() }) }; if (id == null) { return(View(screeningVM)); } //for edit screeningVM.Screening = _unitOfWork.Screening.Get(id.GetValueOrDefault()); if (screeningVM == null) { return(NotFound()); } return(View(screeningVM)); }
public IActionResult Upsert(int?id) { IEnumerable <Genre> GenList = _unitOfWork.Genre.GetAll(); UpsertVM movieVM = new UpsertVM() { Movie = new Movie(), GenreList = GenList.Select(g => new SelectListItem { Text = g.Name, Value = g.Id.ToString() }) }; if (id == null) { return(View(movieVM)); } //for edit movieVM.Movie = _unitOfWork.Movie.Get(id.GetValueOrDefault()); if (movieVM == null) { return(NotFound()); } return(View(movieVM)); }
public IActionResult Upsert(UpsertVM movieVM) { if (ModelState.IsValid) { string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images\movies"); var extenstion = Path.GetExtension(files[0].FileName); if (movieVM.Movie.ImageUrl != null) { //this is an edit and we need to remove old image var imagePath = Path.Combine(webRootPath, movieVM.Movie.ImageUrl.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create)) { files[0].CopyTo(filesStreams); } movieVM.Movie.ImageUrl = @"\images\movies\" + fileName + extenstion; } else { //update when they do not change the image if (movieVM.Movie.Id != 0) { Movie objFromDb = _unitOfWork.Movie.Get(movieVM.Movie.Id); movieVM.Movie.ImageUrl = objFromDb.ImageUrl; } } if (movieVM.Movie.Id == 0) { _unitOfWork.Movie.Add(movieVM.Movie); } else { _unitOfWork.Movie.Update(movieVM.Movie); } _unitOfWork.Save(); return(RedirectToAction(nameof(Index))); } else { if (movieVM.Movie.Id != 0) { movieVM.Movie = _unitOfWork.Movie.Get(movieVM.Movie.Id); } } return(View(movieVM)); }
public IActionResult Upsert(UpsertVM screeningVM) { if (ModelState.IsValid) { if (screeningVM.Screening.Id == 0) { //insert //check if time is available //that is current time or 4h after current time //so take the date and the auditorium of the screening you try to create /* var date = screeningVM.Screening.Date; * var time = screeningVM.Screening.ScreeningStart; * var auditorium = screeningVM.Screening.Auditorium;*/ //check if there's a screening in the db in that day and in that time interval // var screeningListFromDb = _unitOfWork.Screening.GetScreeningsForInterval(date, time, auditorium); //if it's preview, add 20% to price if (screeningVM.Screening.isPreview == true) { screeningVM.Screening.TicketPrice = (20 * screeningVM.Screening.TicketPrice) + screeningVM.Screening.TicketPrice; } _unitOfWork.Screening.Add(screeningVM.Screening); } else { //edit //check if time is available //that is current time or 4h after current time //so take the date and the auditorium of the screening you try to edit /* var date = screeningVM.Screening.Date; * var time = screeningVM.Screening.ScreeningStart; * var auditorium = screeningVM.Screening.Auditorium;*/ //check if there's a screening in the db in that day and in that time interval // var screeningListFromDb = _unitOfWork.Screening.GetScreeningsForInterval(date, time, auditorium); if (screeningVM.Screening.isPreview == true) { screeningVM.Screening.TicketPrice = (20 / 100) * screeningVM.Screening.TicketPrice + screeningVM.Screening.TicketPrice; } _unitOfWork.Screening.Update(screeningVM.Screening); } _unitOfWork.Save(); return(RedirectToAction(nameof(Index))); } return(View(screeningVM)); }