This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
AQ56DX - Weather #9
Open
sziligunz
wants to merge
7
commits into
CsharptutorialHungary:main
Choose a base branch
from
sziligunz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44d5652
Created all the files for the project.
b80b0b3
Added model for the API's json repsonse. Added WeatherPareser. Added …
6e4ff08
Added get now command. Added null pointer safety.
b2ff379
Added get now short command.
5c0a48b
Added exit command.
961e9c8
Code cleanup.
6a9a53d
Added info command. Code cleanup.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Weather.Controller | ||
{ | ||
internal static class WeatherAPI | ||
{ | ||
public static async Task<string> GetWeatherDataAsync() | ||
{ | ||
HttpClient client = new HttpClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A HttpClient IDisposable, szóval egy using blokk-ban lenne érdemes használni, mert memory leakelni fog. |
||
string res = String.Empty; | ||
HttpResponseMessage response = await client.GetAsync("https://api.openweathermap.org/data/2.5/forecast?lat=46.253&lon=20.14824&units=metric&appid=9c5ec3c52fdd66e52bba3b00dc4fcc37"); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
res = await response.Content.ReadAsStringAsync(); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("An error occured during reaching weather web API."); | ||
} | ||
return res; | ||
} | ||
} | ||
} |
This file contains 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
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; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal interface IWeatherCommand | ||
{ | ||
string Name { get; } | ||
|
||
Task<bool> Execute(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandExit : IWeatherCommand | ||
{ | ||
public string Name => "exit"; | ||
|
||
public Task<bool> Execute() | ||
{ | ||
Environment.Exit(0); | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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
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; | ||
using Weather.Controllers; | ||
using Weather.Models; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandGetAll : IWeatherCommand | ||
{ | ||
public string Name => "get all"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
var response = await WeatherReportParser.GetCurrentWeather(); | ||
if (response == null || response.list == null || response.list.Count == 0) return false; | ||
foreach (ModelWeatherReport i in response.list) | ||
{ | ||
Console.WriteLine(i.ToString()); | ||
} | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Weather.Controllers; | ||
using Weather.Models; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandGetNow : IWeatherCommand | ||
{ | ||
public string Name => "get now"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
var response = await WeatherReportParser.GetCurrentWeather(); | ||
if (response == null || response.list == null || response.list.Count == 0) return false; | ||
DateTime time; | ||
var now = response.list.Find(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt) > DateTime.Now); | ||
if (now == null) return false; | ||
var builder = new WeatherReportStringBuilder(); | ||
Console.WriteLine($"At the time: {builder.formatWeatherReport(now)}"); | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Weather.Controllers; | ||
using Weather.Models; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandGetNowShort : IWeatherCommand | ||
{ | ||
public string Name => "get now short"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
var response = await WeatherReportParser.GetCurrentWeather(); | ||
if (response == null || response.list == null || response.list.Count == 0) return false; | ||
DateTime time; | ||
var now = response.list.Find(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt) > DateTime.Now); | ||
if (now == null) return false; | ||
var builder = new WeatherReportStringBuilder(); | ||
Console.WriteLine($"{builder.shortFromatWeatherReport(now)}"); | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Weather.Controllers; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandGetToday : IWeatherCommand | ||
{ | ||
public string Name => "get today"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
var response = await WeatherReportParser.GetCurrentWeather(); | ||
if (response == null || response.list == null || response.list.Count == 0) return false; | ||
DateTime time; | ||
var today = response.list.FindAll(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt).Day == DateTime.Now.Day); | ||
var builder = new WeatherReportStringBuilder(); | ||
if (today.Count == 0) return false; | ||
Console.WriteLine("Todays forecast:"); | ||
foreach (var i in today) { | ||
Console.WriteLine(builder.formatWeatherReport(i)); | ||
} | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Weather.Controllers; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandInfo : IWeatherCommand | ||
{ | ||
public string Name => "info"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
var response = await WeatherReportParser.GetCurrentWeather(); | ||
if (response == null || response.list == null || response.list.Count == 0) return false; | ||
DateTime time; | ||
foreach (var weather in response.list) | ||
{ | ||
if (String.IsNullOrEmpty(weather.dt_txt) || !DateTime.TryParse(weather.dt_txt, out time)) | ||
{ | ||
Console.WriteLine("Couldn't find oldest and youngest weather report."); | ||
return false; | ||
} | ||
} | ||
var youngest = response.list.MaxBy(x => DateTime.Parse(x.dt_txt)); // line 19: checked already if x.dt_txt is null or empty or can not be parsed | ||
var oldest = response.list.MinBy(x => DateTime.Parse(x.dt_txt)); // line 19: checked already if x.dt_txt is null or empty or can not be parsed | ||
Console.WriteLine($"\tOldest data: {oldest.dt_txt}"); | ||
Console.WriteLine($"\tYoungest data: {youngest.dt_txt}"); | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Weather.Controller; | ||
using Weather.Controllers; | ||
using Weather.Models; | ||
|
||
namespace Weather.Commands | ||
{ | ||
internal class WeatherCommandRefresh : IWeatherCommand | ||
{ | ||
public string Name => "refresh"; | ||
|
||
public async Task<bool> Execute() | ||
{ | ||
string apiResponse = String.Empty; | ||
try | ||
{ | ||
apiResponse = await WeatherAPI.GetWeatherDataAsync(); | ||
} | ||
catch (Exception e) | ||
{/*aswswarfgddujjjklkÉL122 | ||
65 <--- ez itt a macska műve*/ | ||
Console.WriteLine(e.Message); | ||
return false; | ||
} | ||
if(!(await WeatherIO.WriteOut(apiResponse))) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Weather.Controllers | ||
{ | ||
internal static class WeatherIO | ||
{ | ||
private static string path = Path.Combine(AppContext.BaseDirectory, "weather_data.json"); | ||
|
||
public static async Task<bool> WriteOut(string message) | ||
{ | ||
try | ||
{ | ||
using (StreamWriter sw = File.CreateText(path)) | ||
{ | ||
await sw.WriteLineAsync(message); | ||
} | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine(ioe.Message); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static async Task<string> ReadIn() | ||
{ | ||
string data = string.Empty; | ||
try | ||
{ | ||
using (StreamReader sr = File.OpenText(path)) | ||
{ | ||
data = await sr.ReadToEndAsync(); | ||
} | ||
} | ||
catch (FileNotFoundException fnfe) | ||
{ | ||
Console.WriteLine($"Couldn't find file at: {path}. TRY RUNNING 'refresh'!"); | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine(ioe.Message); | ||
} | ||
return data; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Text.Json; | ||
using Weather.Models; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Unicode; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Weather.Controllers | ||
{ | ||
internal static class WeatherReportParser | ||
{ | ||
private static JsonSerializerOptions parserOptions = new() | ||
{ | ||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), | ||
}; | ||
|
||
private static ModelJsonResponse? DeserializeModelWeather(string input) | ||
{ | ||
ModelJsonResponse? wObject = JsonSerializer.Deserialize<ModelJsonResponse>(input, parserOptions); | ||
if (wObject == null) | ||
{ | ||
Console.WriteLine("Couldn't parse to ModelJsonResponse object from input string."); | ||
} | ||
return wObject; | ||
} | ||
|
||
public static string SerializeModelWeather(ModelJsonResponse input) | ||
{ | ||
return JsonSerializer.Serialize(input, parserOptions); | ||
} | ||
|
||
public static async Task<ModelJsonResponse?> GetCurrentWeather() | ||
{ | ||
string data = await WeatherIO.ReadIn(); | ||
ModelJsonResponse modelJsonResponse; | ||
try | ||
{ | ||
modelJsonResponse = DeserializeModelWeather(data); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e.Message); | ||
return null; | ||
} | ||
if (modelJsonResponse == null || modelJsonResponse.list == null) | ||
{ | ||
Console.WriteLine("The weather report which is provided by the weather API doesn't conatain any actual data."); | ||
} | ||
return modelJsonResponse; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Összességében szép munka. Pár apró komment amibe bele tudtam kötni, de tényleg szép. Well done.