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(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 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 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; } }