Created
October 22, 2018 20:07
-
-
Save C00kiie/38ddddc15846c233816fd65082ff86cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using Newtonsoft; | |
using System.Collections; | |
using Newtonsoft.Json; | |
namespace Tasks | |
{ | |
// task one | |
/* API */ | |
public class CommentsDTO | |
{ | |
[JsonProperty("postId")] | |
public int postId; | |
[JsonProperty("Id")] | |
public int id; | |
[JsonProperty("name")] | |
public string name; | |
[JsonProperty("email")] | |
public string email; | |
[JsonProperty("body")] | |
public string body; | |
} | |
public class PostsDTO | |
{ | |
public int userId; | |
public int id; | |
public string title; | |
public string body; | |
public List<CommentsDTO> comments = new List<CommentsDTO>() { }; | |
} | |
public class Fetch | |
{ | |
public string Posts_API, comments_API; | |
public List<PostsDTO> all_posts; | |
public List<CommentsDTO> all_comments; | |
public List<string> JsonObjects; | |
public Fetch(string Posts_API, string comments_API) | |
{ | |
this.comments_API = comments_API; | |
this.Posts_API = Posts_API; | |
json2lists(); | |
appendComments2Posts(); | |
} | |
private void json2lists() | |
{ | |
WebRequest PostsAPI_Request = WebRequest.Create(Posts_API); | |
WebRequest CommentsAPI_Request = WebRequest.Create(comments_API); | |
// Get the response. | |
HttpWebResponse PostsAPIRequest = (HttpWebResponse)PostsAPI_Request.GetResponse(); | |
HttpWebResponse CommentsAPIRequest = (HttpWebResponse)CommentsAPI_Request.GetResponse(); | |
// Display the status. | |
// Console.WriteLine(PostsResponse.StatusDescription); | |
// Get the stream containing content returned by the server. | |
Stream posts_stream = PostsAPIRequest.GetResponseStream(); | |
Stream comments_stream = CommentsAPIRequest.GetResponseStream(); | |
// Open the stream using a StreamReader for easy access. | |
StreamReader posts_reader = new StreamReader(posts_stream); | |
StreamReader comments_reader = new StreamReader(comments_stream); | |
// Read the content. | |
string postsJsonString = posts_reader.ReadToEnd(); | |
string commentsJsonString = comments_reader.ReadToEnd(); | |
// Cleanup the streams and the response. | |
posts_reader.Close(); | |
comments_reader.Close(); | |
// streams | |
posts_stream.Close(); | |
comments_stream.Close(); | |
// responses. | |
PostsAPIRequest.Close(); | |
CommentsAPIRequest.Close(); | |
var comments = JsonConvert.DeserializeObject<List<CommentsDTO>>(commentsJsonString); | |
var posts = JsonConvert.DeserializeObject<List<PostsDTO>>(postsJsonString); | |
Console.WriteLine(posts[1].body); | |
this.all_comments = comments; | |
this.all_posts = posts; | |
} | |
private void appendComments2Posts() | |
{ | |
var comments = this.all_comments; | |
foreach (var post in all_posts) | |
{ | |
foreach (var comment in all_comments) | |
{ | |
if (comment.postId == post.id) | |
{ | |
post.comments.Add(comment); | |
} | |
} | |
} | |
} | |
public void getPostsByID(int userId) | |
{ | |
foreach (var post in all_posts) | |
{ | |
if (post.userId == userId) | |
{ | |
Console.WriteLine("POST: " + post.body); | |
for (int i = 0; i < post.comments.Count; i++) | |
{ | |
Console.WriteLine("COMMENT NO." + i + "\t" + post.comments[i].body); | |
} | |
} | |
} | |
} | |
public class TestUnit | |
{ | |
public static void Main() | |
{ | |
//sorry that I didn't provide the steps, | |
// I did this pretty lately because I do two jobs, | |
// which leaves me with little to no time | |
// hopefully the code isn't a potato.. :) | |
string posts_api = "https://jsonplaceholder.typicode.com/posts"; | |
string comments_api = "https://jsonplaceholder.typicode.com/comments"; | |
//Fetch fetch = new Fetch(posts_api, comments_api); | |
//fetch.getPostsByID(5); | |
string[] results = Tasks.Functions.number2string(new int[5] { | |
5, | |
8, | |
10, | |
0, | |
1 | |
}); | |
for (int i = 0; i < results.Length; i++) | |
{ | |
Console.WriteLine(results[i]); | |
} | |
/* FIVE | |
EIGHT | |
TEN | |
ZERO | |
ONE*/ | |
} | |
} | |
} | |
// | |
// TASK TWO HERE BELOW | |
// | |
public class Functions | |
{ | |
//task two | |
/* Q2: Write a program that accepts an array of numbers and returns an array of numbers in written form e.g. | |
[1,4,6] → [“one”, “four”, “six”] | |
[0,0,6,2,7] → [“zero”, “zero”, “six”,"two","seven"] | |
[5,4,3,2,1,5,8] → ["five", "four", "three", "two", "one", "five","eight"]*/ | |
public enum conversations | |
{ | |
ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5, SIX = 6, SEVEN = 7, EIGHT = 8, NINE = 9, TEN = 10 | |
} | |
public static string[] number2string(int[] array) | |
{ | |
int SIZE = array.Length; | |
String[] string_ = new string[SIZE]; | |
for (int i = 0; i < SIZE; i++) | |
{ | |
string_[i] = Enum.ToObject(typeof(conversations), array[i]).ToString(); | |
} | |
return string_; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment