public async Task <IActionResult> Add([FromBody] dynamic _in) { try { if (!ModelState.IsValid) { throw (new Exception("Bad Request,ModelState:" + ModelState.ToString())); } string news_id = _in.news_id; var newss = from row in _context.News where row.news_id == news_id select row; var news = newss.FirstOrDefault(); if (news != null) { throw (new Exception("该对象ID已存在")); } var nws = new news() { author_name = _in.author_name, author_id = _in.author_id, content = _in.content, news_id = _in.news_id, part = _in.part, post_date = _in.post_date, title = _in.title, reader_num = 0, craze = 0 }; _context.News.Add(nws); if (!(await _context.SaveChangesAsync() > 0)) { throw (new Exception("插入到数据库失败")); } RestfulResult.RestfulData rr = new RestfulResult.RestfulData(1, "成功发布新闻"); return(new JsonResult(rr)); } catch (Exception exc) { RestfulResult.RestfulData rr = new RestfulResult.RestfulData(0, exc.Message); return(new JsonResult(rr)); } }
public async Task <IActionResult> Add([FromBody] dynamic _in) { try { if (!ModelState.IsValid) { throw (new Exception("Bad Request,ModelState:" + ModelState.ToString())); } string ID = _in.ID; var pedias = from row in _context.Encyclopedia where row.ID == ID select row; var pedia = pedias.FirstOrDefault(); if (pedia != null) { throw (new Exception("该对象ID已存在")); } var newpedia = new encyclopedia() { author_name = _in.author_name, author_id = _in.author_id, content = _in.content, ID = ID, part = _in.part, post_date = _in.post_date, title = _in.title, reader_num = 0 }; _context.Encyclopedia.Add(newpedia); if (!(await _context.SaveChangesAsync() > 0)) { throw (new Exception("插入到数据库失败")); } RestfulResult.RestfulData rr = new RestfulResult.RestfulData(1, "成功发布百科"); return(new JsonResult(rr)); } catch (Exception exc) { RestfulResult.RestfulData rr = new RestfulResult.RestfulData(0, exc.ToString()); return(new JsonResult(rr)); } }
public async Task <IActionResult> Add([FromBody] dynamic _in) { try { if (!ModelState.IsValid) { throw (new Exception("Bad Request,ModelState:" + ModelState.ToString())); } string post_id = _in.post_id; var posts = from row in _context.Post_data where row.post_id == post_id select row; var post = posts.FirstOrDefault(); if (post != null) { throw (new Exception("该对象ID已存在")); } var newpost = new post_data() { content = _in.content, title = _in.title, post_date = _in.post_date, post_id = _in.post_id, user_id = _in.user_id }; _context.Post_data.Add(newpost); if (!(await _context.SaveChangesAsync() > 0)) { throw (new Exception("插入到数据库失败")); } RestfulResult.RestfulData rr = new RestfulResult.RestfulData(1, "成功发布主题贴"); return(new JsonResult(rr)); } catch (Exception exc) { RestfulResult.RestfulData rr = new RestfulResult.RestfulData(0, exc.Message); return(new JsonResult(rr)); } }