88 using System . Linq ;
99 using Microsoft . eShopOnContainers . Services . Locations . API . Infrastructure . Exceptions ;
1010 using System . Collections . Generic ;
11+ using Microsoft . eShopOnContainers . BuildingBlocks . EventBus . Abstractions ;
12+ using Microsoft . eShopOnContainers . Services . Locations . API . IntegrationEvents . Events ;
1113
1214 public class LocationsService : ILocationsService
1315 {
14- private ILocationsRepository _locationsRepository ;
16+ private readonly ILocationsRepository _locationsRepository ;
17+ private readonly IEventBus _eventBus ;
1518
16- public LocationsService ( ILocationsRepository locationsRepository )
19+ public LocationsService ( ILocationsRepository locationsRepository , IEventBus eventBus )
1720 {
1821 _locationsRepository = locationsRepository ?? throw new ArgumentNullException ( nameof ( locationsRepository ) ) ;
22+ _eventBus = eventBus ?? throw new ArgumentNullException ( nameof ( eventBus ) ) ;
1923 }
2024
2125 public async Task < Locations > GetLocation ( string locationId )
2226 {
2327 return await _locationsRepository . GetAsync ( locationId ) ;
2428 }
2529
26- public async Task < UserLocation > GetUserLocation ( string id )
30+ public async Task < UserLocation > GetUserLocation ( string userId )
2731 {
28- if ( ! Guid . TryParse ( id , out Guid userId ) )
29- {
30- throw new ArgumentException ( "Not valid userId" ) ;
31- }
32-
33- return await _locationsRepository . GetUserLocationAsync ( userId . ToString ( ) ) ;
32+ return await _locationsRepository . GetUserLocationAsync ( id ) ;
3433 }
3534
3635 public async Task < List < Locations > > GetAllLocation ( )
3736 {
3837 return await _locationsRepository . GetLocationListAsync ( ) ;
3938 }
4039
41- public async Task < bool > AddOrUpdateUserLocation ( string id , LocationRequest currentPosition )
42- {
43- if ( ! Guid . TryParse ( id , out Guid userId ) )
44- {
45- throw new ArgumentException ( "Not valid userId" ) ;
46- }
47-
40+ public async Task < bool > AddOrUpdateUserLocation ( string userId , LocationRequest currentPosition )
41+ {
4842 // Get the list of ordered regions the user currently is within
4943 var currentUserAreaLocationList = await _locationsRepository . GetCurrentUserRegionsListAsync ( currentPosition ) ;
5044
@@ -55,14 +49,40 @@ public async Task<bool> AddOrUpdateUserLocation(string id, LocationRequest curre
5549
5650 // If current area found, then update user location
5751 var locationAncestors = new List < string > ( ) ;
58- var userLocation = await _locationsRepository . GetUserLocationAsync ( userId . ToString ( ) ) ;
52+ var userLocation = await _locationsRepository . GetUserLocationAsync ( userId ) ;
5953 userLocation = userLocation ?? new UserLocation ( ) ;
6054 userLocation . UserId = userId ;
6155 userLocation . LocationId = currentUserAreaLocationList [ 0 ] . Id ;
6256 userLocation . UpdateDate = DateTime . UtcNow ;
6357 await _locationsRepository . UpdateUserLocationAsync ( userLocation ) ;
6458
59+ // Publish integration event to update marketing read data model
60+ // with the new locations updated
61+ PublishNewUserLocationPositionIntegrationEvent ( userId , currentUserAreaLocationList ) ;
62+
6563 return true ;
6664 }
65+
66+ private void PublishNewUserLocationPositionIntegrationEvent ( string userId , List < Locations > newLocations )
67+ {
68+ var newUserLocations = MapUserLocationDetails ( newLocations ) ;
69+ var @event = new UserLocationUpdatedIntegrationEvent ( userId , newUserLocations ) ;
70+ _eventBus . Publish ( @event ) ;
71+ }
72+
73+ private List < UserLocationDetails > MapUserLocationDetails ( List < Locations > newLocations )
74+ {
75+ var result = new List < UserLocationDetails > ( ) ;
76+ newLocations . ForEach ( location => {
77+ result . Add ( new UserLocationDetails ( )
78+ {
79+ LocationId = location . Id ,
80+ Code = location . Code ,
81+ Description = location . Description
82+ } ) ;
83+ } ) ;
84+
85+ return result ;
86+ }
6787 }
6888}
0 commit comments