예제 #1
0
        public Issue GetComic(int id)
        {
            Issue result = null;

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;

                    var issue = context.Comics.Where(x => x.Id_Comic == id).ToList().FirstOrDefault();

                    var mapper = new MyNextComicMapper();

                    result = mapper.MapIssue(issue);
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #2
0
        public double GetUserRating(string userName, int comicId)
        {
            double result = 0;

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    var userId = context.Users.Where(x => x.Username == userName).FirstOrDefault().Id;
                    var values = context.Preferences.Where(x => x.ItemID == comicId && x.UserID == userId).Select(x => x.Value).ToList();
                    if (values.Count > 0)
                    {
                        result = values.Average();
                    }
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #3
0
        public bool InsertComics()
        {
            var result = false;

            try
            {
                var totalCalls     = 670;
                var startingOffset = 116400;
                var numberOfCalls  = 10;
                var limit          = 100;
                for (int i = 0; i < totalCalls; i++)
                {
                    var issues = GetAllComics(numberOfCalls, startingOffset, limit);

                    using (TransactionScope scope = new TransactionScope())
                    {
                        MyNextComicEntities context = null;
                        try
                        {
                            context = new MyNextComicEntities();
                            context.Configuration.AutoDetectChangesEnabled = false;

                            var mapper = new MyNextComicMapper();

                            int count = 0;
                            foreach (var issue in issues)
                            {
                                ++count;
                                context = AddToContext(context, mapper.MapComic(issue), count, 100, true);
                            }

                            context.SaveChanges();
                        }
                        catch (DbEntityValidationException e)
                        {
                        }
                        finally
                        {
                            if (context != null)
                            {
                                context.Dispose();
                            }
                        }

                        scope.Complete();
                    }

                    result = true;

                    startingOffset = startingOffset + (numberOfCalls * limit);
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return(result);
        }
예제 #4
0
        public User GetUserData(string userName)
        {
            var result = new User();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;
                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;

                    var storedUser = context.Users.Where(x => x.Username == userName).FirstOrDefault();
                    if (storedUser != null)
                    {
                        result.UserId   = storedUser.Id;
                        result.UserName = storedUser.Username;
                        result.Email    = storedUser.Email;
                        result.IsAdmin  = storedUser.IsAdmin ?? false;

                        var preferences = context.Preferences.Where(x => x.UserID == storedUser.Id).ToList();
                        var comicsIds   = preferences.Select(x => x.ItemID).ToList();
                        if (preferences.Count() > 0)
                        {
                            var ratedComics = context.Comics.Where(x => comicsIds.Any(y => x.Id_Comic == y)).ToList();
                            var mapper      = new MyNextComicMapper();
                            var issues      = new List <Issue>();
                            foreach (var comic in ratedComics)
                            {
                                var issue = mapper.MapIssue(comic);
                                issue.Rating = preferences.Where(x => x.ItemID == comic.Id_Comic).FirstOrDefault().Value;
                                issues.Add(issue);
                            }

                            result.ComicList = issues.AsEnumerable();
                        }
                    }
                    else
                    {
                        context.Dispose();
                    }
                }
                catch (DbEntityValidationException e)
                {
                }

                scope.Complete();
            }

            return(result);
        }
예제 #5
0
        public List <Issue> GetComics(string searchString, int genre, List <int> ids)
        {
            var result = new List <Issue>();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    var comics = new List <Comics>();
                    if (!string.IsNullOrEmpty(searchString))
                    {
                        comics = context.Comics.Where(x => x.Name.ToLower().Contains(searchString.ToLower())).OrderBy(x => x.Id).ToList();
                    }
                    else if (ids.Count() > 0)
                    {
                        comics = context.Comics.Where(x => ids.Contains(x.Id_Comic)).ToList();
                    }
                    else
                    {
                        comics = context.Comics.OrderBy(x => x.Id).ToList();
                    }

                    if (genre != 0)
                    {
                        comics = comics.Where(x => x.Genre == genre).ToList();
                    }

                    var mapper = new MyNextComicMapper();

                    foreach (var comic in comics)
                    {
                        result.Add(mapper.MapIssue(comic));
                    }
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #6
0
        public UserServiceResponse InsertUser(User user)
        {
            var result = new UserServiceResponse();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;
                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;

                    if (!(context.Users.Any(x => x.Username == user.UserName)))
                    {
                        var Id = context.Users.OrderByDescending(x => x.Id).Select(x => x.Id).First();
                        context.Users.Add(new Users()
                        {
                            Id       = Id + 1,
                            Username = user.UserName,
                            Password = HashPassword(user.Password),
                            Email    = user.Email
                        });
                        context.SaveChanges();
                        context.Dispose();

                        result.Success      = true;
                        result.ErrorMessage = "";
                    }
                    else
                    {
                        context.Dispose();
                        result.Success      = false;
                        result.ErrorMessage = "El nombre de usuario ingresado no se encuentra disponible";
                    }
                }
                catch (DbEntityValidationException e)
                {
                    result.Success      = false;
                    result.ErrorMessage = e.Message;
                }

                scope.Complete();
            }

            return(result);
        }
예제 #7
0
        public bool SaveUserRating(string userName, int comicId, double value)
        {
            bool result = false;

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    var userId     = context.Users.Where(x => x.Username == userName).FirstOrDefault().Id;
                    var preference = context.Preferences.Where(x => x.UserID == userId && x.ItemID == comicId).FirstOrDefault();

                    if (preference != null)
                    {
                        preference.Value = value;
                        context.Preferences.Attach(preference);
                        var entry = context.Entry(preference);
                        entry.Property(e => e.Value).IsModified = true;
                        context.SaveChanges();
                    }
                    else
                    {
                        context.Preferences.Add(new Preferences()
                        {
                            ItemID = comicId,
                            UserID = userId,
                            Value  = value
                        });
                        context.SaveChanges();
                    }
                    result = true;
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #8
0
        public UserServiceResponse LogInUser(User user)
        {
            var result = new UserServiceResponse();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;
                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;

                    var storedUser = context.Users.Where(x => x.Username == user.UserName).FirstOrDefault();
                    if (storedUser != null)
                    {
                        if (VerifyPassword(storedUser.Password, user.Password))
                        {
                            result.Success      = true;
                            result.ErrorMessage = "";
                        }
                        else
                        {
                            result.Success      = false;
                            result.ErrorMessage = "Contraseña inválida";
                        }
                    }
                    else
                    {
                        context.Dispose();
                        result.Success      = false;
                        result.ErrorMessage = "No se encontró ningun usuario con ese nombre";
                    }
                }
                catch (DbEntityValidationException e)
                {
                    result.Success      = false;
                    result.ErrorMessage = e.Message;
                }

                scope.Complete();
            }

            return(result);
        }
