public async Task Save(UserViewModel entity)
        {
            try
            {
                using VideoBlockDbContext _context = new VideoBlockDbContext();
                if (_context.User.Any(x => x.UserName == entity.UserName.Trim()))
                {
                    throw new Exception(message: "Existe un elemento con este usuario en la base de datos");
                }
                var user = new User
                {
                    Name     = entity.Name.Trim(),
                    LastName = entity.LastName.Trim(),
                    Password = PasswordEncryptHelper.Encrypt(entity.Password.Trim()),
                    UserName = entity.UserName.Trim(),
                    IdRole   = (int)RolesEnum.Cliente
                };

                _context.User.Add(user);
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task <string> Login(LoginViewModel model)
        {
            try
            {
                using (VideoBlockDbContext _context = new VideoBlockDbContext())
                {
                    string password = PasswordEncryptHelper.Encrypt(model.Password);

                    var user = await _context.User.Where(x => x.UserName == model.UserName && x.Password == password).FirstOrDefaultAsync();

                    if (user == null)
                    {
                        throw new Exception(message: "Datos incorrectos");
                    }
                    user.Token = Guid.NewGuid().ToString();

                    await _context.SaveChangesAsync();

                    return(await Task.FromResult(user.Token));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task Save(MovieViewModel entity)
        {
            try
            {
                using (VideoBlockDbContext _context = new VideoBlockDbContext())
                {
                    var movie = new Movie
                    {
                        Description = entity.Descripcion,
                        IdDirector  = entity.IdDirector,
                        Price       = entity.CostoAlquiler,
                        Stock       = entity.CantidadInventario,
                        Title       = entity.Titulo
                    };

                    foreach (int idActor in entity.ActoresId)
                    {
                        movie.Actors.Add(new PersonMovie
                        {
                            IdPerson = idActor
                        });
                    }
                    _context.Movie.Add(movie);

                    await _context.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public async Task <IList <PersonaViewModel> > GetAll()
 {
     try
     {
         using (VideoBlockDbContext _context = new VideoBlockDbContext())
         {
             return(await _context.Person.Select(x => new PersonaViewModel
             {
                 Id = x.Id,
                 Nombre = $"{x.Name} {x.LastName}"
             }).ToListAsync());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        public async Task Booking(int id, IList <int> bookings)
        {
            try
            {
                using (VideoBlockDbContext _context = new VideoBlockDbContext())
                {
                    var user = await _context.User.FindAsync(id);

                    if (user == null)
                    {
                        throw new Exception(message: "El usuario especificado no está registrado en la base de datos");
                    }

                    foreach (int movie in bookings)
                    {
                        var pelicula = await _context.Movie.FindAsync(movie);

                        if (pelicula.Stock != 0)
                        {
                            pelicula.Stock -= 1;
                            if (!user.Bookings.Any(x => x.IdMovie == movie))
                            {
                                user.Bookings.Add(new Book
                                {
                                    IdMovie = movie
                                });
                            }
                        }
                        else
                        {
                            throw new Exception(message: "La película no tiene reservas disponibles");
                        }
                    }
                    await _context.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public async Task <IList <UserViewModel> > GetAll()
 {
     try
     {
         using (VideoBlockDbContext _context = new VideoBlockDbContext())
         {
             return(await _context.User.Select(e => new UserViewModel
             {
                 Id = e.Id,
                 Name = e.Name,
                 LastName = e.LastName,
                 UserName = e.Password,
                 Bookings = string.Join(", ", e.Bookings.Select(x => x.Movie.Title).ToArray())
             }).ToListAsync());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 public async Task <UserViewModel> GetUserByUserName(string username)
 {
     try
     {
         using (VideoBlockDbContext _context = new VideoBlockDbContext())
         {
             return(await _context.User.Select(x => new UserViewModel
             {
                 Id = x.Id,
                 Name = x.Name,
                 LastName = x.LastName,
                 UserName = x.UserName,
                 Bookings = string.Join(", ", x.Bookings.Select(e => e.Movie.Title).ToArray()),
                 IdRol = x.IdRole
             }).FirstOrDefaultAsync());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 public async Task <IList <MovieViewModel> > GetAll()
 {
     try
     {
         using (VideoBlockDbContext _context = new VideoBlockDbContext())
         {
             return(await _context.Movie.Select(e => new MovieViewModel
             {
                 Id = e.Id,
                 CantidadInventario = e.Stock,
                 CostoAlquiler = e.Price,
                 Descripcion = e.Description,
                 Director = $"{e.Director.Name} {e.Director.LastName}",
                 Actores = string.Join(", ", e.Actors.Select(x => $"{x.Person.Name} {x.Person.LastName}").ToArray()),
                 Titulo = e.Title
             }).ToListAsync());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }