private bool IsModelValid(FeedBacksRecipeViewModel model) { var recipe = this._context.Recipes.FirstOrDefault(x => x.Id == model.rid); var user = this._context.Users.FirstOrDefault(x => x.Id == model.uid); if (recipe != null && user != null && !string.IsNullOrEmpty(model.Text)) { return(true); } else { return(false); } }
public IActionResult PostComment([FromBody] FeedBacksRecipeViewModel model) { if (IsModelValid(model)) { this._context.Comments.Add(new Comment() { recipe = _context.Recipes.FirstOrDefault(x => x.Id == model.rid), Text = model.Text, user = _context.Users.FirstOrDefault(x => x.Id == model.uid), Time = DateTime.Now, }); this._context.SaveChanges(); return(Ok("OK")); } else { return(BadRequest("Invalid request!")); } }