Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }