33using MetroStart . Themes . Responses ;
44using 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