Beispiel #1
0
        public override async Task <IHttpActionResult> Put([FromODataUri] Guid key, BlogPost entity)
        {
            var currentEntry = await Service.FindOneAsync(entity.Id);

            entity.TenantId        = currentEntry.TenantId;
            entity.UserId          = currentEntry.UserId;
            entity.DateCreatedUtc  = currentEntry.DateCreatedUtc;
            entity.FullDescription = MediaHelper.EnsureCorrectUrls(entity.FullDescription);
            var result = await base.Put(key, entity);

            if (!entity.Tags.IsNullOrEmpty())
            {
                var chosenTagIds = entity.Tags.Select(x => x.TagId);
                var existingTags = await postTagService.Value.FindAsync(x => x.PostId == entity.Id);

                var existingTagIds = existingTags.Select(x => x.TagId);

                var toDelete = existingTags.Where(x => !chosenTagIds.Contains(x.TagId));
                var toInsert = chosenTagIds.Where(x => !existingTagIds.Contains(x)).Select(x => new BlogPostTag
                {
                    PostId = entity.Id,
                    TagId  = x
                });

                await postTagService.Value.DeleteAsync(toDelete);

                await postTagService.Value.InsertAsync(toInsert);
            }

            return(result);
        }
 public override async Task <IHttpActionResult> Post(PageVersion entity)
 {
     entity.DateCreatedUtc  = DateTime.UtcNow;
     entity.DateModifiedUtc = DateTime.UtcNow;
     entity.Fields          = MediaHelper.EnsureCorrectUrls(entity.Fields);
     return(await base.Post(entity));
 }
Beispiel #3
0
        public override async Task <IHttpActionResult> Post(BlogPost entity)
        {
            int tenantId = GetTenantId();

            entity.TenantId = tenantId;

            if (!CanModifyEntity(entity))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            entity.DateCreatedUtc  = DateTime.UtcNow;
            entity.UserId          = workContext.Value.CurrentUser.Id;
            entity.FullDescription = MediaHelper.EnsureCorrectUrls(entity.FullDescription);

            var tags = entity.Tags;

            entity.Tags = null;

            SetNewId(entity);

            OnBeforeSave(entity);
            await Service.InsertAsync(entity);

            var result = Created(entity);

            if (!tags.IsNullOrEmpty())
            {
                var toInsert = tags.Select(x => new BlogPostTag
                {
                    PostId = entity.Id,
                    TagId  = x.TagId
                });
                postTagService.Value.Insert(toInsert);
            }

            OnAfterSave(entity);

            return(result);
        }
        public override async Task <IHttpActionResult> Put([FromODataUri] Guid key, PageVersion entity)
        {
            if (!CanModifyEntity(entity))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!key.Equals(GetId(entity)))
            {
                return(BadRequest());
            }

            try
            {
                // Getting by ID only is not good enough in this instance, because GetCurrentVersion()
                //  will return the invariant record if no localized record exists. That means when an entity is updated to here,
                //  we are trying to update a localized one, but using the ID of the invariant one! So, we need to get by culture code AND ID
                //  and then if not exists, create it.
                var currentVersion = await Service.FindOneAsync(entity.Id);

                if (currentVersion.CultureCode != entity.CultureCode)
                {
                    // We need to duplicate it to a new record!
                    var newRecord = new PageVersion
                    {
                        Id              = Guid.NewGuid(),
                        TenantId        = currentVersion.TenantId,
                        PageId          = entity.PageId,
                        CultureCode     = entity.CultureCode,
                        Status          = currentVersion.Status,
                        Title           = entity.Title,
                        Slug            = entity.Slug,
                        Fields          = MediaHelper.EnsureCorrectUrls(entity.Fields),
                        ShowOnMenus     = entity.ShowOnMenus,
                        DateCreatedUtc  = DateTime.UtcNow,
                        DateModifiedUtc = DateTime.UtcNow,
                    };
                    await Service.InsertAsync(newRecord);
                }
                else
                {
                    if (currentVersion.Status == VersionStatus.Published)
                    {
                        // archive current version before updating
                        var backup = new PageVersion
                        {
                            Id              = Guid.NewGuid(),
                            TenantId        = currentVersion.TenantId,
                            PageId          = currentVersion.PageId,
                            CultureCode     = currentVersion.CultureCode,
                            DateCreatedUtc  = currentVersion.DateCreatedUtc,
                            DateModifiedUtc = currentVersion.DateModifiedUtc,
                            Status          = VersionStatus.Archived,
                            Title           = currentVersion.Title,
                            Slug            = currentVersion.Slug,
                            Fields          = currentVersion.Fields,
                            ShowOnMenus     = currentVersion.ShowOnMenus
                        };
                        await Service.InsertAsync(backup);

                        RemoveOldVersions(currentVersion.PageId, currentVersion.CultureCode);
                    }

                    entity.TenantId        = currentVersion.TenantId;
                    entity.DateCreatedUtc  = currentVersion.DateCreatedUtc;
                    entity.DateModifiedUtc = DateTime.UtcNow;
                    entity.Fields          = MediaHelper.EnsureCorrectUrls(entity.Fields);
                    await Service.UpdateAsync(entity);
                }
            }
            catch (DbUpdateConcurrencyException x)
            {
                Logger.Error(x.Message, x);

                if (!EntityExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(entity));
        }