Skip to content

Commit

Permalink
refactor srv_qry_mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
effedib committed Nov 5, 2023
1 parent c399788 commit 339a9e1
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 182 deletions.
71 changes: 0 additions & 71 deletions srv_qry_mongo/ContactsController/Controllers/ContactsController.cs

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions srv_qry_mongo/srv_qry_mongo.Api/Controllers/Contact.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
27 changes: 27 additions & 0 deletions srv_qry_mongo/srv_qry_mongo.Api/Controllers/ContactsController.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
28 changes: 28 additions & 0 deletions srv_qry_mongo/srv_qry_mongo.Api/Controllers/MongoDBService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MongoDB.Driver;
using Microsoft.Extensions.Options;


namespace srv_qry_mongo.Api.Controllers
{
public class MongoDBService
{
private readonly IMongoCollection<Contact> _collection;

public MongoDBService(IOptions<MongoDBConnect> configuration)
{
string connectionString = configuration.Value.MongoDBConnection;

var client = new MongoClient(connectionString);

var database = client.GetDatabase("meteor");
_collection = database.GetCollection<Contact>("contacts");
}

public IEnumerable<Contact> GetAllContacts()
{
List<Contact> data = _collection.Find(_ => true).ToList();
return data;
}

}
}
Original file line number Diff line number Diff line change
@@ -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<MongoDBConnect>(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.AddSingleton<MongoDBConnect>();
builder.Services.AddScoped<ContactsController.Controllers.MongoDBService>();


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<MongoDBConnect>(builder.Configuration.GetSection("ConnectionStrings"));
builder.Services.AddSingleton<MongoDBConnect>();
builder.Services.AddScoped<srv_qry_mongo.Api.Controllers.MongoDBService>();


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();
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion phone-book-boost.sln → srv_qry_mongo/srv_qry_mongo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 339a9e1

Please sign in to comment.