public async Task <FeedPost> GetPostById(int postid) { using (var conn = new MySqlConnection(connString)) //Creates a temporary new Connectio { await conn.OpenAsync(); //Waits for connection to open using (var cmd = new MySqlCommand($"SELECT * FROM feed_post WHERE `PostId`={postid};", conn)) //Select command to get the row of the id using (var reader = await cmd.ExecuteReaderAsync()) //Executes the above command { if (await reader.ReadAsync()) //If any row was returned { FeedPost post = new FeedPost //Create new FeedPost object from our returned values { postId = Convert.ToInt32(reader["postId"]), posterId = Convert.ToInt32(reader["posterId"]), postType = reader["postType"].ToString(), subjectId = Convert.ToInt32(reader["subjectId"]), datePosted = Convert.ToDateTime(reader["datePosted"]), isAnnouncement = Convert.ToBoolean(reader["isAnnouncement"]), isDeleted = Convert.ToBoolean(reader["isDeleted"]), poster = await new UserTasks().GetUserById(Convert.ToInt32(reader["posterId"]), flatten: true), likes = await GetAllLikesForPost(Convert.ToInt32(reader["postId"])) }; string json = Json.Stringify(post); //Convert the above object into a json string switch (post.postType) //What to do for each post type { case "text": //IF it is a text post FeedTextPost tPost = Json.Parse <FeedTextPost>(json); //Convert the above json into a FeedTextPost object tPost = await AddTextPostValues(tPost); //Create a new object from the above one, with our additional text values return(tPost); //return it case "media": FeedMediaPost mPost = Json.Parse <FeedMediaPost>(json); //Convert the abve json intoa FeedMediaPost object mPost = await AddMediaPostValues(mPost); //Create a new object from the above, with our additional media values return(mPost); //return it case "poll": //If it is a poll FeedPoll pPost = Json.Parse <FeedPoll>(json); //Convert above json to a FeedPoll object pPost = await AddPollPostValues(pPost); //Create a new object from the above, including additional values return(pPost); //return it case "quiz": FeedQuiz fPost = Json.Parse <FeedQuiz>(json); //Convert json into FeedQuiz object fPost = await AddBasicQuizValues(fPost); //Adds quiz values return(fPost); //return it default: return(null); //If the switch statement fails, return nothing. } } } return(null); //If no row is returned, return nothing. } }
public async Task <FeedPost> UploadMediaPost(FeedMediaPost post) { using (var conn = new MySqlConnection(connString)) //Creates new temp connection { await conn.OpenAsync(); //Waits for connection to open using (var cmd = new MySqlCommand($"INSERT INTO feed_post VALUES({0}, 'media', {post.posterId}, {post.subjectId}, '{post.datePosted.ToString("yyyy-MM-dd hh:mm:ss")}', " + $"{Convert.ToBoolean(post.isAnnouncement)}, {Convert.ToBoolean(post.isDeleted)});", conn)) //Inserts post into feed_post { await cmd.ExecuteNonQueryAsync(); //Executes that command using (var cmd2 = new MySqlCommand($"INSERT INTO feed_media_post VALUES ({cmd.LastInsertedId}, '{post.urlToPost}', {post.isVideo}," + $" '{post.postDescription}');", conn)) //Inserts the above post into feed_text_post { await cmd2.ExecuteNonQueryAsync(); //Executes the above command return(await GetPostById((int)cmd.LastInsertedId)); //Returns the new post object } } } }
public async Task <FeedMediaPost> AddMediaPostValues(FeedMediaPost post) { using (var conn = new MySqlConnection(connString)) //New connection { await conn.OpenAsync(); //Opens connection using (var cmd = new MySqlCommand($"SELECT * FROM feed_media_post WHERE `PostId`={post.postId};", conn)) // ^ Selects all values frm the selecte drow using (var reader = await cmd.ExecuteReaderAsync()) //Read the row { if (await reader.ReadAsync()) //If anything is returned { post.urlToPost = reader["urlToPost"].ToString(); post.isVideo = Convert.ToBoolean(reader["isVideo"]); post.postDescription = reader["description"].ToString(); // ^ Add the media values to the original object return(post); //return the object } } return(null); //Else, return nothing. } }
public async Task <IActionResult> UploadMediaPost([FromBody] FeedMediaPost post) { return(Ok(await new FeedTasks().UploadMediaPost(post))); }