예제 #9
0
        public List <Issue> GetTopComics()
        {
            var result = new List <Issue>();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;

                    var comics = new List <Issue>();


                    var preSelection = context.Preferences.OrderBy(x => Guid.NewGuid()).Take(8).ToList();

                    var Ids        = preSelection.Select(x => x.ItemID).ToList();
                    var comicsData = context.Comics.Where(x => Ids.Any(y => x.Id_Comic == y)).ToList();

                    var mapper = new MyNextComicMapper();

                    foreach (var comic in preSelection)
                    {
                        var mappedComic = mapper.MapIssue(comicsData.Where(x => x.Id_Comic == comic.ItemID).FirstOrDefault());
                        mappedComic.Rating = Math.Round(comic.Value, 0, MidpointRounding.AwayFromZero);
                        comics.Add(mappedComic);
                    }

                    result = comics;
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #10
0
        public List <Genre> GetGenres(int?genreId = null)
        {
            List <Genre> result = new List <Genre>();

            using (TransactionScope scope = new TransactionScope())
            {
                MyNextComicEntities context = null;

                try
                {
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    var mapper = new MyNextComicMapper();

                    if (genreId == null)
                    {
                        var generos = context.Genres.ToList();
                        foreach (var genre in generos)
                        {
                            result.Add(mapper.MapGenre(genre));
                        }
                    }
                    else
                    {
                        var genre = context.Genres.Where(x => x.IdGenre == genreId).ToList().FirstOrDefault();
                        result.Add(mapper.MapGenre(genre));
                    }
                }
                catch (DbEntityValidationException e)
                {
                }

                context.Dispose();
                scope.Complete();
            }

            return(result);
        }
예제 #11
0
        private MyNextComicEntities AddToContext(MyNextComicEntities context, Comics issue, int count, int commitCount, bool recreateContext)
        {
            if (!(context.Comics.Any(x => x.Id_Comic == issue.Id_Comic)))
            {
                context.Comics.Add(issue);
            }

            //context.SaveChanges();

            //context.Set<Comics>().Add(issue);

            if (count % commitCount == 0)
            {
                context.SaveChanges();
                if (recreateContext)
                {
                    context.Dispose();
                    context = new MyNextComicEntities();
                    context.Configuration.AutoDetectChangesEnabled = false;
                }
            }

            return(context);
        }