public ActionResult <dynamic> Unsubscribe() { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int32 userId = _userService.GetUserId(token); view_user user = _contextForView.view_user.FirstOrDefault(x => x.id == userId); if (!user.is_subscribed) { throw new MindnoteException("痾~你好像不是訂閱用戶", HttpStatusCode.ExpectationFailed); } transaction existedTransaction = _context.transaction.FirstOrDefault(x => x.id == user.transaction_id); if (!existedTransaction.is_next_subscribe) { throw new MindnoteException("已經取消訂閱囉~下一期我們將停止扣款"); } // transaction existedTransaction = new transaction { id = user.transaction_id ?? -1 }; // _context.Attach<transaction>(existedTransaction); existedTransaction.is_next_subscribe = false; _context.SaveChanges(); JSONResponse json = new JSONResponse(JSONResponseStatus.OK, new { }); return(json.toResponseObj()); }
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 <dynamic> DeleteBoard([FromRoute] Int32 boardId) { 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); } board.deleted_at = DateTime.Now; board.updated_at = DateTime.Now; _context.SaveChanges(); JSONResponse result = new JSONResponse(JSONResponseStatus.OK, new { }); return(result.toResponseObj()); }
public ActionResult <node> PatchNode([FromRoute] Int32 boardId, [FromRoute] Int16 nodeId, [FromBody] dynamic requestBody) { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int16 userId = _userService.GetUserId(token); node node = _context.node.FirstOrDefault(x => x.id == nodeId && x.owner_id == userId && x.board_id == boardId && x.deleted_at == null); if (node == null) { throw new MindnoteException("嗚喔! 分類已經被刪除,無法瀏覽", HttpStatusCode.NotFound); } if (requestBody.title != null) { node.title = requestBody.title; } if (requestBody.description != null) { node.description = requestBody.description; } if (requestBody.x != null) { node.x = requestBody.x; } if (requestBody.y != null) { node.y = requestBody.y; } if (requestBody.cover != null) { node.cover = requestBody.cover; } node.updated_at = DateTime.Now; _context.SaveChanges(); return(node); }
public ActionResult <int> DeleteRelationshipInBoard([FromRoute] Int32 boardId, [FromBody] dynamic requestBody) { 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[] relationshipIds = requestBody.relationshipIds.ToObject <int[]>(); List <node_relationship> relationships = _context.node_relationship.Where(x => relationshipIds.Contains(x.id) && x.deleted_at == null).ToList(); foreach (node_relationship relationship in relationships) { relationship.deleted_at = DateTime.Now; } return(_context.SaveChanges()); }
public async Task <dynamic> Post([FromBody] dynamic postBody) { string token = postBody["code"]; // string authorization = Request.Headers["Authorization"]; // if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) { // validate token HttpClient http = new HttpClient(); string googleValidateUserUrl = "https://www.googleapis.com/oauth2/v3/userinfo?access_token="; HttpResponseMessage response = await http.GetAsync(googleValidateUserUrl + token); string result = await response.Content.ReadAsStringAsync(); if ((int)response.StatusCode == StatusCodes.Status200OK) { // check user exists var userFromGoogle = JsonConvert.DeserializeObject <JObject>(result); string sub = (string)userFromGoogle["sub"]; string email = (string)userFromGoogle["email"]; string name = (string)userFromGoogle["name"]; user currentUser; if (_context.user.Where(rec => rec.sub == sub).Count() == 0) { currentUser = new user { email = email, provider = "GOOGLE", sub = sub, full_name = name }; _context.user.Add(currentUser); _context.SaveChanges(); } else { currentUser = _context.user.SingleOrDefault(rec => rec.sub == sub); } userFromGoogle.Add("username", currentUser.username); // generate token userFromGoogle.Add("token", _userService.GenerateToken(currentUser)); return(userFromGoogle); } else { throw new Exception("無效憑證: " + result); } }
public ActionResult <board> PostBoard([FromBody] dynamic body) { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int32 userId = _userService.GetUserId(token); view_user user = _contextForView.view_user.FirstOrDefault(x => x.id == userId); if (user.board_count >= 2 && !user.is_subscribed) { // throw new MindnoteException("Add board deny, because free account only have two boards. If you need to create new board, join us just pay $3 per month to subscribe our service.", HttpStatusCode.ExpectationFailed); throw new MindnoteException("因為免費使用者只有兩個分類可以使用,所以新增分類失敗,如果你需要更多的分類來整理筆記,一個月只需要 $ 99 元,就能使用無限多的分類,請點擊這則訊息進入到付款流程。", HttpStatusCode.ExpectationFailed); } board newBoard = new board { title = body.title, uniquename = body.uniquename, owner_id = userId }; _context.board.Add(newBoard); _context.SaveChanges(); return(_context.board.SingleOrDefault(rec => rec.id == newBoard.id)); }
public ActionResult <dynamic> PostImages([FromBody] dynamic requestBody) { string authorization = Request.Headers["Authorization"]; string token = authorization.Substring("Bearer ".Length).Trim(); Int16 userId = _userService.GetUserId(token); view_user user = _contextForView.view_user.FirstOrDefault(x => x.id == userId); if (user == null) { throw new MindnoteException("你不是帳號的擁有者", HttpStatusCode.NotFound); } GoogleCredential gc = GoogleCredential.FromJson(_GCSCredential); StorageClient client = StorageClient.Create(gc); for (int i = 0; i < requestBody.base64Files.Count; i++) { //check all file content type; string contentType = requestBody.base64Files[i].contentType.Value; if (!contentType.StartsWith("image/")) { throw new MindnoteException("你上傳了非圖片的檔案"); } } // post to gcs List <dynamic> result = new List <dynamic>(); for (int i = 0; i < requestBody.base64Files.Count; i++) { //check all file content type; string base64Data = requestBody.base64Files[i].data.Value; string contentType = requestBody.base64Files[i].contentType.Value; string clientSideFlagId = requestBody.base64Files[i].clientSideFlagId != null ? requestBody.base64Files[i].clientSideFlagId.Value : ""; decimal width = -1; if (requestBody.base64Files[i].width != null) { width = (decimal)requestBody.base64Files[i].width.Value; } decimal height = -1; if (requestBody.base64Files[i].height != null) { height = (decimal)requestBody.base64Files[i].height.Value; } int nodeId = -1; if (requestBody.base64Files[i].nodeId != null) { nodeId = (int)requestBody.base64Files[i].nodeId.Value; } string extensionFilename = ".jpg"; if (contentType == "image/jpg" || contentType == "image/jpeg") { extensionFilename = ".jpg"; } else if (contentType == "image/png") { extensionFilename = ".png"; } else if (contentType == "image/gif") { extensionFilename = ".gif"; } else if (contentType == "image/bmp") { extensionFilename = ".bmp"; } else { extensionFilename = contentType.Replace("image/", ""); } string id = Guid.NewGuid().ToString("N"); MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Data)); Decimal size = stream.Length; if ((user.storage_usage + size) / 1024 / 1024 > 200) { throw new MindnoteException("上傳的圖片已經超過免費使用者的上限 (200 M),只要每月 99 元,就能享有 100 倍的上傳空間", HttpStatusCode.ExpectationFailed); } else if ((user.storage_usage + size) / 1024 / 1024 / 1024 > 20) { throw new MindnoteException("上傳的圖片已經超過付費使用者的上限 (20 G),請聯繫管理員 [email protected]", HttpStatusCode.InternalServerError); } Google.Apis.Storage.v1.Data.Object resultFromGCS = client.UploadObject(_GCSBucketName, id + extensionFilename, contentType, stream, new UploadObjectOptions() { UserProject = _GCPProjectId, }); JSONResponseStatus status = JSONResponseStatus.OK; if (resultFromGCS.Name == null) { status = JSONResponseStatus.FAILED; } dynamic resultItem = new ExpandoObject(); resultItem.status = Enum.GetName(typeof(JSONResponseStatus), status); resultItem.filename = resultFromGCS.Name; resultItem.clientSideFlagId = clientSideFlagId ?? ""; resultItem.nodeId = nodeId; resultItem.size = resultFromGCS.Size; resultItem.width = width; resultItem.height = height; result.Add(resultItem); } // check gcs result and save local db for (int i = 0; i < result.Count; i++) { if (result[i].status == Enum.GetName(typeof(JSONResponseStatus), JSONResponseStatus.OK)) { image image = new image { owner_id = userId, filename = result[i].filename, size = result[i].size, }; if (result[i].width != -1) { image.width = result[i].width; } if (result[i].height != -1) { image.height = result[i].height; } if (result[i].nodeId != -1) { image.node_id = result[i].nodeId; } result[i].imageContext = image; _context.image.Add(image); } } _context.SaveChanges(); return(result); }