Esempio n. 1
0
        public static Func <Request, Response, Task> Remove(SQLiteAsyncConnection db)
        {
            return(async(req, res) =>
            {
                var article = await req.ParseBodyAsync <Article>();

                var articleHtml = new ArticleHtml {
                    Id = article.Id
                };
                var articleMarkdown = new ArticleMarkdown {
                    Id = article.Id
                };

                var deleted = await db.DeleteAsync(article);

                await Task.WhenAll(db.DeleteAsync(articleHtml), db.DeleteAsync(articleMarkdown));

                await res.SendStatus(deleted == 1?HttpStatusCode.OK : HttpStatusCode.NotFound);
            });
        }
Esempio n. 2
0
        public static Func <Request, Response, Task> Upsert(SQLiteAsyncConnection db, Settings settings, Configuration config)
        {
            return(async(req, res) =>
            {
                var sessionData = req.GetSession <Session>();
                var updatedArticle = await req.ParseBodyAsync <Article>();

                if (await db.FindAsync <Article>(arti =>
                                                 arti.Slug == updatedArticle.Slug && arti.Id != updatedArticle.Id) != null)
                {
                    const string msg = "Another article with the same slug already exists";
                    await res.SendString(msg, status : HttpStatusCode.BadRequest);

                    return;
                }

                Article article;
                ArticleHtml articleHtml;
                ArticleMarkdown articleMarkdown;
                var newArticle = updatedArticle.Id == Guid.Empty;
                if (!newArticle)
                {
                    article = await db.FindAsync <Article>(arti => arti.Id == updatedArticle.Id);

                    if (article == null)
                    {
                        const string msg = "Could not find an existing article with the specified id";
                        await res.SendString(msg, status : HttpStatusCode.BadRequest);

                        return;
                    }

                    articleHtml = await db.FindAsync <ArticleHtml>(arti => arti.Id == updatedArticle.Id);

                    articleMarkdown = await db.FindAsync <ArticleMarkdown>(arti => arti.Id == updatedArticle.Id);
                }
                else
                {
                    article = new Article {
                        Id = Guid.NewGuid(), Author = sessionData.Name
                    };
                    articleHtml = new ArticleHtml {
                        Id = article.Id
                    };
                    articleMarkdown = new ArticleMarkdown {
                        Id = article.Id
                    };
                }

                TextFormatting.CopyMeta(article, updatedArticle);

                articleMarkdown.Content = updatedArticle.Markdown;
                articleHtml.Content = CommonMarkConverter.Convert(updatedArticle.Markdown);

                var firstParagraph = TextFormatting.FirstParagraph(articleHtml.Content);
                article.Html = firstParagraph;
                article.Markdown = "";


                if (newArticle)
                {
                    await db.InsertAllAsync(new object[] { article, articleHtml, articleMarkdown });
                }
                else
                {
                    await db.UpdateAllAsync(new object[] { article, articleHtml, articleMarkdown });
                }

                FileHandling.UpdateIndexingFiles(db, settings, config);
                await res.SendString(article.Id.ToString());
            });
        }