Skip to content

Commit

Permalink
Merge pull request #10 from scogliera/feature/add_dal
Browse files Browse the repository at this point in the history
Propojení projektu s MongoDB
  • Loading branch information
zweetband authored Dec 18, 2023
2 parents 897a353 + 9d2fa35 commit 95def99
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Controllers/TdaApiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using TeacherDigitalAgency.DAL;
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.Controllers;
Expand All @@ -8,10 +9,12 @@ namespace TeacherDigitalAgency.Controllers;
public class TdaApiController: ControllerBase
{
private readonly ILogger<TdaApiController> _logger;
private readonly IMongoDal _mongoDal;

public TdaApiController(ILogger<TdaApiController> logger)
public TdaApiController(ILogger<TdaApiController> logger, IMongoDal mongoDal)
{
_logger = logger;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_mongoDal = mongoDal ?? throw new ArgumentNullException(nameof(mongoDal));
}

[HttpGet("/api")]
Expand Down
12 changes: 12 additions & 0 deletions DAL/IMongoDal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.DAL;

public interface IMongoDal
{
public Lecturer? GetLecturer(Guid id);
public IEnumerable<Lecturer> GetAllLecturers();
public void SetLecturer(Lecturer lecturer);
public bool DeleteLecturer(Guid id);
public void AddLecturer(Lecturer lecturer);
}
64 changes: 64 additions & 0 deletions DAL/MongoDal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using MongoDB.Driver;
using TeacherDigitalAgency.Models;

namespace TeacherDigitalAgency.DAL;

public class MongoDal: IMongoDal
{
private readonly IMongoCollection<Lecturer> _lecturersCollection;

public MongoDal(IConfiguration configuration)
{
if (configuration == null)
throw new ArgumentNullException(nameof(configuration));

var connectionString = configuration.GetConnectionString("MongoDb") ?? throw new ArgumentNullException(nameof(configuration));

var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);

var client = new MongoClient(settings);
_lecturersCollection = client.GetDatabase("tda-db").GetCollection<Lecturer>("lecturers");
}

public Lecturer? GetLecturer(Guid id)
{
return _lecturersCollection.Find(lecturer => lecturer.Uuid == id).FirstOrDefault();
}

public IEnumerable<Lecturer> GetAllLecturers()
{
return _lecturersCollection.Find(_ => true).ToEnumerable();
}

public void SetLecturer(Lecturer lecturer)
{
var filter = Builders<Lecturer>.Filter.Eq(l => l.Uuid, lecturer.Uuid);
var update = Builders<Lecturer>.Update
.Set(l => l.TitleBefore, lecturer.TitleBefore)
.Set(l => l.FirstName, lecturer.FirstName)
.Set(l => l.MiddleName, lecturer.MiddleName)
.Set(l => l.LastName, lecturer.LastName)
.Set(l => l.TitleAfter, lecturer.TitleAfter)
.Set(l => l.PictureUrl, lecturer.PictureUrl)
.Set(l => l.Location, lecturer.Location)
.Set(l => l.Claim, lecturer.Claim)
.Set(l => l.Bio, lecturer.Bio)
.Set(l => l.Tags, lecturer.Tags)
.Set(l => l.PricePerHour, lecturer.PricePerHour)
.Set(l => l.ContactInfo, lecturer.ContactInfo);

_lecturersCollection.UpdateOne(filter, update);
}

public bool DeleteLecturer(Guid id)
{
var deleteResult = _lecturersCollection.DeleteOne(lecturer => lecturer.Uuid == id);
return deleteResult.DeletedCount > 0 && deleteResult.IsAcknowledged;
}

public void AddLecturer(Lecturer lecturer)
{
_lecturersCollection.InsertOne(lecturer);
}
}
2 changes: 2 additions & 0 deletions Models/Lecturer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Text.Json.Serialization;
using MongoDB.Bson;

namespace TeacherDigitalAgency.Models;

public class Lecturer
{
[JsonIgnore] public ObjectId? _id { get; init; }
[JsonPropertyName("uuid")] public Guid Uuid { get; init; } = Guid.NewGuid();
[JsonPropertyName("title_before")] public string? TitleBefore { get; set; }
[JsonPropertyName("first_name")] public required string FirstName { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using TeacherDigitalAgency.DAL;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
Expand All @@ -6,6 +8,7 @@
builder.Services.AddControllers();
builder.Services.AddAntDesign();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IMongoDal, MongoDal>();

var app = builder.Build();

Expand Down
1 change: 1 addition & 0 deletions TeacherDigitalAgency.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<PackageReference Include="AntDesign" Version="0.16.3" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
Expand Down
5 changes: 4 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"MongoDb": ""
}
}

0 comments on commit 95def99

Please sign in to comment.