Skip to content

Commit dd991a4

Browse files
committed
Add logging to sessionize service update
1 parent 68b15b1 commit dd991a4

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

PocketDDD.Server/PocketDDD.Server.Services/SessionizeService.cs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using System.Net.Http.Json;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Logging;
24
using PocketDDD.Server.DB;
35
using PocketDDD.Server.Model.DBModel;
46
using PocketDDD.Server.Model.Sessionize;
5-
using System.Net.Http.Json;
7+
using Session = PocketDDD.Server.Model.DBModel.Session;
68

79
namespace PocketDDD.Server.Services;
810

911
public class SessionizeService
1012
{
11-
private readonly HttpClient httpClient;
1213
private readonly PocketDDDContext dbContext;
14+
private readonly HttpClient httpClient;
1315

14-
public SessionizeService(HttpClient httpClient, PocketDDDContext dbContext)
16+
public SessionizeService(HttpClient httpClient, PocketDDDContext dbContext, ILogger<SessionizeService> logger)
1517
{
18+
Logger = logger;
1619
this.httpClient = httpClient;
1720
this.dbContext = dbContext;
1821
httpClient.BaseAddress = new Uri("https://sessionize.com/api/v2/");
1922
}
2023

24+
private ILogger<SessionizeService> Logger { get; }
25+
2126
public async Task UpdateFromSessionize()
2227
{
23-
var dbEvent = await dbContext.EventDetail.SingleAsync(x => x.Id == 1);
28+
Logger.LogInformation("Looking for event detail in database");
2429

30+
var dbEvent = await dbContext.EventDetail.SingleAsync(x => x.Id == 1);
2531
var sessionizeEventId = dbEvent.SessionizeId;
32+
33+
Logger.LogInformation("About to get data from Sessionize API");
34+
2635
var sessionizeEvent = await httpClient.GetFromJsonAsync<SessionizeEvent>($"{sessionizeEventId}/view/All");
2736

2837
if (sessionizeEvent is null)
2938
throw new ArgumentNullException(nameof(sessionizeEvent));
3039

40+
Logger.LogInformation("Information retrieved from Sessionize API");
41+
Logger.LogInformation("Looking for changes to rooms");
42+
3143
var dbTracks = await dbContext.Tracks.ToListAsync();
3244
foreach (var item in sessionizeEvent.rooms)
3345
{
@@ -46,8 +58,17 @@ public async Task UpdateFromSessionize()
4658
dbTrack.Name = $"Track {item.sort}";
4759
}
4860

49-
await dbContext.SaveChangesAsync();
50-
61+
if (dbContext.ChangeTracker.HasChanges())
62+
{
63+
Logger.LogInformation("Updating db with changes to rooms");
64+
await dbContext.SaveChangesAsync();
65+
}
66+
else
67+
{
68+
Logger.LogInformation("No changes to rooms were detected");
69+
}
70+
71+
Logger.LogInformation("Looking for changes to time slots and breaks");
5172

5273
var dbTimeSlots = await dbContext.TimeSlots.ToListAsync();
5374
var sessionizeTimeSlots = sessionizeEvent.sessions
@@ -71,10 +92,18 @@ public async Task UpdateFromSessionize()
7192
dbContext.TimeSlots.Add(dbTimeSlot);
7293
}
7394
}
95+
if (dbContext.ChangeTracker.HasChanges())
96+
{
97+
Logger.LogInformation("Updating db with changes to time slots and breaks");
98+
await dbContext.SaveChangesAsync();
99+
}
100+
else
101+
{
102+
Logger.LogInformation("No changes to time slots or breaks were detected");
103+
}
74104

75-
await dbContext.SaveChangesAsync();
76-
77-
105+
Logger.LogInformation("Looking for changes to sessions");
106+
78107
var dbSessions = await dbContext.Sessions.ToListAsync();
79108
var speakers = sessionizeEvent.speakers;
80109
dbTracks = await dbContext.Tracks.ToListAsync();
@@ -89,7 +118,7 @@ public async Task UpdateFromSessionize()
89118
var dbSession = dbSessions.SingleOrDefault(x => x.SessionizeId == sessionizeId);
90119
if (dbSession == null)
91120
{
92-
dbSession = new Model.DBModel.Session
121+
dbSession = new Session
93122
{
94123
SessionizeId = sessionizeId,
95124
EventDetail = dbEvent,
@@ -105,8 +134,15 @@ public async Task UpdateFromSessionize()
105134
dbSession.Track = dbTracks.Single(x => x.SessionizeId == item.roomId);
106135
dbSession.TimeSlot = GetTimeSlot(dbTimeSlots, item.startsAt, item.endsAt);
107136
}
108-
109-
await dbContext.SaveChangesAsync();
137+
if (dbContext.ChangeTracker.HasChanges())
138+
{
139+
Logger.LogInformation("Updating db with changes to sessions");
140+
await dbContext.SaveChangesAsync();
141+
}
142+
else
143+
{
144+
Logger.LogInformation("No changes to sessions were detected");
145+
}
110146
}
111147

112148
private string GetSpeakers(List<Speaker> speakers, List<string> speakerIds)

0 commit comments

Comments
 (0)