Skip to content

Commit

Permalink
Added and removed a few files. The two APi now works, and can output …
Browse files Browse the repository at this point in the history
…data as required.
  • Loading branch information
Layusmen committed Jul 27, 2024
1 parent ba22fe8 commit 9665fd2
Show file tree
Hide file tree
Showing 21 changed files with 412 additions and 127 deletions.
81 changes: 81 additions & 0 deletions Alertnity/ApiMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Alertnity
{
internal class ApiMethods
{
public static PostcodeApiResponse PostcodeApiReturnJson(string Url)
{
using (var client = new HttpClient())
{
var endpoint = new Uri(Url);
var result = client.GetAsync(endpoint).Result;
var json = result.Content.ReadAsStringAsync().Result;
var postcodeApiResponseValue = JsonSerializer.Deserialize<PostcodeApiResponse>(json, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
return postcodeApiResponseValue;
}
}

public static List<PostcodeConverter> SavePostcodeApiResponse(PostcodeApiResponse postcodeApiResponseValue)
{

List<PostcodeConverter> converters = new List<PostcodeConverter>();

if (postcodeApiResponseValue != null && postcodeApiResponseValue.Result != null)
{
foreach (var result in postcodeApiResponseValue.Result)
{
PostcodeConverter converter = new PostcodeConverter
{
Latitude = result.Latitude,
Longitude = result.Longitude
};
converters.Add(converter);

Console.WriteLine("Longitude: " + result.Longitude);
Console.WriteLine("Latitude: " + result.Latitude);
Console.WriteLine("............................");
}

}
else
{
Console.WriteLine("Response is null or nothing contained in the api.");
}
return converters;
}

public static string CreatePolyParameter(List<PostcodeConverter> converters)
{
var polyParts = converters.Select(c => $"{c.Latitude},{c.Longitude}");
return string.Join(":", polyParts);
}

public static Outcome[] PoliceApiReturnJson(string Url)
{

using (var client = new HttpClient())
{
var endpoint = new Uri(Url);
var result = client.GetAsync(endpoint).Result;

var json = result.Content.ReadAsStringAsync().Result;

var crimeIncidents = JsonSerializer.Deserialize<Outcome[]>(json, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
return crimeIncidents;
}
}


}

}

26 changes: 26 additions & 0 deletions Alertnity/Codes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alertnity
{
public class Codes
{
public string AdminDistrict { get; set; }
public string AdminCounty { get; set; }
public string AdminWard { get; set; }
public string Parish { get; set; }
public string ParliamentaryConstituency { get; set; }
public string ParliamentaryConstituency2024 { get; set; }
public string Ccg { get; set; }
public string CcgId { get; set; }
public string Ced { get; set; }
public string Nuts { get; set; }
public string Lsoa { get; set; }
public string Msoa { get; set; }
public string Lau2 { get; set; }
public string Pfa { get; set; }
}
}
13 changes: 0 additions & 13 deletions Alertnity/CrimeAddress.cs

This file was deleted.

14 changes: 0 additions & 14 deletions Alertnity/CrimeCategory.cs

This file was deleted.

12 changes: 7 additions & 5 deletions Alertnity/CrimeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace Alertnity
{
public class CrimeInfo
{
public DateTime ReportedDate { get; set; }
public CrimeCategory Category { get; set; }
public DateTime? ReportedDate { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public string Category { get; set; }
public string CrimeOutcome { get; set; }
public string CrimeDescription { get; set; }
public ReportingParty Party { get; set; }
public string CrimeID { get; set; }
public string? CrimeDescription { get; set; }
public string ReportingParty { get; set; }
public string? CrimeID { get; set; }
}
}
11 changes: 11 additions & 0 deletions Alertnity/Location.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Alertnity
{
public class Location
{
public string latitude { get; set; }
public Street street { get; set; }
public string longitude { get; set; }
}
}
22 changes: 22 additions & 0 deletions Alertnity/Outcome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Alertnity.Program;

namespace Alertnity
{
public class Outcome
{
public string category { get; set; }
public string location_type { get; set; }
public Location location { get; set; }
public string context { get; set; }
public Outcome_Status outcome_status { get; set; }
public string persistent_id { get; set; }
public int id { get; set; }
public string location_subtype { get; set; }
public string month { get; set; }
}
}
16 changes: 16 additions & 0 deletions Alertnity/Outcome_Status.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alertnity
{
public class Outcome_Status
{


public string Category { get; set; }
public string Month{ get; set; }
}
}
14 changes: 14 additions & 0 deletions Alertnity/PoliceApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Alertnity.Program;

namespace Alertnity
{
public class PoliceApiResponse
{
public List<Outcome> outcome { get; set; }
}
}
17 changes: 17 additions & 0 deletions Alertnity/Post.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Alertnity
{
internal class Post
{
public int ID { get; set; }

public UserInfo User { get; set; }

public string Title { get; set; }
public string Content { get; set; }
public DateTime Timestamp { get; set; }


}
}
15 changes: 15 additions & 0 deletions Alertnity/PostcodeApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Alertnity.Program;

namespace Alertnity
{
public class PostcodeApiResponse
{
public int Status { get; set; }
public Result[] Result { get; set; }
}
}
5 changes: 2 additions & 3 deletions Alertnity/PostcodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace Alertnity
{
public class PostcodeConverter
{
public string Postcode {get; set;}
public double Longitude {get; set;}
public double Latitude {get; set;}
public double Latitude { get; set; }
public double Longitude { get; set; }
}
}
44 changes: 41 additions & 3 deletions Alertnity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
using System;
using System.Text.Json.Nodes;
using static System.Net.WebRequestMethods;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Security.Cryptography.X509Certificates;



namespace Alertnity
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter postcode:");
string insertPostcode = Console.ReadLine();

Console.WriteLine("Enter Date int this format 2017-01:");
string insertDate = Console.ReadLine();

//Fetching Nearest Postcodes to be able to calculate Community Crime Rate, used 200m radius and 20 postcode limit
var Url = $"https://api.postcodes.io/postcodes/{insertPostcode}/nearest?radius=200&limit=20";
PostcodeApiResponse postcodeApiResponseValue = ApiMethods.PostcodeApiReturnJson(Url);

//Save all Longitude and Latitude Into an instance of List<PostcodeConverter>

List<PostcodeConverter> converters = ApiMethods.SavePostcodeApiResponse(postcodeApiResponseValue);

//Now Insert the postcodeApiResponseValue into Police API
//First convert the response
string poly = ApiMethods.CreatePolyParameter(converters);
//Insert the converted response into the API Url
Url = $"https://data.police.uk/api/crimes-street/all-crime?poly={poly}&date={insertDate}";

Console.WriteLine(Url);


Outcome[] crimeIncidents = ApiMethods.PoliceApiReturnJson(Url);



/*
//Single User Information
Console.WriteLine($"\nUser General Information is being attached:\n");
UserInfo userInfo = UserInputMethods.CreateUserInfo();
Expand All @@ -18,7 +54,7 @@ static void Main(string[] args)
//Single User Posts.
Console.WriteLine($"\nUser Post Information is being attached:\n");
UserPost userPost = UserInputMethods.CreateUserPost(userInfo, 101, "Hi Everyone", "This is my first post!");
Post userPost = UserInputMethods.CreateUserPost(userInfo, 101, "Hi Everyone", "This is my first post!");
UserPrintMethods.PrintUserPost(userPost);
//Crime Reporter
Expand All @@ -33,7 +69,7 @@ static void Main(string[] args)
//Print Converted Postcode
Console.WriteLine($"\nInformation about the converted postcode:\n");
PostcodeConverter postcodeConverter = UserInputMethods.ConvertPostcodeToLongitudeAndLatitude(crimeAddress);
PostcodeConverter postcodeConverter = UserInputMethods.CrimeAddressPostcodeConverter(crimeAddress);
UserPrintMethods.PrintPostcodeInLongitudeAndLatitude(postcodeConverter);
//Upvote Dummy Calculation
Expand All @@ -46,6 +82,8 @@ static void Main(string[] args)
Console.WriteLine("Upvotes: " + vote.Upvotes);
Console.WriteLine("Downvotes: " + vote.Downvotes);
Console.WriteLine("Total Score: " + vote.GetTotalScore());
*/

}
}
}
}
13 changes: 0 additions & 13 deletions Alertnity/ReportingParty.cs

This file was deleted.

Loading

0 comments on commit 9665fd2

Please sign in to comment.