Skip to content

Commit

Permalink
srv_qry_mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
effedib committed Oct 30, 2023
1 parent 554e354 commit a2274fa
Show file tree
Hide file tree
Showing 60 changed files with 2,113 additions and 0 deletions.
30 changes: 30 additions & 0 deletions phone-book-boost.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DC704891-3D6F-4452-9150-9AB0A3203499}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC704891-3D6F-4452-9150-9AB0A3203499}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC704891-3D6F-4452-9150-9AB0A3203499}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC704891-3D6F-4452-9150-9AB0A3203499}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DC704891-3D6F-4452-9150-9AB0A3203499} = {58D866C7-55F7-4690-BB2B-CBDCFAE6C9EA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8AF7D318-858A-40CE-AB93-641A6AFFF269}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions srv_qry_mongo/ContactsController/ContactsController.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +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>
66 changes: 66 additions & 0 deletions srv_qry_mongo/ContactsController/Controllers/ContactsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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<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;
}

}

public class Contact
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }

[BsonElement("name")]
public string? Name { get; set; }

[BsonElement("phone")]
public string? Phone { get; set; }

}

}
31 changes: 31 additions & 0 deletions srv_qry_mongo/ContactsController/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +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();
41 changes: 41 additions & 0 deletions srv_qry_mongo/ContactsController/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +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"
}
}
}
}
11 changes: 11 additions & 0 deletions srv_qry_mongo/ContactsController/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"MongoDBConnection": "mongodb://172.17.0.1:27017"
}
}
12 changes: 12 additions & 0 deletions srv_qry_mongo/ContactsController/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MongoDBConnection": "mongodb://172.17.0.1:27017"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a2274fa

Please sign in to comment.