From 339a9e169c6150d4bc60e0ab6a2e55b671bcc39a Mon Sep 17 00:00:00 2001 From: Francesco Date: Sun, 5 Nov 2023 20:42:24 +0100 Subject: [PATCH] refactor srv_qry_mongo --- .../Controllers/ContactsController.cs | 71 ---------------- .../.gitignore | 0 .../ContactsController.csproj | 30 +++---- .../srv_qry_mongo.Api/Controllers/Contact.cs | 24 ++++++ .../Controllers/ContactsController.cs | 27 ++++++ .../Controllers/MongoDBService.cs | 28 +++++++ .../Program.cs | 62 +++++++------- .../Properties/launchSettings.json | 82 +++++++++---------- .../appsettings.Development.json | 22 ++--- .../appsettings.json | 24 +++--- .../mongoDBConnect.cs | 0 .../srv_qry_mongo.sln | 2 +- 12 files changed, 190 insertions(+), 182 deletions(-) delete mode 100644 srv_qry_mongo/ContactsController/Controllers/ContactsController.cs rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/.gitignore (100%) rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/ContactsController.csproj (96%) create mode 100644 srv_qry_mongo/srv_qry_mongo.Api/Controllers/Contact.cs create mode 100644 srv_qry_mongo/srv_qry_mongo.Api/Controllers/ContactsController.cs create mode 100644 srv_qry_mongo/srv_qry_mongo.Api/Controllers/MongoDBService.cs rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/Program.cs (86%) rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/Properties/launchSettings.json (96%) rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/appsettings.Development.json (94%) rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/appsettings.json (94%) rename srv_qry_mongo/{ContactsController => srv_qry_mongo.Api}/mongoDBConnect.cs (100%) rename phone-book-boost.sln => srv_qry_mongo/srv_qry_mongo.sln (87%) diff --git a/srv_qry_mongo/ContactsController/Controllers/ContactsController.cs b/srv_qry_mongo/ContactsController/Controllers/ContactsController.cs deleted file mode 100644 index f85d7bf..0000000 --- a/srv_qry_mongo/ContactsController/Controllers/ContactsController.cs +++ /dev/null @@ -1,71 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using MongoDB.Driver; -using System; -using System.Collections.Generic; -using MongoDB.Bson; -using MongoDB.Bson.Serialization.Attributes; -using Microsoft.Extensions.Options; - -namespace ContactsController.Controllers -{ - [Route("api/contacts")] - [ApiController] - public class ContactsController : ControllerBase - { - private readonly MongoDBService _mongoDBService; - public ContactsController(MongoDBService mongoDBService) - { - _mongoDBService = mongoDBService; - } - - [HttpGet] - public IActionResult GetMongoContacts() - { - var contacts_data = _mongoDBService.GetAllContacts(); - return Ok(contacts_data); - } - } - - public class MongoDBService - { - private readonly IMongoCollection _collection; - - public MongoDBService(IOptions configuration) - { - string connectionString = configuration.Value.MongoDBConnection; - - var client = new MongoClient(connectionString); - - var database = client.GetDatabase("meteor"); - _collection = database.GetCollection("contacts"); - } - - public IEnumerable GetAllContacts() - { - List data = _collection.Find(_ => true).ToList(); - return data; - } - - } - - public class Contact - { - [BsonId] - [BsonRepresentation(BsonType.ObjectId)] - public string? Id { get; set; } - - [BsonElement("0")] - public ContactInfo? Info { get; set; } - } - - public class ContactInfo - { - [BsonElement("name")] - public string? Name { get; set; } - - [BsonElement("phone")] - public string? Phone { get; set; } - } - -} \ No newline at end of file diff --git a/srv_qry_mongo/ContactsController/.gitignore b/srv_qry_mongo/srv_qry_mongo.Api/.gitignore similarity index 100% rename from srv_qry_mongo/ContactsController/.gitignore rename to srv_qry_mongo/srv_qry_mongo.Api/.gitignore diff --git a/srv_qry_mongo/ContactsController/ContactsController.csproj b/srv_qry_mongo/srv_qry_mongo.Api/ContactsController.csproj similarity index 96% rename from srv_qry_mongo/ContactsController/ContactsController.csproj rename to srv_qry_mongo/srv_qry_mongo.Api/ContactsController.csproj index 5696258..0db85ba 100644 --- a/srv_qry_mongo/ContactsController/ContactsController.csproj +++ b/srv_qry_mongo/srv_qry_mongo.Api/ContactsController.csproj @@ -1,15 +1,15 @@ - - - - net7.0 - enable - enable - - - - - - - - - + + + + net7.0 + enable + enable + + + + + + + + + diff --git a/srv_qry_mongo/srv_qry_mongo.Api/Controllers/Contact.cs b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/Contact.cs new file mode 100644 index 0000000..25b6efd --- /dev/null +++ b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/Contact.cs @@ -0,0 +1,24 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace srv_qry_mongo.Api.Controllers +{ + public class Contact + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string? Id { get; set; } + + [BsonElement("0")] + public ContactInfo? Info { get; set; } + } + + public class ContactInfo + { + [BsonElement("name")] + public string? Name { get; set; } + + [BsonElement("phone")] + public string? Phone { get; set; } + } +} \ No newline at end of file diff --git a/srv_qry_mongo/srv_qry_mongo.Api/Controllers/ContactsController.cs b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/ContactsController.cs new file mode 100644 index 0000000..5c5671e --- /dev/null +++ b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/ContactsController.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using Microsoft.Extensions.Options; + + +namespace srv_qry_mongo.Api.Controllers +{ + [Route("api/contacts")] + [ApiController] + public class ContactsController : ControllerBase + { + private readonly MongoDBService _mongoDBService; + public ContactsController(MongoDBService mongoDBService) + { + _mongoDBService = mongoDBService; + } + + [HttpGet] + public IActionResult GetMongoContacts() + { + var contacts_data = _mongoDBService.GetAllContacts(); + return Ok(contacts_data); + } + } +} \ No newline at end of file diff --git a/srv_qry_mongo/srv_qry_mongo.Api/Controllers/MongoDBService.cs b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/MongoDBService.cs new file mode 100644 index 0000000..6b95be2 --- /dev/null +++ b/srv_qry_mongo/srv_qry_mongo.Api/Controllers/MongoDBService.cs @@ -0,0 +1,28 @@ +using MongoDB.Driver; +using Microsoft.Extensions.Options; + + +namespace srv_qry_mongo.Api.Controllers +{ + public class MongoDBService + { + private readonly IMongoCollection _collection; + + public MongoDBService(IOptions configuration) + { + string connectionString = configuration.Value.MongoDBConnection; + + var client = new MongoClient(connectionString); + + var database = client.GetDatabase("meteor"); + _collection = database.GetCollection("contacts"); + } + + public IEnumerable GetAllContacts() + { + List data = _collection.Find(_ => true).ToList(); + return data; + } + + } +} \ No newline at end of file diff --git a/srv_qry_mongo/ContactsController/Program.cs b/srv_qry_mongo/srv_qry_mongo.Api/Program.cs similarity index 86% rename from srv_qry_mongo/ContactsController/Program.cs rename to srv_qry_mongo/srv_qry_mongo.Api/Program.cs index e014137..a2fa107 100644 --- a/srv_qry_mongo/ContactsController/Program.cs +++ b/srv_qry_mongo/srv_qry_mongo.Api/Program.cs @@ -1,31 +1,31 @@ - -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. - -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - -builder.Services.Configure(builder.Configuration.GetSection("ConnectionStrings")); -builder.Services.AddSingleton(); -builder.Services.AddScoped(); - - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseHttpsRedirection(); - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.Configure(builder.Configuration.GetSection("ConnectionStrings")); +builder.Services.AddSingleton(); +builder.Services.AddScoped(); + + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/srv_qry_mongo/ContactsController/Properties/launchSettings.json b/srv_qry_mongo/srv_qry_mongo.Api/Properties/launchSettings.json similarity index 96% rename from srv_qry_mongo/ContactsController/Properties/launchSettings.json rename to srv_qry_mongo/srv_qry_mongo.Api/Properties/launchSettings.json index 13912dd..93d4064 100644 --- a/srv_qry_mongo/ContactsController/Properties/launchSettings.json +++ b/srv_qry_mongo/srv_qry_mongo.Api/Properties/launchSettings.json @@ -1,41 +1,41 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:13421", - "sslPort": 44388 - } - }, - "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "http://localhost:5009", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "https://localhost:7254;http://localhost:5009", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:13421", + "sslPort": 44388 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5009", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7254;http://localhost:5009", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/srv_qry_mongo/ContactsController/appsettings.Development.json b/srv_qry_mongo/srv_qry_mongo.Api/appsettings.Development.json similarity index 94% rename from srv_qry_mongo/ContactsController/appsettings.Development.json rename to srv_qry_mongo/srv_qry_mongo.Api/appsettings.Development.json index a984198..2a69237 100644 --- a/srv_qry_mongo/ContactsController/appsettings.Development.json +++ b/srv_qry_mongo/srv_qry_mongo.Api/appsettings.Development.json @@ -1,11 +1,11 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "ConnectionStrings": { - "MongoDBConnection": "mongodb://localhost:27017" - } -} +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "MongoDBConnection": "mongodb://localhost:27017" + } +} diff --git a/srv_qry_mongo/ContactsController/appsettings.json b/srv_qry_mongo/srv_qry_mongo.Api/appsettings.json similarity index 94% rename from srv_qry_mongo/ContactsController/appsettings.json rename to srv_qry_mongo/srv_qry_mongo.Api/appsettings.json index b4f9e1a..e8b9dea 100644 --- a/srv_qry_mongo/ContactsController/appsettings.json +++ b/srv_qry_mongo/srv_qry_mongo.Api/appsettings.json @@ -1,12 +1,12 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "ConnectionStrings": { - "MongoDBConnection": "mongodb://localhost:27017" - } -} +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "MongoDBConnection": "mongodb://localhost:27017" + } +} diff --git a/srv_qry_mongo/ContactsController/mongoDBConnect.cs b/srv_qry_mongo/srv_qry_mongo.Api/mongoDBConnect.cs similarity index 100% rename from srv_qry_mongo/ContactsController/mongoDBConnect.cs rename to srv_qry_mongo/srv_qry_mongo.Api/mongoDBConnect.cs diff --git a/phone-book-boost.sln b/srv_qry_mongo/srv_qry_mongo.sln similarity index 87% rename from phone-book-boost.sln rename to srv_qry_mongo/srv_qry_mongo.sln index 6f117bb..79900a7 100644 --- a/phone-book-boost.sln +++ b/srv_qry_mongo/srv_qry_mongo.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.5.002.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "srv_qry_mongo", "srv_qry_mongo", "{58D866C7-55F7-4690-BB2B-CBDCFAE6C9EA}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContactsController", "srv_qry_mongo\ContactsController\ContactsController.csproj", "{DC704891-3D6F-4452-9150-9AB0A3203499}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "srv_qry_mongo.Api", "srv_qry_mongo.Api\ContactsController.csproj", "{DC704891-3D6F-4452-9150-9AB0A3203499}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution