public async Task <T> Create <T>(T content) where T : IContent
        {
            content.Id = 0;

            var c = _mapper.Map <ContentVersion>(content);

            c.UpdatedAt = DateTime.UtcNow;

            _context.Add((object)c);

            await _context.SaveChangesAsync();

            content.Id = c.Id;

            var olderVersions = await _context.ContentVersions.Where(w => w.ContentId == c.ContentId).ToListAsync();

            if (olderVersions.Count > 10)
            {
                var sorted = olderVersions.OrderByDescending(w => w.Id);
                var tooOld = sorted.Skip(10).ToList();
                if (tooOld.Any())
                {
                    foreach (var contentVersion in tooOld)
                    {
                        _context.ContentVersions.Remove(contentVersion);
                    }

                    await _context.SaveChangesAsync();
                }
            }

            return(content);
        }
Esempio n. 2
0
        public async Task <T> Create <T>(T content) where T : IContent
        {
            content.CreatedAt = DateTime.UtcNow;

            var c = _mapper.Map <Content>(content);

            if (c.ParentId.HasValue)
            {
                var maxOrder = _context.Content.Max(w => w.Order);
                c.Order = maxOrder + 1;
            }

            _context.Add(c);
            await _context.SaveChangesAsync();

            content.Id = c.Id;

            _routeDataCache.Clear();

            return(content);
        }