Advertisement
Guest User

Untitled

a guest
Oct 1st, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Data;
  6.  
  7. using Amazon.Lambda.Core;
  8. using Amazon.Lambda.APIGatewayEvents;
  9. using System.Net.Http;
  10. using MySql.Data.MySqlClient;
  11. using Newtonsoft.Json;
  12.  
  13. using System.IdentityModel.Tokens.Jwt;
  14.  
  15. // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
  16. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
  17.  
  18. namespace InsertNewsfeed
  19. {
  20. public class NewsFeedEntry
  21. {
  22. /// <summary>
  23. /// Attributes
  24. /// </summary>
  25. private int _id;
  26. private string _newsFeedText;
  27. private int _author_id;
  28. private DateTime _creationDate;
  29.  
  30. /// <summary>
  31. /// getter/setter for the Id
  32. /// </summary>
  33. public int id { get => id; set => id = value; }
  34. /// <summary>
  35. /// getter/setter for the FeedbackText
  36. /// </summary>
  37. public string newsfeedText { get => newsfeedText; set => newsfeedText = value; }
  38. /// <summary>
  39. /// getter/setter for the Author
  40. /// </summary>
  41. public int author_id { get => author_id; set => author_id = value; }
  42. /// <summary>
  43. /// getter/setter for the date of creation
  44. /// </summary>
  45. public DateTime createdate { get => createdate; set => createdate = value; }
  46.  
  47. /// <summary>
  48. /// Constructor
  49. /// </summary>
  50. /// <param name="id"></param>
  51. /// <param name="feedbackText"></param>
  52. public NewsFeedEntry(int id, string newsfeedText, int author_id, DateTime createdate)
  53. {
  54. this.id = id;
  55. this.newsfeedText = newsfeedText;
  56. this.author_id = author_id;
  57. this.createdate = createdate;
  58. }
  59. }
  60.  
  61. public class Function
  62. {
  63. public String getUserName(APIGatewayProxyRequest input)
  64. {
  65. input.Headers.TryGetValue("Authorization", out String to);
  66. JwtSecurityTokenHandler myhandler = new JwtSecurityTokenHandler();
  67. var token = myhandler.ReadJwtToken(to);
  68. var claims = token.Claims;
  69. foreach (var c in claims)
  70. {
  71. if (c.Type.Equals("cognito:username"))
  72. {
  73. return c.Value;
  74. }
  75. }
  76. return "";
  77. }
  78.  
  79. public int getUserId(MySqlConnectionStringBuilder anta, String username)
  80. {
  81. MySqlConnection conn = new MySqlConnection(anta.ConnectionString);
  82. conn.Open();
  83. String getUserIdCommand = "SELECT user_id FROM user_name_id WHERE username = '" + username + "';";
  84. MySqlCommand cmd = conn.CreateCommand();
  85. int userid = -1;
  86. //Get The User ID from The user_name_id Tabel
  87. cmd.CommandText = getUserIdCommand;
  88. MySqlDataReader reader = cmd.ExecuteReader();
  89. if (reader.Read())
  90. {
  91. userid = reader.GetInt32("user_id");
  92. }
  93. reader.Close();
  94. conn.Close();
  95. return userid;
  96. }
  97.  
  98. /// <summary>
  99. /// A simple function that takes a string and does a ToUpper
  100. /// </summary>
  101. /// <param name="input"></param>
  102. /// <param name="context"></param>
  103. /// <returns></returns>
  104. public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest input, ILambdaContext context)
  105. {
  106. MySqlConnectionStringBuilder anta = new MySqlConnectionStringBuilder
  107. {
  108. Server = "rea.cvawub2b2icc.eu-central-1.rds.amazonaws.com",
  109. Database = "innodb",
  110. UserID = "root",
  111. Password = "kadfjwbcsiadhj%10SDFGqtn=}"
  112. };
  113. NewsFeedEntry nf = JsonConvert.DeserializeObject<NewsFeedEntry>(input.Body);
  114. //NewsFeedEntry nf = new NewsFeedEntry(-1, "text", 2, new DateTime());
  115. String insertNewsfeedString = "INSERT INTO newsfeed (text, author_id, createdate) VALUES (@text ,@author_id ,@createdate);";
  116. MySqlConnection conn = new MySqlConnection(anta.ConnectionString);
  117. conn.Open();
  118. MySqlCommand cmd = conn.CreateCommand();
  119. cmd.CommandText = insertNewsfeedString;
  120. cmd.Parameters.AddWithValue("@text", nf.newsfeedText);
  121. cmd.Parameters.AddWithValue("@author_id", nf.author_id);
  122. cmd.Parameters.AddWithValue("@createdate", nf.createdate);
  123. cmd.ExecuteNonQuery();
  124. int insertedID = Convert.ToInt32(cmd.LastInsertedId);
  125. conn.Close();
  126. APIGatewayProxyResponse result = new APIGatewayProxyResponse
  127. {
  128. Body = "Create Entry Success (ID:"+ insertedID + ")",
  129. StatusCode = 200
  130. };
  131. return result;
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement