예제 #1
0
        // [Authorize]
        public async Task <ActionResult <RetrieveMovieViewModel> > GetMovie(string movieName)
        {
            // -------- take access token from current user context
            var userId = await TakeUserIdByAccessToken();

            if (userId.Equals("Unauthorized"))
            {
                return(Unauthorized());
            }


            Movie movie = await _applicationDb.Movies.SingleOrDefaultAsync(m => m.Name == movieName);

            if (movie == null)
            {
                return(NotFound());
            }

            // ------- assign movie properties value to RetrieveMovieViewModel properties value
            var retrieveMovie = new RetrieveMovieViewModel(movie);


            var movieUserRanked = await _applicationDb.MovieUserRankeds
                                  .SingleOrDefaultAsync(m => m.UserId == new Guid(userId) && m.MovieId == movie.Id);


            if (movieUserRanked != null)
            {
                retrieveMovie.UserRank = movieUserRanked.Rank;
            }

            return(retrieveMovie);
        }
예제 #2
0
        public async Task <IActionResult> AddMovie([FromForm] AddMovieViewModel movieModel)
        {
            // ------- save ImageFile to directory
            var    rootPath      = _hostEnvironment.WebRootPath;
            var    fileName      = Path.GetFileNameWithoutExtension(movieModel.ImageFile.FileName);
            var    extension     = Path.GetExtension(movieModel.ImageFile.FileName);
            var    userPhotoName = fileName + DateTime.Now.ToString("yymmddssfff") + extension;
            string path          = Path.Combine(rootPath, "image", userPhotoName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                await movieModel.ImageFile.CopyToAsync(fileStream);
            }


            // ------- create new movie with current movie data that coming from request body
            Movie movie = new Movie
            {
                Name        = movieModel.Name,
                Description = movieModel.Description,
                ReleaseDate = Int32.Parse(movieModel.ReleaseDate),
                Director    = movieModel.Director,
                UserId      = new Guid(movieModel.UserId),
                ImagePath   = path,
                RankCount   = 0,
            };


            await _applicationDb.Movies.AddAsync(movie);

            // await _applicationDb.Movies.AddAsync(movie);
            await _applicationDb.SaveChangesAsync();

            var retrieveMovie = new RetrieveMovieViewModel(movie);


            return(CreatedAtAction(nameof(GetMovie), new { movieName = retrieveMovie.Name },
                                   retrieveMovie));
        }
        public async Task <ActionResult <RetrieveMovieViewModel> > Index(string movieName)
        {
            var access_token = await HttpContext.GetTokenAsync("access_token");

            ViewData["access_token"] = access_token;                     // for delete button
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId


            // ------- call the movie Api to get 1 movie
            var allMoviesRequest = _httpClientFactory.CreateClient();

            allMoviesRequest.SetBearerToken(access_token);
            var allMoviesResponse =
                await allMoviesRequest.GetAsync($"https://*****:*****@"\", @"/");
                retrieveMovie.ImagePath = imagePathTemp;
            }

            return(View("movie", retrieveMovie));
        }