From 9d2fa359c3e75c52e5c2f0998520aaad05d72ceb Mon Sep 17 00:00:00 2001 From: scogliera Date: Mon, 18 Dec 2023 17:59:35 +0100 Subject: [PATCH] Add DAL for storing lecturers --- Controllers/TdaApiController.cs | 7 ++-- DAL/IMongoDal.cs | 12 +++++++ DAL/MongoDal.cs | 64 +++++++++++++++++++++++++++++++++ Models/Lecturer.cs | 2 ++ Program.cs | 3 ++ TeacherDigitalAgency.csproj | 1 + appsettings.json | 5 ++- 7 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 DAL/IMongoDal.cs create mode 100644 DAL/MongoDal.cs diff --git a/Controllers/TdaApiController.cs b/Controllers/TdaApiController.cs index 3b55357..9bf1987 100644 --- a/Controllers/TdaApiController.cs +++ b/Controllers/TdaApiController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using TeacherDigitalAgency.DAL; using TeacherDigitalAgency.Models; namespace TeacherDigitalAgency.Controllers; @@ -8,10 +9,12 @@ namespace TeacherDigitalAgency.Controllers; public class TdaApiController: ControllerBase { private readonly ILogger _logger; + private readonly IMongoDal _mongoDal; - public TdaApiController(ILogger logger) + public TdaApiController(ILogger logger, IMongoDal mongoDal) { - _logger = logger; + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _mongoDal = mongoDal ?? throw new ArgumentNullException(nameof(mongoDal)); } [HttpGet("/api")] diff --git a/DAL/IMongoDal.cs b/DAL/IMongoDal.cs new file mode 100644 index 0000000..ce2b066 --- /dev/null +++ b/DAL/IMongoDal.cs @@ -0,0 +1,12 @@ +using TeacherDigitalAgency.Models; + +namespace TeacherDigitalAgency.DAL; + +public interface IMongoDal +{ + public Lecturer? GetLecturer(Guid id); + public IEnumerable GetAllLecturers(); + public void SetLecturer(Lecturer lecturer); + public bool DeleteLecturer(Guid id); + public void AddLecturer(Lecturer lecturer); +} \ No newline at end of file diff --git a/DAL/MongoDal.cs b/DAL/MongoDal.cs new file mode 100644 index 0000000..6808a21 --- /dev/null +++ b/DAL/MongoDal.cs @@ -0,0 +1,64 @@ +using MongoDB.Driver; +using TeacherDigitalAgency.Models; + +namespace TeacherDigitalAgency.DAL; + +public class MongoDal: IMongoDal +{ + private readonly IMongoCollection _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("lecturers"); + } + + public Lecturer? GetLecturer(Guid id) + { + return _lecturersCollection.Find(lecturer => lecturer.Uuid == id).FirstOrDefault(); + } + + public IEnumerable GetAllLecturers() + { + return _lecturersCollection.Find(_ => true).ToEnumerable(); + } + + public void SetLecturer(Lecturer lecturer) + { + var filter = Builders.Filter.Eq(l => l.Uuid, lecturer.Uuid); + var update = Builders.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); + } +} \ No newline at end of file diff --git a/Models/Lecturer.cs b/Models/Lecturer.cs index 8010f76..e379108 100644 --- a/Models/Lecturer.cs +++ b/Models/Lecturer.cs @@ -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; } diff --git a/Program.cs b/Program.cs index b0c1c78..17d6f7f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,5 @@ +using TeacherDigitalAgency.DAL; + var builder = WebApplication.CreateBuilder(args); // Add services to the container @@ -6,6 +8,7 @@ builder.Services.AddControllers(); builder.Services.AddAntDesign(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/TeacherDigitalAgency.csproj b/TeacherDigitalAgency.csproj index 22af351..2d53bd7 100644 --- a/TeacherDigitalAgency.csproj +++ b/TeacherDigitalAgency.csproj @@ -16,6 +16,7 @@ + diff --git a/appsettings.json b/appsettings.json index 10f68b8..e6337e5 100644 --- a/appsettings.json +++ b/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "MongoDb": "" + } }