Skip to content

Commit 05bd964

Browse files
committed
Adapt Locations.api to nosql db
1 parent d657428 commit 05bd964

26 files changed

Lines changed: 414 additions & 781 deletions

docker-compose.override.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ services:
8383
ports:
8484
- "5433:1433"
8585

86+
nosql.data:
87+
ports:
88+
- "27017:27017"
89+
8690
webstatus:
8791
environment:
8892
- ASPNETCORE_ENVIRONMENT=Development
@@ -100,7 +104,8 @@ services:
100104
environment:
101105
- ASPNETCORE_ENVIRONMENT=Development
102106
- ASPNETCORE_URLS=http://0.0.0.0:80
103-
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.LocationsDb;User Id=sa;Password=Pass@word
107+
- ConnectionString=mongodb://nosql.data
108+
- Database=LocationsDb
104109
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
105110
- EventBusConnection=rabbitmq
106111
ports:

docker-compose.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ services:
5959
sql.data:
6060
image: microsoft/mssql-server-linux
6161

62+
nosql.data:
63+
image: mongo
64+
6265
basket.data:
6366
image: redis
6467
ports:
@@ -81,4 +84,4 @@ services:
8184
context: ./src/Services/Location/Locations.API
8285
dockerfile: Dockerfile
8386
depends_on:
84-
- sql.data
87+
- nosql.data

src/Services/Location/Locations.API/Controllers/LocationsController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ public LocationsController(ILocationsService locationsService, IIdentityService
2020
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
2121
}
2222

