Example #1
0
        public async Task Create(
            string projectId,
            IPage page,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            if (page == null)
            {
                throw new ArgumentException("page must not be null");
            }

            var p = PageEntity.FromIPage(page);

            if (string.IsNullOrEmpty(p.Id))
            {
                p.Id = Guid.NewGuid().ToString();
            }

            if (string.IsNullOrEmpty(p.ProjectId))
            {
                p.ProjectId = projectId;
            }
            p.LastModified = DateTime.UtcNow;

            using (var dbContext = _contextFactory.CreateContext())
            {
                dbContext.Pages.Add(p);

                int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                                   .ConfigureAwait(false);
            }
        }
Example #2
0
        public async Task Update(
            string projectId,
            IPage page,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            if (page == null)
            {
                throw new ArgumentException("page must not be null");
            }
            if (string.IsNullOrEmpty(page.Id))
            {
                throw new ArgumentException("can only update an existing page with a populated Id");
            }

            //if (string.IsNullOrEmpty(projectId)) throw new ArgumentException("projectId must be provided");
            var p = PageEntity.FromIPage(page);

            p.LastModified = DateTime.UtcNow;
            bool tracking = dbContext.ChangeTracker.Entries <PageEntity>().Any(x => x.Entity.Id == p.Id);

            if (!tracking)
            {
                dbContext.Pages.Update(p);
            }

            int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                               .ConfigureAwait(false);
        }
Example #3
0
        public async Task Update(
            string projectId,
            IPage page,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            if (page == null)
            {
                throw new ArgumentException("page must not be null");
            }
            if (string.IsNullOrEmpty(page.Id))
            {
                throw new ArgumentException("can only update an existing page with a populated Id");
            }
            var p = PageEntity.FromIPage(page);

            p.LastModified = DateTime.UtcNow;

            using (var dbContext = _contextFactory.CreateContext())
            {
                bool tracking = dbContext.ChangeTracker.Entries <PageEntity>().Any(x => x.Entity.Id == p.Id);
                if (!tracking)
                {
                    dbContext.PageResources.RemoveRange(dbContext.PageResources.Where(x => x.PageEntityId == p.Id));
                    dbContext.Pages.Update(p);
                }

                int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                if (page.Resources.Count > 0)
                {
                    p.Resources = page.Resources;
                    foreach (var r in p.PageResources)
                    {
                        r.PageEntityId = p.Id;
                    }

                    rowsAffected = await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
            }
        }