Skip to content

Commit 2208170

Browse files
committed
Update weather
1 parent 7bbf104 commit 2208170

16 files changed

Lines changed: 362 additions & 375 deletions

Entities/ThemeEntity.cs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
1-
using System;
2-
using System.Collections.Generic;
31
using Azure;
42
using Azure.Data.Tables;
53
using Newtonsoft.Json;
64

7-
namespace MetroStart.Entities
5+
namespace MetroStart.Entities;
6+
7+
public class ThemeEntity : ITableEntity
88
{
9-
public class ThemeEntity : ITableEntity
9+
public ThemeEntity()
10+
{
11+
}
12+
public ThemeEntity(string author, string title, bool online, Dictionary<string, string> themeContent)
1013
{
11-
public ThemeEntity(string author, string title, bool online, Dictionary<string, string> themeContent)
12-
{
13-
PartitionKey = author;
14-
RowKey = title;
15-
Online = online;
16-
ThemeContent = themeContent;
17-
}
18-
public ThemeEntity()
19-
{
20-
}
14+
PartitionKey = author;
15+
RowKey = title;
16+
Online = online;
17+
ThemeContent = themeContent;
18+
}
2119

22-
public string PartitionKey { get; set; }
23-
public string RowKey { get; set; }
24-
public string Author => PartitionKey;
25-
public string Title => RowKey;
26-
public DateTimeOffset? Timestamp { get; set; }
27-
public ETag ETag { get; set; }
28-
public bool Online { get; set; }
29-
public string ThemeContentJson { get; set; }
30-
public Dictionary<string, string> ThemeContent
31-
{
32-
get => string.IsNullOrEmpty(ThemeContentJson)
33-
? []
34-
: JsonConvert.DeserializeObject<Dictionary<string, string>>(ThemeContentJson) ?? [];
35-
set => ThemeContentJson = JsonConvert.SerializeObject(value ?? []);
36-
}
37-
public override string ToString() => $"(Author: {Author}, Title: {Title})";
20+
public string PartitionKey { get; set; }
21+
public string RowKey { get; set; }
22+
public string Author => PartitionKey;
23+
public string Title => RowKey;
24+
public DateTimeOffset? Timestamp { get; set; }
25+
public ETag ETag { get; set; }
26+
public bool Online { get; set; }
27+
public string ThemeContentJson { get; set; }
28+
public Dictionary<string, string> ThemeContent
29+
{
30+
get => string.IsNullOrEmpty(ThemeContentJson)
31+
? []
32+
: JsonConvert.DeserializeObject<Dictionary<string, string>>(ThemeContentJson) ?? [];
33+
set => ThemeContentJson = JsonConvert.SerializeObject(value ?? []);
3834
}
35+
public override string ToString() => $"(Author: {Author}, Title: {Title})";
3936
}

Entities/WeatherEntity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public WeatherEntity()
2626
public TimeSpan WeatherForecastAge => DateTime.Now - WeatherForecastModified;
2727

2828
public string CurrentWeatherJson { get; set; }
29-
public CurrentWeatherResponse CurrentWeather
29+
public CurrentWeatherResponse? CurrentWeather
3030
{
3131
get => CurrentWeatherResponse.FromJson(CurrentWeatherJson);
3232
set
@@ -37,7 +37,7 @@ public CurrentWeatherResponse CurrentWeather
3737
}
3838

