public class EditItem_OutputModel
{
public string guidarticle { get; set; } = string.Empty;
public string title { get; set; } = string.Empty;
public string html { get; set; } = string.Empty;
public string folder { get; set; } = string.Empty;
public string keywords { get; set; } = string.Empty;
public DateTime dateedit { get; set; } = DateTime.Now;
public bool isowner { get; set; } = false;
}
//=Update
//*response
with editmodel, to confirm new guid on new
[HttpPost("save_article")]
public async
Task<ActionResult<EditItem_OutputModel>>
Save_Article(EditItem_OutputModel article)
{
//*check
if (article == null) return BadRequest("Article to save is empty");
//*get matching Article from Database by
ID
ArticleDbModel? dbArticle;
try
{
dbArticle = await
_dbContext.tbl_Articles.FindAsync(article.guidarticle);
}
catch (Exception e)
{
return BadRequest("Database error:" + e.Message);
//throw;
}
//*check
if (dbArticle == null) return BadRequest("Article not found");
//< init.correction >
string title=article.title;
string keywords=article.keywords;
string folder=article.folder;
string html=article.html;
//</ init.correction >
dbArticle.Title = title;
dbArticle.Folder=folder;
dbArticle.Keywords = keywords;
dbArticle.Html=html;
dbArticle.DateEdit =
DateTime.Now;
await _dbContext.SaveChangesAsync();
//*check
//< Prepare Output >
EditItem_OutputModel outEditItem
= new EditItem_OutputModel();
outEditItem.guidarticle =
dbArticle.GuidArticle;
outEditItem.title =
dbArticle.Title ?? "";
outEditItem.html = dbArticle.Html
?? "";
outEditItem.folder =
dbArticle.Folder ?? "";
outEditItem.keywords =
dbArticle.Keywords ?? "";
outEditItem.dateedit =
dbArticle.DateEdit;
outEditItem.isowner = false; //init
//</ Prepare Output >
//< check_owner >
//get client User to check if is owner
var userGUID =
HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userGUID == dbArticle.GuidUser)
{
outEditItem.isowner = true;
}
//</ check_owner >
return Ok(outEditItem); //with status
}
|