23+
//GET api/v1/[controller]/1
24+
[Route("{userId:int}")]
25+
[HttpGet]
26+
public async Task<IActionResult> GetUserLocation(int userId)
27+
{
28+
var userLocation = await _locationsService.GetUserLocation(userId);
29+
return Ok(userLocation);
30+
}
31+
32+
//GET api/v1/[controller]/locations
33+
[Route("locations")]
34+
[HttpGet]
35+
public async Task<IActionResult> GetAllLocations()
36+
{
37+
var userLocation = await _locationsService.GetAllLocation();
38+
return Ok(userLocation);
39+
}
40+
2341
//POST api/v1/[controller]/
2442
[Route("")]
2543
[HttpPost]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.ActionResults
5+
{
6+
public class InternalServerErrorObjectResult : ObjectResult
7+
{
8+
public InternalServerErrorObjectResult(object error)
9+
: base(error)
10+
{
11+
StatusCode = StatusCodes.Status500InternalServerError;
12+
}
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Exception type for app exceptions
7+
/// </summary>
8+
public class LocationDomainException : Exception
9+
{
10+
public LocationDomainException()
11+
{ }
12+
13+
public LocationDomainException(string message)
14+
: base(message)
15+
{ }
16+
17+
public LocationDomainException(string message, Exception innerException)
18+
: base(message, innerException)
19+
{ }
20+
}
21+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters
2+
{
3+
using AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Mvc.Filters;
6+
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.ActionResults;
7+
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions;
8+
using Microsoft.Extensions.Logging;
9+
using System.Net;
10+
11+
public class HttpGlobalExceptionFilter : IExceptionFilter
12+
{
13+
private readonly IHostingEnvironment env;
14+
private readonly ILogger<HttpGlobalExceptionFilter> logger;
15+
16+
public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger<HttpGlobalExceptionFilter> logger)
17+
{
18+
this.env = env;
19+
this.logger = logger;
20+
}
21+
22+
public void OnException(ExceptionContext context)
23+
{
24+
logger.LogError(new EventId(context.Exception.HResult),
25+
context.Exception,
26+
context.Exception.Message);
27+
28+
if (context.Exception.GetType() == typeof(LocationDomainException))
29+
{
30+
var json = new JsonErrorResponse
31+
{
32+
Messages = new[] { context.Exception.Message }
33+
};
34+
35+
// Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
36+
//It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
37+
context.Result = new BadRequestObjectResult(json);
38+
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
39+
}
40+
else
41+
{
42+
var json = new JsonErrorResponse
43+
{
44+
Messages = new[] { "An error occur.Try it again." }
45+
};
46+
47+
if (env.IsDevelopment())
48+
{
49+
json.DeveloperMessage = context.Exception;
50+
}
51+
52+
// Result asigned to a result object but in destiny the response is empty. This is a known bug of .net core 1.1
53+
// It will be fixed in .net core 1.1.2. See https://github.com/aspnet/Mvc/issues/5594 for more information
54+
context.Result = new InternalServerErrorObjectResult(json);
55+
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
56+
}
57+
context.ExceptionHandled = true;
58+
}
59+
60+
private class JsonErrorResponse
61+
{
62+
public string[] Messages { get; set; }
63+
64+
public object DeveloperMessage { get; set; }
65+
}
66+
}
67+
}
Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,34 @@
11
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure
22
{
3-
using Microsoft.EntityFrameworkCore;
4-
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5-
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories;
63
using Microsoft.eShopOnContainers.Services.Locations.API.Model;
7-
using System.Threading;
8-
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Options;
5+
using MongoDB.Driver;
96

10-
public class LocationsContext : DbContext, IUnitOfWork
7+
public class LocationsContext
118
{
12-
public LocationsContext(DbContextOptions options) : base(options)
13-
{
14-
}
9+
private readonly IMongoDatabase _database = null;
1510

16-
public DbSet<Locations> Locations { get; set; }
17-
public DbSet<FrontierPoints> FrontierPoints { get; set; }
18-
public DbSet<UserLocation> UserLocation { get; set; }
19-
20-
protected override void OnModelCreating(ModelBuilder builder)
11+
public LocationsContext(IOptions<LocationSettings> settings)
2112
{
22-
builder.Entity<Locations>(ConfigureLocations);
23-
builder.Entity<FrontierPoints>(ConfigureFrontierPoints);
24-
builder.Entity<UserLocation>(ConfigureUserLocation);
13+
var client = new MongoClient(settings.Value.ConnectionString);
14+
if (client != null)
15+
_database = client.GetDatabase(settings.Value.Database);
2516
}
2617

27-
void ConfigureLocations(EntityTypeBuilder<Locations> builder)
18+
public IMongoCollection<UserLocation> UserLocation
2819
{
29-
builder.ToTable("Locations");
30-
31-
builder.HasKey(cl => cl.Id);
32-
33-
builder.Property(cl => cl.Id)
34-
.ForSqlServerUseSequenceHiLo("locations_seq")
35-
.IsRequired();
36-
37-
builder.Property(cb => cb.Code)
38-
.IsRequired()
39-
.HasColumnName("LocationCode")
40-
.HasMaxLength(15);
41-
42-
builder.HasMany(f => f.Polygon)
43-
.WithOne(l => l.Location)
44-
.IsRequired();
45-
46-
builder.Property(cb => cb.Description)
47-
.HasMaxLength(100);
20+
get
21+
{
22+
return _database.GetCollection<UserLocation>("UserLocation");
23+
}
4824
}
4925

50-
void ConfigureFrontierPoints(EntityTypeBuilder<FrontierPoints> builder)
26+
public IMongoCollection<Locations> Locations
5127
{
52-
builder.ToTable("FrontierPoints");
53-
54-
builder.HasKey(fp => fp.Id);
55-
56-
builder.Property(fp => fp.Id)
57-
.ForSqlServerUseSequenceHiLo("frontier_seq")
58-
.IsRequired();
59-
}
60-
61-
void ConfigureUserLocation(EntityTypeBuilder<UserLocation> builder)
62-
{
63-
builder.ToTable("UserLocation");
64-
65-
builder.Property(ul => ul.Id)
66-
.ForSqlServerUseSequenceHiLo("UserLocation_seq")
67-
.IsRequired();
68-
69-
builder.HasIndex(ul => ul.UserId).IsUnique();
70-
}
28+
get
29+
{
30+
return _database.GetCollection<Locations>("Locations");
31+
}
32+
}
7133
}
7234
}

0 commit comments

Comments
 (0)