public async Task <IActionResult> Modify([FromBody] HomeContentCreateEditModel content) { return(await this.Execute(true, true, async() => { await this.homeContent.ModifyArticle(content); return this.Ok("Content has been modified"); })); //if (!this.IsInRole("admin")) //{ // return this.StatusCode(StatusCodes.Status401Unauthorized); //} //if (!ModelState.IsValid) //{ // return this.BadRequest(ModelState); //} //try //{ // await this.homeContent.ModifyArticle(content); // return this.Ok("Content has been modified"); //} //catch (Exception e) //{ // return this.StatusCode(StatusCodes.Status400BadRequest, e.Message); //} }
public async Task ModifyArticle(HomeContentCreateEditModel content) { if (string.IsNullOrWhiteSpace(content.SectionHeading) || string.IsNullOrWhiteSpace(content.SectionContent) || string.IsNullOrWhiteSpace(content.ArticleHeading) || string.IsNullOrWhiteSpace(content.ArticleContent)) { throw new ArgumentException("None of home content data could be an empty string"); } HomeContent persistedContent = await this.db.HomeContent.FirstOrDefaultAsync(); if (persistedContent != null) { persistedContent.SectionHeading = content.SectionHeading; persistedContent.SectionContent = content.SectionContent; persistedContent.ArticleHeading = content.ArticleHeading; persistedContent.ArticleContent = content.ArticleContent; } else { HomeContent homeContent = new HomeContent { ArticleHeading = content.ArticleHeading, ArticleContent = content.ArticleContent, SectionHeading = content.SectionHeading, SectionContent = content.SectionContent }; await this.db.HomeContent.AddAsync(homeContent); } await this.db.SaveChangesAsync(); }