3939
public string WeatherForecastJson { get; set; }
40-
public WeatherForecastResponse WeatherForecast
40+
public WeatherForecastResponse? WeatherForecast
4141
{
4242
get => WeatherForecastResponse.FromJson(WeatherForecastJson);
4343
set

Extensions.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
namespace MetroStart
1+
namespace MetroStart;
2+
3+
public static class Extensions
24
{
3-
public static class Extensions
5+
// https://stackoverflow.com/a/36191436/24391
6+
public static string ToHumanReadableString(this TimeSpan t)
47
{
5-
public static string Nullable(this string @string) => string.IsNullOrEmpty(@string) ? null : @string;
6-
7-
// https://stackoverflow.com/a/36191436/24391
8-
public static string ToHumanReadableString(this TimeSpan t)
8+
if (t.TotalSeconds <= 1)
99
{
10-
if (t.TotalSeconds <= 1)
11-
{
12-
return $@"{t:s\.ff} seconds";
13-
}
14-
if (t.TotalMinutes <= 1)
15-
{
16-
return $@"{t:%s} seconds";
17-
}
18-
if (t.TotalHours <= 1)
19-
{
20-
return $@"{t:%m} minutes";
21-
}
22-
if (t.TotalDays <= 1)
23-
{
24-
return $@"{t:%h} hours";
25-
}
26-
27-
return $@"{t:%d} days";
10+
return $@"{t:s\.ff} seconds";
11+
}
12+
if (t.TotalMinutes <= 1)
13+
{
14+
return $@"{t:%s} seconds";
2815
}
16+
if (t.TotalHours <= 1)
17+
{
18+
return $@"{t:%m} minutes";
19+
}
20+
if (t.TotalDays <= 1)
21+
{
22+
return $@"{t:%h} hours";
23+
}
24+
25+
return $@"{t:%d} days";
2926
}
3027
}

GlobalSuppressions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Style", "IDE0130:Namespace does not match folder structure", Justification = "", Scope = "namespace", Target = "~N:MetroStart.Weather.Respnoses")]

Helpers/ThemeHelpers.cs

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,92 @@
33
using MetroStart.Themes.Responses;
44
using Microsoft.Extensions.Logging;
55

6-
namespace MetroStart.Helpers
6+
namespace MetroStart.Helpers;
7+
8+
public static class ThemeHelpers
79
{
8-
public static class ThemeHelpers
10+
public static async Task<TableClient> GetCloudTable(ILogger log)
911
{
10-
public static async Task<TableClient> GetCloudTable(ILogger log)
12+
if (System.Environment.GetEnvironmentVariable("METROSTART_TABLE_CONNECTION_STRING", EnvironmentVariableTarget.Process) is var connectionString)
1113
{
12-
if (System.Environment.GetEnvironmentVariable("METROSTART_TABLE_CONNECTION_STRING", EnvironmentVariableTarget.Process) is var connectionString)
13-
{
14-
var table = new TableClient(connectionString, "weather");
14+
var table = new TableClient(connectionString, "themes");
1515

16-
// Create the table if it doesn't exist.
17-
await table.CreateIfNotExistsAsync();
16+
// Create the table if it doesn't exist.
17+
await table.CreateIfNotExistsAsync();
1818

19-
return table;
20-
}
21-
22-
throw new ApplicationException("Could not find connectionString.");
19+
return table;
2320
}
2421

25-
public static ThemeEntity CreateThemeEntity(SharedTheme theme, ILogger log)
26-
{
27-
return new ThemeEntity(
28-
theme.Author.Nullable() ?? throw new ArgumentNullException(nameof(theme.Author)),
29-
theme.Title.Nullable() ?? throw new ArgumentNullException(nameof(theme.Title)),
30-
theme.Online,
31-
theme.ThemeContent);
32-
}
22+
throw new ApplicationException("Could not find connectionString.");
23+
}
24+
25+
public static ThemeEntity CreateThemeEntity(SharedTheme theme, ILogger log) => new ThemeEntity(
26+
theme.Author,
27+
theme.Title,
28+
theme.Online,
29+
theme.ThemeContent);
3330

34-
public static ThemeEntity CreateThemeEntity(IDictionary<string, string> flatTheme, ILogger log)
31+
public static ThemeEntity CreateThemeEntity(IDictionary<string, string> flatTheme, ILogger log)
32+
{
33+
string author = string.Empty;
34+
string title = string.Empty;
35+
bool online = true;
36+
Dictionary<string, string> themeContent = new Dictionary<string, string>();
37+
foreach (var (Key, Value) in flatTheme)
3538
{
36-
string author = null;
37-
string title = null;
38-
bool online = true;
39-
Dictionary<string, string> themeContent = new Dictionary<string, string>();
40-
foreach (var (Key, Value) in flatTheme)
39+
log.LogInformation($"Adding Key={Key}, Value={Value}");
40+
if (Key.Equals("author", StringComparison.InvariantCultureIgnoreCase))
4141
{
42-
log.LogInformation($"Adding Key={Key}, Value={Value}");
43-
if (Key.Equals("author", StringComparison.InvariantCultureIgnoreCase))
44-
{
45-
author = Value;
46-
}
47-
else if (Key.Equals("title", StringComparison.InvariantCultureIgnoreCase))
48-
{
49-
title = Value;
50-
}
51-
else if (Key.Equals("online", StringComparison.InvariantCultureIgnoreCase))
52-
{
53-
if (!bool.TryParse(Value, out online))
54-
{
55-
online = true;
56-
}
57-
}
58-
else
42+
author = Value;
43+
}
44+
else if (Key.Equals("title", StringComparison.InvariantCultureIgnoreCase))
45+
{
46+
title = Value;
47+
}
48+
else if (Key.Equals("online", StringComparison.InvariantCultureIgnoreCase))
49+
{
50+
if (!bool.TryParse(Value, out online))
5951
{
60-
themeContent.Add(Key, Value);
52+
online = true;
6153
}
6254
}
63-
64-
return new ThemeEntity(
65-
author.Nullable() ?? throw new ArgumentNullException(nameof(author)),
66-
title.Nullable() ?? throw new ArgumentNullException(nameof(title)),
67-
online,
68-
themeContent);
55+
else
56+
{
57+
themeContent.Add(Key, Value);
58+
}
6959
}
7060

71-
public static async Task<ThemeEntity> InsertTheme(ThemeEntity themeEntity, TableClient table, ILogger log)
72-
{
73-
var response = table.AddEntity(themeEntity);
74-
return response.IsError ? throw new InvalidDataException($"Theme {themeEntity} was not saved") : themeEntity ;
75-
}
61+
return new ThemeEntity(author, title, online, themeContent);
62+
}
7663

77-
public static async Task<List<ThemeEntity>> GetAllThemes(ILogger log)
78-
{
79-
var table = await GetCloudTable(log);
80-
var results = new List<ThemeEntity>();
64+
public static async Task<ThemeEntity> InsertTheme(ThemeEntity themeEntity, TableClient table, ILogger log)
65+
{
66+
var response = table.AddEntity(themeEntity);
67+
return response.IsError ? throw new InvalidDataException($"Theme {themeEntity} was not saved") : themeEntity ;
68+
}
8169

82-
var queryResults = table.QueryAsync<ThemeEntity>();
83-
await foreach (var theme in queryResults.AsPages())
84-
{
85-
results.AddRange(theme.Values);
86-
}
70+
public static async Task<List<ThemeEntity>> GetAllThemes(ILogger log)
71+
{
72+
var table = await GetCloudTable(log);
73+
var results = new List<ThemeEntity>();
8774

88-
return results;
75+
var queryResults = table.QueryAsync<ThemeEntity>();
76+
await foreach (var theme in queryResults.AsPages())
77+
{
78+
results.AddRange(theme.Values);
8979
}
9080

91-
public static async Task<bool> ThemeExists(string title, ILogger log)
81+
return results;
82+
}
83+
84+
public static async Task<bool> ThemeExists(string title, ILogger log)
85+
{
86+
var table = await GetCloudTable(log);
87+
var results = table.QueryAsync((ThemeEntity t) => t.PartitionKey.Equals(title));
88+
await foreach (var theme in results)
9289
{
93-
var table = await GetCloudTable(log);
94-
var results = table.QueryAsync((ThemeEntity t) => t.PartitionKey.Equals(title));
95-
await foreach (var theme in results)
96-
{
97-
return true;
98-
}
99-
return false;
90+
return true;
10091
}
92+
return false;
10193
}
10294
}

0 commit comments

Comments
 (0)