Exemple #1
0
        public async Task <IActionResult> PublishChangesAsync(Post newPost, [FromServices] IMongoCollection <Post> mongoCollection, [FromServices] IMongoCollection <OldVersionOfPost> oldCollection, [FromServices] IMongoCollection <Notification> notificationsCollection)
        {
            Post currentValue = await(await mongoCollection.FindAsync(x => x.ID == newPost.ID)).FirstOrDefaultAsync();

            if (currentValue == null)
            {
                return(this.Error(HttpStatusCode.NotFound, "There's no such Post!"));
            }
            OldVersionOfPost oldVersion = new OldVersionOfPost(currentValue)
            {
                ID = Guid.NewGuid()
            };

            if (!currentValue.IsPublished)
            {
                await oldCollection.InsertOneAsync(oldVersion);
            }
            if (newPost.ID == Guid.Empty)
            {
                newPost.ID = Guid.NewGuid();
            }
            newPost.PublishTime = DateTime.Now;
            newPost.IsPublished = true;
            UpdateResult result = await mongoCollection.UpdateOneAsync(x => x.ID == newPost.ID, Extensions.GenerateUpdateDefinition(currentValue, newPost));

            if (result.IsAcknowledged)
            {
                return(this.Success(newPost.ID));
            }
            else
            {
                return(this.Error(HttpStatusCode.InternalServerError, "Publishing changes failed!"));
            }
        }
Exemple #2
0
        public async Task <IActionResult> DeletePostAsync(Post post, [FromServices] IMongoCollection <Post> mongoCollection, [FromServices] IMongoCollection <OldVersionOfPost> oldCollection)
        {
            Post currentValue = await(await mongoCollection.FindAsync(x => x.ID == post.ID)).FirstOrDefaultAsync();

            if (currentValue == null)
            {
                return(this.Error(HttpStatusCode.NotFound, $"There's no post with Id={post.ID}"));
            }
            if (currentValue.IsPublished)
            {
                OldVersionOfPost temp = new OldVersionOfPost(currentValue)
                {
                    ID = Guid.NewGuid()
                };
                await oldCollection.InsertOneAsync(temp);
            }
            DeleteResult result = await mongoCollection.DeleteOneAsync(x => x.ID == post.ID);

            if (result.IsAcknowledged)
            {
                return(this.Ok());
            }
            return(this.Error(HttpStatusCode.InternalServerError, "Deleting of a post failed!"));
        }