1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Net . Http ;
5+ using System . Net . Http . Json ;
6+ using System . Threading . Tasks ;
7+ using FetchDataFunctions . Models ;
8+ using FetchDataFunctions . Models . HuettenHoliday ;
9+ using Microsoft . AspNetCore . Http ;
10+ using Microsoft . AspNetCore . Mvc ;
11+ using Microsoft . Azure . Functions . Worker ;
12+ using Microsoft . EntityFrameworkCore ;
13+ using Microsoft . Extensions . Logging ;
14+
15+ namespace FetchDataFunctions . Functions . HuettenHoliday ;
16+
17+ public class HuettenHolidayGetHutFromProvider
18+ {
19+ private readonly ILogger < HuettenHolidayGetHutFromProvider > _logger ;
20+ private readonly IHttpClientFactory _clientFactory ;
21+
22+ private const int HutIdOffset = 10000 ; // Offset to avoid conflicts with other hut IDs
23+
24+ public HuettenHolidayGetHutFromProvider ( ILogger < HuettenHolidayGetHutFromProvider > logger , IHttpClientFactory clientFactory )
25+ {
26+ _logger = logger ;
27+ _clientFactory = clientFactory ;
28+ }
29+
30+ [ Function ( nameof ( HuettenHolidayUpdateHutHttpTriggered ) ) ]
31+ public async Task < IActionResult > HuettenHolidayUpdateHutHttpTriggered ( [ HttpTrigger ( AuthorizationLevel . Function , "get" ) ] HttpRequest req , string hutId )
32+ {
33+ _logger . LogInformation ( "HuettenHolidayUpdateHutHttpTriggered called with hutIds: {HutId}" , hutId ) ;
34+
35+ var hutIdsList = hutId . Split ( ',' ) . Select ( i => int . Parse ( i ) + HutIdOffset ) . ToList ( ) ;
36+
37+ // Get all huts from the provider, then filter by hutId
38+ var allHuts = await HuettenHolidayGetHutsFromProvider ( null ) ;
39+ var huts = allHuts ? . Where ( h => hutIdsList . Contains ( h . Id ) ) . ToList ( ) ;
40+ if ( huts == null )
41+ {
42+ return new NotFoundObjectResult ( "No huts found." ) ;
43+ }
44+
45+ return new OkObjectResult ( huts ) ;
46+ }
47+
48+ [ Function ( nameof ( HuettenHolidayGetHutFromProvider ) ) ]
49+ public async Task < IEnumerable < Hut > ? > HuettenHolidayGetHutsFromProvider ( [ ActivityTrigger ] string ? input )
50+ {
51+ try
52+ {
53+ var dbContext = Helpers . GetDbContext ( ) ;
54+
55+ var httpClient = _clientFactory . CreateClient ( "HttpClient" ) ;
56+ var url = "https://www.huetten-holiday.com/get-cabins?page=1" ;
57+ var huts = new List < Hut > ( ) ;
58+ do
59+ {
60+ _logger . LogInformation ( "Fetching huts from HuettenHoliday: {Url}" , url ) ;
61+ var response = await httpClient . GetFromJsonAsync < GetCabinsResult > ( url ) ;
62+
63+ if ( response == null )
64+ {
65+ _logger . LogWarning ( "No huts found on page" ) ;
66+ break ;
67+ }
68+
69+ foreach ( var cabin in response . data )
70+ {
71+ if ( cabin . is_delete )
72+ {
73+ _logger . LogInformation ( "Skipping deleted cabin with ID {CabinId}" , cabin . id ) ;
74+ continue ;
75+ }
76+
77+ cabin . id += HutIdOffset ;
78+
79+
80+ if ( cabin . website != null )
81+ {
82+ if ( ! cabin . website . StartsWith ( "http://" ) && ! cabin . website . StartsWith ( "https://" ) )
83+ {
84+ cabin . website = "https://" + cabin . website ;
85+ }
86+ }
87+
88+ var hut = await dbContext . Huts . SingleOrDefaultAsync ( h => h . Id == cabin . id ) ;
89+
90+ if ( hut == null )
91+ {
92+ _logger . LogInformation ( "Creating new hut object for cabin with ID {CabinId}" , cabin . id ) ;
93+
94+ hut = new Hut
95+ {
96+ Id = cabin . id ,
97+ Source = "HuettenHoliday" ,
98+ Name = cabin . name ,
99+ HutWebsite = cabin . website ,
100+ Link = $ "https://www.huetten-holiday.com/huts/{ cabin . slug } ",
101+ Latitude = cabin . latitude ,
102+ Longitude = cabin . longitude ,
103+ Altitude = ( int ? ) cabin . altitude ,
104+ Country = cabin . country . name . de ,
105+ Enabled = true ,
106+ Added = DateTime . UtcNow ,
107+ LastUpdated = DateTime . UtcNow ,
108+ } ;
109+ dbContext . Add ( hut ) ;
110+ }
111+ else
112+ {
113+ if ( hut . ManuallyEdited == true )
114+ {
115+ _logger . LogInformation ( "Hut with ID {HutId} was manually edited. Not updating hut information" , hut . Id ) ;
116+ continue ; // Skip updating manually edited huts
117+ }
118+
119+ // Update existing hut
120+ _logger . LogInformation ( "Updating existing hut with ID {HutId}" , hut . Id ) ;
121+ hut . Name = cabin . name ;
122+ hut . HutWebsite = cabin . website ;
123+ hut . Link = $ "https://www.huetten-holiday.com/huts/{ cabin . slug } ";
124+ hut . Latitude = cabin . latitude ;
125+ hut . Longitude = cabin . longitude ;
126+ hut . Altitude = ( int ? ) cabin . altitude ;
127+ hut . Country = cabin . country . name . de ;
128+ hut . Enabled = true ;
129+ hut . LastUpdated = DateTime . UtcNow ;
130+ }
131+
132+ huts . Add ( hut ) ;
133+ }
134+
135+ await dbContext . SaveChangesAsync ( ) ;
136+
137+ url = response . next_page_url ?? null ;
138+ } while ( url != null ) ;
139+
140+ _logger . LogInformation ( "Fetched {HutCount} huts from HuettenHoliday" , huts . Count ) ;
141+ return huts ;
142+ }
143+ catch ( Exception e )
144+ {
145+ _logger . LogError ( e , "Error while fetching huts from HuettenHoliday" ) ;
146+ return null ;
147+ }
148+ }
149+ }
0 commit comments