-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventDataService.cs
More file actions
79 lines (69 loc) · 3.22 KB
/
EventDataService.cs
File metadata and controls
79 lines (69 loc) · 3.22 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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using PocketDDD.Server.DB;
using PocketDDD.Shared.API.RequestDTOs;
using PocketDDD.Shared.API.ResponseDTOs;
namespace PocketDDD.Server.Services;
public class EventDataService(PocketDDDContext dbContext, IMemoryCache memoryCache, ILogger<EventDataService> logger)
{
private const string FetchLatestEventDataCacheKey = nameof(FetchLatestEventData);
private const string FetchCurrentEventDetailIdCacheKey = nameof(FetchCurrentEventDetailId);
public async Task<int> FetchCurrentEventDetailId()
{
if (memoryCache.TryGetValue<int?>(FetchCurrentEventDetailIdCacheKey, out var cachedEventDetailId))
return cachedEventDetailId!.Value;
cachedEventDetailId = await dbContext.EventDetail.MaxAsync(e => e.Id);
memoryCache.Set(FetchCurrentEventDetailIdCacheKey, cachedEventDetailId, TimeSpan.FromMinutes(5));
return cachedEventDetailId.Value;
}
public async Task<EventDataResponseDTO?> FetchLatestEventData(EventDataUpdateRequestDTO requestDto)
{
var currentEventDetailId = await FetchCurrentEventDetailId();
if (memoryCache.TryGetValue<EventDataResponseDTO?>(FetchLatestEventDataCacheKey, out var latestEventData))
{
logger.LogDebug("Retrieved latest event data from the cache {eventData}", latestEventData);
}
else
{
logger.LogDebug("No event data in the cache, retrieving from the db");
latestEventData = await dbContext.EventDetail
.AsNoTracking()
.AsSingleQuery()
.Select(eventDetails => new EventDataResponseDTO
{
Id = eventDetails.Id,
Version = eventDetails.Version,
TimeSlots = eventDetails.TimeSlots.Select(ts => new TimeSlotDTO
{
Id = ts.Id,
Info = ts.Info,
From = ts.From,
To = ts.To
}).ToList(),
Tracks = eventDetails.Tracks.Select(t => new TrackDTO
{
Id = t.Id,
Name = t.Name,
RoomName = t.RoomName
}).ToList(),
Sessions = eventDetails.Sessions.Select(s => new SessionDTO
{
Id = s.Id,
Title = s.Title,
ShortDescription = s.ShortDescription,
FullDescription = s.FullDescription,
Speaker = s.Speaker,
TimeSlotId = s.TimeSlot.Id,
TrackId = s.Track.Id
}).ToList()
})
.SingleAsync(e => e.Id == currentEventDetailId);
memoryCache.Set(FetchLatestEventDataCacheKey, latestEventData, TimeSpan.FromMinutes(5));
logger.LogDebug("Updated the latest event data in the cache {eventData}", latestEventData);
}
if (requestDto.Version >= latestEventData!.Version)
return null;
return latestEventData;
}
}