Browse Source

Merge 226890fbce into 9486760a9e

pull/573/merge
Roma Marusyk 6 years ago
committed by GitHub
parent
commit
4ca37dfe5d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 97 deletions
  1. +4
    -4
      src/Services/Location/Locations.API/Controllers/LocationsController.cs
  2. +66
    -66
      src/Services/Location/Locations.API/Infrastructure/LocationsContextSeed.cs
  3. +16
    -16
      src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs
  4. +4
    -4
      src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs
  5. +7
    -7
      src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs

+ 4
- 4
src/Services/Location/Locations.API/Controllers/LocationsController.cs View File

@ -29,7 +29,7 @@ namespace Locations.API.Controllers
[ProducesResponseType(typeof(UserLocation), (int)HttpStatusCode.OK)] [ProducesResponseType(typeof(UserLocation), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserLocation(Guid userId) public async Task<IActionResult> GetUserLocation(Guid userId)
{ {
var userLocation = await _locationsService.GetUserLocation(userId.ToString());
var userLocation = await _locationsService.GetUserLocationAsync(userId.ToString());
return Ok(userLocation); return Ok(userLocation);
} }
@ -39,7 +39,7 @@ namespace Locations.API.Controllers
//[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)] //[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetAllLocations() public async Task<IActionResult> GetAllLocations()
{ {
var locations = await _locationsService.GetAllLocation();
var locations = await _locationsService.GetAllLocationAsync();
return Ok(locations); return Ok(locations);
} }
@ -49,7 +49,7 @@ namespace Locations.API.Controllers
//[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)] //[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetLocation(int locationId) public async Task<IActionResult> GetLocation(int locationId)
{ {
var location = await _locationsService.GetLocation(locationId);
var location = await _locationsService.GetLocationAsync(locationId);
return Ok(location); return Ok(location);
} }
@ -61,7 +61,7 @@ namespace Locations.API.Controllers
public async Task<IActionResult> CreateOrUpdateUserLocation([FromBody]LocationRequest newLocReq) public async Task<IActionResult> CreateOrUpdateUserLocation([FromBody]LocationRequest newLocReq)
{ {
var userId = _identityService.GetUserIdentity(); var userId = _identityService.GetUserIdentity();
var result = await _locationsService.AddOrUpdateUserLocation(userId, newLocReq);
var result = await _locationsService.AddOrUpdateUserLocationAsync(userId, newLocReq);
return result ? return result ?
(IActionResult)Ok() : (IActionResult)Ok() :


+ 66
- 66
src/Services/Location/Locations.API/Infrastructure/LocationsContextSeed.cs View File

@ -22,20 +22,20 @@
if (!ctx.Locations.Database.GetCollection<Locations>(nameof(Locations)).AsQueryable().Any()) if (!ctx.Locations.Database.GetCollection<Locations>(nameof(Locations)).AsQueryable().Any())
{ {
await SetIndexes();
await SetNorthAmerica();
await SetSouthAmerica();
await SetAfrica();
await SetEurope();
await SetAsia();
await SetAustralia();
await SetBarcelonaLocations();
await SetIndexesAsync();
await SetNorthAmericaAsync();
await SetSouthAmericaAsync();
await SetAfricaAsync();
await SetEuropeAsync();
await SetAsiaAsync();
await SetAustraliaAsync();
await SetBarcelonaLocationsAsync();
} }
} }
static async Task SetNorthAmerica()
private static async Task SetNorthAmericaAsync()
{ {
var us = new Locations()
var us = new Locations
{ {
Code = "NA", Code = "NA",
Description = "North America", Description = "North America",
@ -44,10 +44,10 @@
us.SetLocation(-103.219329, 48.803281); us.SetLocation(-103.219329, 48.803281);
us.SetArea(GetNorthAmericaPoligon()); us.SetArea(GetNorthAmericaPoligon());
await ctx.Locations.InsertOneAsync(us); await ctx.Locations.InsertOneAsync(us);
await SetUSLocations(us.Id);
await SetUSLocationsAsync(us.Id);
} }
static async Task SetUSLocations(string parentId)
private static async Task SetUSLocationsAsync(string parentId)
{ {
var us = new Locations() var us = new Locations()
{ {
@ -57,14 +57,14 @@
LocationId = 2 LocationId = 2
}; };
us.SetLocation(-101.357386, 41.650455); us.SetLocation(-101.357386, 41.650455);
us.SetArea(GetUSPoligon());
us.SetArea(GetUsPoligon());
await ctx.Locations.InsertOneAsync(us); await ctx.Locations.InsertOneAsync(us);
await SetWashingtonLocations(us.Id);
await SetWashingtonLocationsAsync(us.Id);
} }
static async Task SetWashingtonLocations(string parentId)
private static async Task SetWashingtonLocationsAsync(string parentId)
{ {
var wht = new Locations()
var wht = new Locations
{ {
Parent_Id = parentId, Parent_Id = parentId,
Code = "WHT", Code = "WHT",
@ -74,13 +74,13 @@
wht.SetLocation(-119.542781, 47.223652); wht.SetLocation(-119.542781, 47.223652);
wht.SetArea(GetWashingtonPoligon()); wht.SetArea(GetWashingtonPoligon());
await ctx.Locations.InsertOneAsync(wht); await ctx.Locations.InsertOneAsync(wht);
await SetSeattleLocations(wht.Id);
await SetRedmondLocations(wht.Id);
await SetSeattleLocationsAsync(wht.Id);
await SetRedmondLocationsAsync(wht.Id);
} }
static async Task SetSeattleLocations(string parentId)
private static Task SetSeattleLocationsAsync(string parentId)
{ {
var stl = new Locations()
var stl = new Locations
{ {
Parent_Id = parentId, Parent_Id = parentId,
Code = "SEAT", Code = "SEAT",
@ -89,12 +89,12 @@
}; };
stl.SetArea(GetSeattlePoligon()); stl.SetArea(GetSeattlePoligon());
stl.SetLocation(-122.330747, 47.603111); stl.SetLocation(-122.330747, 47.603111);
await ctx.Locations.InsertOneAsync(stl);
return ctx.Locations.InsertOneAsync(stl);
} }
static async Task SetRedmondLocations(string parentId)
private static Task SetRedmondLocationsAsync(string parentId)
{ {
var rdm = new Locations()
var rdm = new Locations
{ {
Parent_Id = parentId, Parent_Id = parentId,
Code = "REDM", Code = "REDM",
@ -103,12 +103,12 @@
}; };
rdm.SetLocation(-122.122887, 47.674961); rdm.SetLocation(-122.122887, 47.674961);
rdm.SetArea(GetRedmondPoligon()); rdm.SetArea(GetRedmondPoligon());
await ctx.Locations.InsertOneAsync(rdm);
return ctx.Locations.InsertOneAsync(rdm);
} }
static async Task SetBarcelonaLocations()
private static Task SetBarcelonaLocationsAsync()
{ {
var bcn = new Locations()
var bcn = new Locations
{ {
Code = "BCN", Code = "BCN",
Description = "Barcelona", Description = "Barcelona",
@ -116,12 +116,12 @@
}; };
bcn.SetLocation(2.156453, 41.395226); bcn.SetLocation(2.156453, 41.395226);
bcn.SetArea(GetBarcelonaPoligon()); bcn.SetArea(GetBarcelonaPoligon());
await ctx.Locations.InsertOneAsync(bcn);
return ctx.Locations.InsertOneAsync(bcn);
} }
static async Task SetSouthAmerica()
private static Task SetSouthAmericaAsync()
{ {
var sa = new Locations()
var sa = new Locations
{ {
Code = "SA", Code = "SA",
Description = "South America", Description = "South America",
@ -129,12 +129,12 @@
}; };
sa.SetLocation(-60.328704, -16.809748); sa.SetLocation(-60.328704, -16.809748);
sa.SetArea(GetSouthAmericaPoligon()); sa.SetArea(GetSouthAmericaPoligon());
await ctx.Locations.InsertOneAsync(sa);
return ctx.Locations.InsertOneAsync(sa);
} }
static async Task SetAfrica()
private static Task SetAfricaAsync()
{ {
var afc = new Locations()
var afc = new Locations
{ {
Code = "AFC", Code = "AFC",
Description = "Africa", Description = "Africa",
@ -142,12 +142,12 @@
}; };
afc.SetLocation(19.475383, 13.063667); afc.SetLocation(19.475383, 13.063667);
afc.SetArea(GetAfricaPoligon()); afc.SetArea(GetAfricaPoligon());
await ctx.Locations.InsertOneAsync(afc);
return ctx.Locations.InsertOneAsync(afc);
} }
static async Task SetEurope()
private static Task SetEuropeAsync()
{ {
var eu = new Locations()
var eu = new Locations
{ {
Code = "EU", Code = "EU",
Description = "Europe", Description = "Europe",
@ -155,12 +155,12 @@
}; };
eu.SetLocation(13.147258, 49.947844); eu.SetLocation(13.147258, 49.947844);
eu.SetArea(GetEuropePoligon()); eu.SetArea(GetEuropePoligon());
await ctx.Locations.InsertOneAsync(eu);
return ctx.Locations.InsertOneAsync(eu);
} }
static async Task SetAsia()
private static Task SetAsiaAsync()
{ {
var asa = new Locations()
var asa = new Locations
{ {
Code = "AS", Code = "AS",
Description = "Asia", Description = "Asia",
@ -168,12 +168,12 @@
}; };
asa.SetLocation(97.522257, 56.069107); asa.SetLocation(97.522257, 56.069107);
asa.SetArea(GetAsiaPoligon()); asa.SetArea(GetAsiaPoligon());
await ctx.Locations.InsertOneAsync(asa);
return ctx.Locations.InsertOneAsync(asa);
} }
static async Task SetAustralia()
private static Task SetAustraliaAsync()
{ {
var aus = new Locations()
var aus = new Locations
{ {
Code = "AUS", Code = "AUS",
Description = "Australia", Description = "Australia",
@ -181,20 +181,20 @@
}; };
aus.SetLocation(133.733195, -25.010726); aus.SetLocation(133.733195, -25.010726);
aus.SetArea(GetAustraliaPoligon()); aus.SetArea(GetAustraliaPoligon());
await ctx.Locations.InsertOneAsync(aus);
return ctx.Locations.InsertOneAsync(aus);
} }
static async Task SetIndexes()
private static Task SetIndexesAsync()
{ {
// Set location indexes // Set location indexes
var builder = Builders<Locations>.IndexKeys; var builder = Builders<Locations>.IndexKeys;
var keys = builder.Geo2DSphere(prop => prop.Location); var keys = builder.Geo2DSphere(prop => prop.Location);
await ctx.Locations.Indexes.CreateOneAsync(keys);
return ctx.Locations.Indexes.CreateOneAsync(keys);
} }
static List<GeoJson2DGeographicCoordinates> GetNorthAmericaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetNorthAmericaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-168.07786, 68.80277), new GeoJson2DGeographicCoordinates(-168.07786, 68.80277),
new GeoJson2DGeographicCoordinates(-119.60378, 32.7561), new GeoJson2DGeographicCoordinates(-119.60378, 32.7561),
@ -207,9 +207,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetSouthAmericaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetSouthAmericaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-91.43724, 13.29007), new GeoJson2DGeographicCoordinates(-91.43724, 13.29007),
new GeoJson2DGeographicCoordinates(-87.96315, -27.15081), new GeoJson2DGeographicCoordinates(-87.96315, -27.15081),
@ -223,9 +223,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetAfricaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetAfricaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-12.68724, 34.05892), new GeoJson2DGeographicCoordinates(-12.68724, 34.05892),
new GeoJson2DGeographicCoordinates(-18.33301, 20.77313), new GeoJson2DGeographicCoordinates(-18.33301, 20.77313),
@ -241,9 +241,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetEuropePoligon()
private static List<GeoJson2DGeographicCoordinates> GetEuropePoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-11.73143, 35.27646), new GeoJson2DGeographicCoordinates(-11.73143, 35.27646),
new GeoJson2DGeographicCoordinates(-10.84462, 35.25123), new GeoJson2DGeographicCoordinates(-10.84462, 35.25123),
@ -255,9 +255,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetAsiaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetAsiaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(31.1592, 45.91629), new GeoJson2DGeographicCoordinates(31.1592, 45.91629),
new GeoJson2DGeographicCoordinates(32.046, 45.89479), new GeoJson2DGeographicCoordinates(32.046, 45.89479),
@ -270,9 +270,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetAustraliaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetAustraliaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(100.76857, -45.74117), new GeoJson2DGeographicCoordinates(100.76857, -45.74117),
new GeoJson2DGeographicCoordinates(101.65538, -45.76273), new GeoJson2DGeographicCoordinates(101.65538, -45.76273),
@ -285,9 +285,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetUSPoligon()
static List<GeoJson2DGeographicCoordinates> GetUsPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-62.88205, 48.7985), new GeoJson2DGeographicCoordinates(-62.88205, 48.7985),
new GeoJson2DGeographicCoordinates(-129.3132, 48.76513), new GeoJson2DGeographicCoordinates(-129.3132, 48.76513),
@ -298,9 +298,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetSeattlePoligon()
private static List<GeoJson2DGeographicCoordinates> GetSeattlePoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-122.36238,47.82929), new GeoJson2DGeographicCoordinates(-122.36238,47.82929),
new GeoJson2DGeographicCoordinates(-122.42091,47.6337), new GeoJson2DGeographicCoordinates(-122.42091,47.6337),
@ -311,9 +311,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetRedmondPoligon()
private static List<GeoJson2DGeographicCoordinates> GetRedmondPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-122.15432, 47.73148), new GeoJson2DGeographicCoordinates(-122.15432, 47.73148),
new GeoJson2DGeographicCoordinates(-122.17673, 47.72559), new GeoJson2DGeographicCoordinates(-122.17673, 47.72559),
@ -326,9 +326,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetWashingtonPoligon()
private static List<GeoJson2DGeographicCoordinates> GetWashingtonPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(-124.68633, 48.8943), new GeoJson2DGeographicCoordinates(-124.68633, 48.8943),
new GeoJson2DGeographicCoordinates(-124.32962, 45.66613), new GeoJson2DGeographicCoordinates(-124.32962, 45.66613),
@ -338,9 +338,9 @@
}; };
} }
static List<GeoJson2DGeographicCoordinates> GetBarcelonaPoligon()
private static List<GeoJson2DGeographicCoordinates> GetBarcelonaPoligon()
{ {
return new List<GeoJson2DGeographicCoordinates>()
return new List<GeoJson2DGeographicCoordinates>
{ {
new GeoJson2DGeographicCoordinates(2.033879, 41.383858), new GeoJson2DGeographicCoordinates(2.033879, 41.383858),
new GeoJson2DGeographicCoordinates(2.113741, 41.419068), new GeoJson2DGeographicCoordinates(2.113741, 41.419068),


+ 16
- 16
src/Services/Location/Locations.API/Infrastructure/Repositories/LocationsRepository.cs View File

@ -19,44 +19,44 @@
_context = new LocationsContext(settings); _context = new LocationsContext(settings);
} }
public async Task<Locations> GetAsync(int locationId)
public Task<Locations> GetAsync(int locationId)
{ {
var filter = Builders<Locations>.Filter.Eq("LocationId", locationId); var filter = Builders<Locations>.Filter.Eq("LocationId", locationId);
return await _context.Locations
.Find(filter)
.FirstOrDefaultAsync();
return _context.Locations
.Find(filter)
.FirstOrDefaultAsync();
} }
public async Task<UserLocation> GetUserLocationAsync(string userId)
public Task<UserLocation> GetUserLocationAsync(string userId)
{ {
var filter = Builders<UserLocation>.Filter.Eq("UserId", userId); var filter = Builders<UserLocation>.Filter.Eq("UserId", userId);
return await _context.UserLocation
.Find(filter)
.FirstOrDefaultAsync();
return _context.UserLocation
.Find(filter)
.FirstOrDefaultAsync();
} }
public async Task<List<Locations>> GetLocationListAsync()
public Task<List<Locations>> GetLocationListAsync()
{ {
return await _context.Locations.Find(new BsonDocument()).ToListAsync();
return _context.Locations.Find(new BsonDocument()).ToListAsync();
} }
public async Task<List<Locations>> GetCurrentUserRegionsListAsync(LocationRequest currentPosition)
public Task<List<Locations>> GetCurrentUserRegionsListAsync(LocationRequest currentPosition)
{ {
var point = GeoJson.Point(GeoJson.Geographic(currentPosition.Longitude, currentPosition.Latitude)); var point = GeoJson.Point(GeoJson.Geographic(currentPosition.Longitude, currentPosition.Latitude));
var orderByDistanceQuery = new FilterDefinitionBuilder<Locations>().Near(x => x.Location, point); var orderByDistanceQuery = new FilterDefinitionBuilder<Locations>().Near(x => x.Location, point);
var withinAreaQuery = new FilterDefinitionBuilder<Locations>().GeoIntersects("Polygon", point); var withinAreaQuery = new FilterDefinitionBuilder<Locations>().GeoIntersects("Polygon", point);
var filter = Builders<Locations>.Filter.And(orderByDistanceQuery, withinAreaQuery); var filter = Builders<Locations>.Filter.And(orderByDistanceQuery, withinAreaQuery);
return await _context.Locations.Find(filter).ToListAsync();
return _context.Locations.Find(filter).ToListAsync();
} }
public async Task AddUserLocationAsync(UserLocation location)
public Task AddUserLocationAsync(UserLocation location)
{ {
await _context.UserLocation.InsertOneAsync(location);
return _context.UserLocation.InsertOneAsync(location);
} }
public async Task UpdateUserLocationAsync(UserLocation userLocation)
public Task UpdateUserLocationAsync(UserLocation userLocation)
{ {
await _context.UserLocation.ReplaceOneAsync(
return _context.UserLocation.ReplaceOneAsync(
doc => doc.UserId == userLocation.UserId, doc => doc.UserId == userLocation.UserId,
userLocation, userLocation,
new UpdateOptions { IsUpsert = true }); new UpdateOptions { IsUpsert = true });


+ 4
- 4
src/Services/Location/Locations.API/Infrastructure/Services/ILocationsService.cs View File

@ -7,12 +7,12 @@
public interface ILocationsService public interface ILocationsService
{ {
Task<Locations> GetLocation(int locationId);
Task<Locations> GetLocationAsync(int locationId);
Task<UserLocation> GetUserLocation(string id);
Task<UserLocation> GetUserLocationAsync(string id);
Task<List<Locations>> GetAllLocation();
Task<List<Locations>> GetAllLocationAsync();
Task<bool> AddOrUpdateUserLocation(string userId, LocationRequest locRequest);
Task<bool> AddOrUpdateUserLocationAsync(string userId, LocationRequest locRequest);
} }
} }

+ 7
- 7
src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs View File

@ -21,22 +21,22 @@
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
} }
public async Task<Locations> GetLocation(int locationId)
public Task<Locations> GetLocationAsync(int locationId)
{ {
return await _locationsRepository.GetAsync(locationId);
return _locationsRepository.GetAsync(locationId);
} }
public async Task<UserLocation> GetUserLocation(string userId)
public Task<UserLocation> GetUserLocationAsync(string userId)
{ {
return await _locationsRepository.GetUserLocationAsync(userId);
return _locationsRepository.GetUserLocationAsync(userId);
} }
public async Task<List<Locations>> GetAllLocation()
public Task<List<Locations>> GetAllLocationAsync()
{ {
return await _locationsRepository.GetLocationListAsync();
return _locationsRepository.GetLocationListAsync();
} }
public async Task<bool> AddOrUpdateUserLocation(string userId, LocationRequest currentPosition)
public async Task<bool> AddOrUpdateUserLocationAsync(string userId, LocationRequest currentPosition)
{ {
// Get the list of ordered regions the user currently is within // Get the list of ordered regions the user currently is within
var currentUserAreaLocationList = await _locationsRepository.GetCurrentUserRegionsListAsync(currentPosition); var currentUserAreaLocationList = await _locationsRepository.GetCurrentUserRegionsListAsync(currentPosition);


Loading…
Cancel
Save