Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Data;
- using Amazon.Lambda.Core;
- using Amazon.Lambda.APIGatewayEvents;
- using System.Net.Http;
- using MySql.Data.MySqlClient;
- using Newtonsoft.Json;
- using System.IdentityModel.Tokens.Jwt;
- // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
- [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
- namespace InsertNewsfeed
- {
- public class NewsFeedEntry
- {
- /// <summary>
- /// Attributes
- /// </summary>
- private int _id;
- private string _newsFeedText;
- private int _author_id;
- private DateTime _creationDate;
- /// <summary>
- /// getter/setter for the Id
- /// </summary>
- public int id { get => id; set => id = value; }
- /// <summary>
- /// getter/setter for the FeedbackText
- /// </summary>
- public string newsfeedText { get => newsfeedText; set => newsfeedText = value; }
- /// <summary>
- /// getter/setter for the Author
- /// </summary>
- public int author_id { get => author_id; set => author_id = value; }
- /// <summary>
- /// getter/setter for the date of creation
- /// </summary>
- public DateTime createdate { get => createdate; set => createdate = value; }
- /// <summary>
- /// Constructor
- /// </summary>
- /// <param name="id"></param>
- /// <param name="feedbackText"></param>
- public NewsFeedEntry(int id, string newsfeedText, int author_id, DateTime createdate)
- {
- this.id = id;
- this.newsfeedText = newsfeedText;
- this.author_id = author_id;
- this.createdate = createdate;
- }
- }
- public class Function
- {
- public String getUserName(APIGatewayProxyRequest input)
- {
- input.Headers.TryGetValue("Authorization", out String to);
- JwtSecurityTokenHandler myhandler = new JwtSecurityTokenHandler();
- var token = myhandler.ReadJwtToken(to);
- var claims = token.Claims;
- foreach (var c in claims)
- {
- if (c.Type.Equals("cognito:username"))
- {
- return c.Value;
- }
- }
- return "";
- }
- public int getUserId(MySqlConnectionStringBuilder anta, String username)
- {
- MySqlConnection conn = new MySqlConnection(anta.ConnectionString);
- conn.Open();
- String getUserIdCommand = "SELECT user_id FROM user_name_id WHERE username = '" + username + "';";
- MySqlCommand cmd = conn.CreateCommand();
- int userid = -1;
- //Get The User ID from The user_name_id Tabel
- cmd.CommandText = getUserIdCommand;
- MySqlDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- {
- userid = reader.GetInt32("user_id");
- }
- reader.Close();
- conn.Close();
- return userid;
- }
- /// <summary>
- /// A simple function that takes a string and does a ToUpper
- /// </summary>
- /// <param name="input"></param>
- /// <param name="context"></param>
- /// <returns></returns>
- public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest input, ILambdaContext context)
- {
- MySqlConnectionStringBuilder anta = new MySqlConnectionStringBuilder
- {
- Server = "rea.cvawub2b2icc.eu-central-1.rds.amazonaws.com",
- Database = "innodb",
- UserID = "root",
- Password = "kadfjwbcsiadhj%10SDFGqtn=}"
- };
- NewsFeedEntry nf = JsonConvert.DeserializeObject<NewsFeedEntry>(input.Body);
- //NewsFeedEntry nf = new NewsFeedEntry(-1, "text", 2, new DateTime());
- String insertNewsfeedString = "INSERT INTO newsfeed (text, author_id, createdate) VALUES (@text ,@author_id ,@createdate);";
- MySqlConnection conn = new MySqlConnection(anta.ConnectionString);
- conn.Open();
- MySqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = insertNewsfeedString;
- cmd.Parameters.AddWithValue("@text", nf.newsfeedText);
- cmd.Parameters.AddWithValue("@author_id", nf.author_id);
- cmd.Parameters.AddWithValue("@createdate", nf.createdate);
- cmd.ExecuteNonQuery();
- int insertedID = Convert.ToInt32(cmd.LastInsertedId);
- conn.Close();
- APIGatewayProxyResponse result = new APIGatewayProxyResponse
- {
- Body = "Create Entry Success (ID:"+ insertedID + ")",
- StatusCode = 200
- };
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement