Beispiel #1
0
        private async Task <bool> DeleteFile(string imageName, string movieId)
        {
            if (string.IsNullOrEmpty(imageName))
            {
                return(false);
            }
            var rootPath      = _webHostEnvironment.WebRootPath;
            var finalfilePath = Path.Combine(rootPath, "images", imageName);

            if (!System.IO.File.Exists(finalfilePath))
            {
                return(false);
            }

            await Task.Run(() =>
            {
                System.IO.File.Delete(finalfilePath);
            });

            var parsedMovieIdResult = Guid.TryParse(movieId, out var parsedMovieId);

            if (!parsedMovieIdResult)
            {
                return(false);;
            }

            var movie = await _moviePortalContext.Movies.FirstOrDefaultAsync(p => p.Id.Equals(parsedMovieId));

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

            movie.Image = null;

            await _moviePortalContext.SaveChangesAsync();

            return(true);
        }
Beispiel #2
0
        public async Task <IActionResult> Create(GenreDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var currenUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (_moviePortalContext.Genres.Any(g => g.Name.ToLower().Equals(model.Name.ToLower())))
            {
                ModelState.AddModelError("", "Жанр с таким названием уже есть в базе");
                return(View(model));
            }

            var genre = _mapper.Map <Genre>(model);

            _moviePortalContext.Genres.Add(genre);
            await _moviePortalContext.SaveChangesAsync();

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #3
0
        public async Task <IActionResult> AddComment(string id, string content)
        {
            var guidUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var movieId    = id;

            if (string.IsNullOrEmpty(content))
            {
                return(BadRequest());
            }

            var movie = await _moviePortalContext.Movies.FirstOrDefaultAsync(m => m.Id.Equals(Guid.Parse(id)));

            if (movie == null)
            {
                return(BadRequest());
            }
            var user = await _userManager.Users.FirstOrDefaultAsync(u => u.Id.Equals(guidUserId));

            if (user == null)
            {
                return(BadRequest());
            }
            var comment = new Comment()
            {
                Id         = Guid.NewGuid(),
                User       = user,
                Movie      = movie,
                Content    = content,
                UploadDate = DateTime.Now,
            };

            _moviePortalContext.Comments.Add(comment);

            await _moviePortalContext.SaveChangesAsync();

            return(Ok("ok"));
        }
Beispiel #4
0
        public async Task Update(MovieDTO model, string fileName)
        {
            //var movie = await _context.Movies.FirstOrDefaultAsync(p => p.Id.Equals(model.Id));

            //if (movie == null)
            //{
            //    throw new Exception($"Movie with id: {model.Id} not found");
            //}

            var movie = _mapper.Map <MoviePortal.Models.Movy.Movie>(model);

            movie.Image = string.IsNullOrEmpty(fileName) ? movie.Image : fileName;

            movie.UpdateDate = DateTime.Now;

            _context.Movies.Update(movie);

            await _context.SaveChangesAsync();
        }