Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First version Gex Backend .Net using Mongo DB #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/bin/
**/obj/
**/global.json
**/Dockerfile*
**/.dockerignore*
**/*.user
**/.vs/
**/Static/
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/.vs/
**/*.user
**/internal_logs
.gitconfig
.env


19 changes: 19 additions & 0 deletions AdapterLayer/AdapterLayer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>AdapterLayer</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApplicationLayer\ApplicationLayer.csproj" />
<ProjectReference Include="..\DomainLayer\DomainLayer.csproj" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions AdapterLayer/DTOs/UserMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

using AdapterLayer.Models;

namespace AdapterLayer.DTOs
{
public class UserMapper
{
public UserMapper() { }

public UserModel ToModel(UserRequest request)
{
return new UserModel
{
nombre = request.nombre,
apellidos = request.apellidos,
email = request.email,
telefonoFijo = request.telefonoFijo,
telefonoMovil = request.telefonoMovil,
telefonoInstitucional = request.telefonoInstitucional,
clave = request.clave,
rol = request.rol,
esResponsable = request.esResponsable,
esAutorizante = request.esAutorizante
};
}
}
}
17 changes: 17 additions & 0 deletions AdapterLayer/DTOs/UserRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

namespace AdapterLayer.DTOs
{
public class UserRequest
{
public string nombre { get; set; }
public string apellidos { get; set; }
public string email { get; set; }
public string telefonoFijo { get; set; }
public string telefonoMovil { get; set; }
public string telefonoInstitucional { get; set; }
public string clave { get; set; }
public string rol { get; set; }
public bool esResponsable { get; set; }
public bool esAutorizante { get; set; }
}
}
22 changes: 22 additions & 0 deletions AdapterLayer/Models/UserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace AdapterLayer.Models
{
public class UserModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string _id { get; set; }
public string nombre { get; set; }
public string apellidos { get; set; }
public string email { get; set; }
public string telefonoFijo { get; set; }
public string telefonoMovil { get; set; }
public string telefonoInstitucional { get; set; }
public string clave { get; set; }
public string rol { get; set; }
public bool esResponsable { get; set; }
public bool esAutorizante { get; set; }
}
}
12 changes: 12 additions & 0 deletions AdapterLayer/Models/UserView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace AdapterLayer.Models
{
public class UserView
{
public string Id { get; set; }
public string nombre { get; set; }
public string apellidos { get; set; }
public string email { get; set; }
public string rol { get; set; }
}
}
26 changes: 26 additions & 0 deletions AdapterLayer/MongoDb/MongoConnectionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MongoDB.Driver;

namespace AdapterLayer.MongoDb
{
public class MongoConnectionService
{
private readonly string ConnectionString;
private readonly string DatabaseName;
public MongoConnectionService()
{
ConnectionString = Environment.GetEnvironmentVariable("MONGO_URL");
if (ConnectionString == null) ConnectionString = "mongodb://localhost:27017";

DatabaseName = Environment.GetEnvironmentVariable("MONGO_DB");
if (DatabaseName == null) DatabaseName = "gex-api";
}

public IMongoCollection<T> Connect<T>(string collection)
{
var client = new MongoClient(ConnectionString);
var db = client.GetDatabase(DatabaseName);
return db.GetCollection<T>(collection);
}

}
}
32 changes: 32 additions & 0 deletions AdapterLayer/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AdapterLayer.Models;
using AdapterLayer.MongoDb;
using ApplicationLayer.Interfaces;
using MongoDB.Driver;

namespace AdapterLayer.Repository
{
public class Repository : IRepository<UserModel>
{
private readonly MongoConnectionService _dbService;

public Repository(MongoConnectionService dbService)
{
_dbService = dbService;
}

public async Task<IEnumerable<UserModel>> GetAllUsersAsync()
{
var collection = _dbService.Connect<UserModel>("users");
var results = await collection.FindAsync(_ => true);
return results.ToList();
}

public async Task AddUserAsync(UserModel user)
{
var collection = _dbService.Connect<UserModel>("users");
await collection.InsertOneAsync(user);

}

}
}
Loading