public ActionResult <view_node> PostNode([FromRoute] Int32 boardId, [FromBody] dynamic node) { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int16 userId = _userService.GetUserId(token); board board = _context.board.FirstOrDefault(x => x.id == boardId && x.owner_id == userId && x.deleted_at == null); if (board == null) { throw new MindnoteException("嗚喔! 分類已經被刪除,無法瀏覽", HttpStatusCode.NotFound); } node newNode = new node { title = node.title, description = node.description, owner_id = userId, board_id = board.id }; _context.node.Add(newNode); _context.SaveChanges(); if (node.parent_node_id != null) { node_relationship nodeRelationship = new node_relationship { parent_node_id = node.parent_node_id, child_node_id = newNode.id }; _context.node_relationship.Add(nodeRelationship); _context.SaveChanges(); } return(_contextForView.view_node.SingleOrDefault(rec => rec.id == newNode.id)); }
public ActionResult <node_relationship> PostRelationshipInBoard([FromRoute] Int32 boardId, [FromBody] dynamic body) { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int16 userId = _userService.GetUserId(token); board board = _context.board.FirstOrDefault(x => x.id == boardId && x.owner_id == userId && x.deleted_at == null); if (board == null) { throw new MindnoteException("嗚喔! 分類已經被刪除,無法瀏覽", HttpStatusCode.NotFound); } int parentNodeId = (int)body.parent_node_id; int childNodeId = (int)body.child_node_id; bool isExists = _context.node_relationship.Where(x => ((x.parent_node_id == parentNodeId && x.child_node_id == childNodeId) || (x.parent_node_id == childNodeId && x.child_node_id == parentNodeId)) && x.deleted_at == null).Count() >= 1; if (isExists) { throw new MindnoteException("關聯建立失敗,不能重複建立連線", HttpStatusCode.ExpectationFailed); } node_relationship nodeRelationship = new node_relationship { parent_node_id = body.parent_node_id, child_node_id = body.child_node_id }; _context.node_relationship.Add(nodeRelationship); _context.SaveChanges(); return(nodeRelationship); }