Exemplo n.º 1
0
        public async Task RemoveWaitingManualExerciseCheckings(string courseId, Guid slideId, string userId)
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                var checkings = GetSlideCheckingsByUser <ManualExerciseChecking>(courseId, slideId, userId, noTracking: false)
                                .AsEnumerable()
                                .Where(c => !c.IsChecked && !c.IsLocked)
                                .ToList();
                foreach (var checking in checkings)
                {
                    // Use EntityState.Deleted because EF could don't know abount these checkings (they have been retrieved via AsNoTracking())
                    // TODO (andgein): Now it's not retrieived via AsNoTracking(). Fix this.
                    foreach (var review in checking.Reviews.ToList())
                    {
                        db.Entry(review).State = EntityState.Deleted;
                    }

                    db.Entry(checking).State = EntityState.Deleted;
                }

                await db.SaveChangesAsync();

                transaction.Commit();
            }
        }
Exemplo n.º 2
0
        public async Task <TextBlob> AddText(string text)
        {
            if (text == null)
            {
                return new TextBlob
                       {
                           Hash = null,
                           Text = null
                       }
            }
            ;

            if (text.Length > MaxTextSize)
            {
                text = text.Substring(0, MaxTextSize);
            }

            var hash = GetHash(text);
            var blob = db.Texts.Find(hash);

            if (blob != null)
            {
                return(blob);
            }

            blob = new TextBlob
            {
                Hash = hash,
                Text = text
            };
            db.Texts.AddOrUpdate(blob);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                throw new Exception(
                          string.Join("\r\n",
                                      e.EntityValidationErrors.SelectMany(v => v.ValidationErrors).Select(err => err.PropertyName + " " + err.ErrorMessage)));
            }
            catch (DbUpdateException)
            {
                // It's ok, just tried to insert text with hash which already exists, try to find it
                if (!db.Texts.AsNoTracking().Any(t => t.Hash == hash))
                {
                    throw;
                }
                db.Entry(blob).State = EntityState.Unchanged;
            }
            return(blob);
        }
Exemplo n.º 3
0
        public async Task <TextBlob> AddText(string text)
        {
            if (text == null)
            {
                return new TextBlob
                       {
                           Hash = null,
                           Text = null
                       }
            }
            ;

            if (text.Length > MaxTextSize)
            {
                text = text.Substring(0, MaxTextSize);
            }

            if (text.Contains('\0'))
            {
                text = text.Replace("\0", "");                 // postgres не поддерживает \0 в строках
            }
            var hash = GetHash(text);
            var blob = db.Texts.Find(hash);

            if (blob != null)
            {
                return(blob);
            }

            blob = new TextBlob
            {
                Hash = hash,
                Text = text
            };
            db.Texts.AddOrUpdate(blob);

            try
            {
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (DbUpdateException)
            {
                // It's ok, just tried to insert text with hash which already exists, try to find it
                if (!db.Texts.AsNoTracking().Any(t => t.Hash == hash))
                {
                    throw;
                }
                db.Entry(blob).State = EntityState.Unchanged;
            }

            return(blob);
        }