Example #1
0
        public async Task <IActionResult> CreateOrEdit([FromForm] MovieViewModel movieVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (movieVm.Id != 0)
            {
                var movieRes = await _movieService.GetMovie(movieVm.Id);

                if (!movieRes.IsSuccess)
                {
                    return(RedirectToAction("Details", new { id = movieVm.Id, showSuccessMessage = false }));
                }
                var movie = movieRes.Data;
                if (!await _userManager.CheckEditPermissionAsync(User, movie))
                {
                    return(Forbid());
                }
            }

            var currentUser = await _userManager.GetUserAsync(User);

            var result = await _movieService.AddOrEditMovie(movieVm.ToModel(), movieVm.NewPoster, currentUser);

            if (result.IsSuccess)
            {
                return(RedirectToAction("Details", new { id = result.Data.Id, showSuccessMessage = result.IsSuccess }));
            }

            movieVm.Message          = OperationResultHelper.GetMessage(result.IsSuccess);
            movieVm.IsSuccessMessage = false;
            return(View(movieVm));
        }