-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
181 lines (164 loc) · 4.82 KB
/
Program.cs
File metadata and controls
181 lines (164 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using MinimalisticWeatherAPI;
using System.Text.Json;
using System.Text.RegularExpressions;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
string Datapath = "Data/weatherData.json";
List<Weather>? weatherList = new List<Weather>();
await ReadWeatherList();
app.Run(async (context) =>
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
var path = request.Path;
string expresion = @"^/weather/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$";
if (path == "/weather" && request.Method == "GET")
{
await GetAllWeather(response);
}
else if (Regex.IsMatch(path, expresion) && request.Method == "GET")
{
string? id = path.Value?.Split('/')[2];
await GetWeather(id, response);
}
else if (Regex.IsMatch(path, expresion) && request.Method == "DELETE")
{
string? id = path.Value?.Split('/')[2];
await DeleteWeather(id, response);
}
else if(path == "/weather" && request.Method == "POST")
{
await CreateWeather(request, response);
}
else if(path == "/weather" && request.Method == "PUT")
{
await UpdateWeather(response,request);
}
else if (File.Exists("WeatherImages" + path))
{
await response.SendFileAsync("WeatherImages" + path);
}
else if (path == "/css")
{
await response.SendFileAsync("html/StyleSheet.css");
}
else
{
response.ContentType = "text/html; charset=utf-8";
await response.SendFileAsync("html/client.html");
}
});
app.Run();
async Task GetAllWeather(HttpResponse response)
{
if (weatherList?.Count > 0)
await response.WriteAsJsonAsync(weatherList.ToArray());
else
{
response.StatusCode = 404;
await response.WriteAsJsonAsync("There is no weather for you");
}
}
async Task GetWeather(string? id, HttpResponse response)
{
Weather? weather = weatherList?.FirstOrDefault((x) => x.Id == id);
if (weather != null)
await response.WriteAsJsonAsync(weather);
else
{
response.StatusCode = 404;
await response.WriteAsJsonAsync("Cannot find that weather");
}
}
async Task CreateWeather(HttpRequest request, HttpResponse response)
{
Weather? weather = await request.ReadFromJsonAsync<Weather>();
if(weather != null)
{
weather.Id = Guid.NewGuid().ToString();
weather.ImageId = GetWeatherImage(weather);
weatherList?.Add(weather);
await UpdateWeatherList();
await response.WriteAsJsonAsync(weather);
}
else
{
response.StatusCode = 404;
await response.WriteAsJsonAsync("Unable to set your weather");
}
}
async Task DeleteWeather(string? id, HttpResponse response)
{
Weather? weather = weatherList?.FirstOrDefault(x => x.Id == id);
if(weather != null)
{
weatherList?.Remove(weather);
await UpdateWeatherList();
await response.WriteAsJsonAsync(weather);
}
else
{
response.StatusCode = 404;
await response.WriteAsJsonAsync("Unable to delete this weather");
}
}
async Task UpdateWeather(HttpResponse response, HttpRequest request)
{
Weather? weather = await request.ReadFromJsonAsync<Weather>();
if (weather != null)
{
Weather? w = weatherList?.FirstOrDefault(x => x.Id == weather.Id);
if(w != null)
{
w.Temp = weather.Temp;
w.Humidity = weather.Humidity;
await response.WriteAsJsonAsync(w);
await UpdateWeatherList();
}
}
else
{
response.StatusCode = 404;
await response.WriteAsJsonAsync("Unable to find thit weather");
}
}
async Task ReadWeatherList()
{
using (FileStream fs = new FileStream(Datapath, FileMode.OpenOrCreate))
{
if(fs.Length > 0)
{
weatherList = await JsonSerializer.DeserializeAsync<List<Weather>>(fs);
}
if (weatherList != null)
{
foreach (var w in weatherList)
{
if (w.Id == "")
{
w.Id = Guid.NewGuid().ToString();
}
if (w.ImageId == "")
{
w.ImageId = GetWeatherImage(w);
}
}
}
}
}
async Task UpdateWeatherList()
{
using (FileStream fs = new FileStream(Datapath, FileMode.Truncate))
{
if(weatherList != null)
{
await JsonSerializer.SerializeAsync<List<Weather>>(fs, weatherList);
}
}
}
string GetWeatherImage(Weather w)
{
if (w.Humidity < 25) return "https://localhost:7232/sun.png"; //"https://solarsystem.nasa.gov/system/basic_html_elements/11561_Sun.png"
else if (w.Humidity > 25 && w.Humidity < 75) return "https://localhost:7232/cloudy.png";
else return "https://localhost:7232/cloud.png";
}