From 1e3f7c74d4c8d4060168d974fce2e500835177b9 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 29 May 2021 20:02:16 +0300 Subject: [PATCH] Implement Post action for FeedbackReceiverController.cs --- .../Retroactiune.WebAPI/UnitTest1.cs | 1 + Retroactiune.WebAPI/Models/BasicResponse.cs | 3 ++ .../Models/FeedbackReceiver.cs | 22 ++++++++++ .../Models/FeedbackReceiverDto.cs | 12 ++++++ Retroactiune.WebAPI/Models/MappingProfile.cs | 12 ++++++ .../Retroactiune.WebAPI.csproj | 3 ++ .../Services/FeedbackReceiverService.cs | 41 +++++++++++++++++++ .../Services/GenericServiceException.cs | 11 +++++ .../Settings/IMongoDbSettings.cs | 16 ++++++++ .../Settings/RetroactiuneDbSettings.cs | 11 +++++ Retroactiune.WebAPI/Startup.cs | 22 ++++++++-- Retroactiune.WebAPI/appsettings.json | 7 ++++ docker-compose.yaml | 6 +++ 13 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 Retroactiune.WebAPI/Models/FeedbackReceiver.cs create mode 100644 Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs create mode 100644 Retroactiune.WebAPI/Models/MappingProfile.cs create mode 100644 Retroactiune.WebAPI/Services/FeedbackReceiverService.cs create mode 100644 Retroactiune.WebAPI/Services/GenericServiceException.cs create mode 100644 Retroactiune.WebAPI/Settings/IMongoDbSettings.cs create mode 100644 Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs create mode 100644 docker-compose.yaml diff --git a/Retroactiune.Tests/Retroactiune.WebAPI/UnitTest1.cs b/Retroactiune.Tests/Retroactiune.WebAPI/UnitTest1.cs index 25dbee6..51b7044 100644 --- a/Retroactiune.Tests/Retroactiune.WebAPI/UnitTest1.cs +++ b/Retroactiune.Tests/Retroactiune.WebAPI/UnitTest1.cs @@ -12,6 +12,7 @@ namespace Retroactiune.Tests [Test] public void Test1() { + // TODO: Test WebAPI, todo, Test service. =) :( Assert.Pass(); } } diff --git a/Retroactiune.WebAPI/Models/BasicResponse.cs b/Retroactiune.WebAPI/Models/BasicResponse.cs index 116375a..c8ee12c 100644 --- a/Retroactiune.WebAPI/Models/BasicResponse.cs +++ b/Retroactiune.WebAPI/Models/BasicResponse.cs @@ -1,5 +1,8 @@ namespace Retroactiune.Models { + /// + /// Simple response model that contains a message. + /// public class BasicResponse { public string Message { get; set; } diff --git a/Retroactiune.WebAPI/Models/FeedbackReceiver.cs b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs new file mode 100644 index 0000000..1ca1c0e --- /dev/null +++ b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs @@ -0,0 +1,22 @@ +using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace Retroactiune.Models +{ + /// + /// FeedbackReceiver is the thing that receives feedback. + /// + public class FeedbackReceiver + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public string Name { get; set; } + + public string Description { get; set; } + + public DateTime CreatedAt { get; set; } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs b/Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs new file mode 100644 index 0000000..3190eb9 --- /dev/null +++ b/Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs @@ -0,0 +1,12 @@ +namespace Retroactiune.Models +{ + /// + /// FeedbackReceiverDto is the DTO for + /// + public class FeedbackReceiverDto + { + public string Name { get; set; } + + public string Description { get; set; } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Models/MappingProfile.cs b/Retroactiune.WebAPI/Models/MappingProfile.cs new file mode 100644 index 0000000..6085e66 --- /dev/null +++ b/Retroactiune.WebAPI/Models/MappingProfile.cs @@ -0,0 +1,12 @@ +using AutoMapper; + +namespace Retroactiune.Models +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap().ReverseMap(); + } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj b/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj index a8f9e54..3125d46 100644 --- a/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj +++ b/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj @@ -6,6 +6,9 @@ + + + diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs new file mode 100644 index 0000000..77ae1f3 --- /dev/null +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using MongoDB.Driver; +using Retroactiune.Models; +using Retroactiune.Settings; + +namespace Retroactiune.Services +{ + /// + /// Service that simplifies access to the database for managing FeedbackReceiver items. + /// + /// + public class FeedbackReceiverService + { + private readonly IMongoCollection _collection; + private readonly ILogger _logger; + + public FeedbackReceiverService(IMongoDbSettings settings, ILogger logger) + { + var client = new MongoClient(settings.ConnectionString); + var database = client.GetDatabase(settings.DatabaseName); + _collection = database.GetCollection(settings.FeedbackReceiverCollectionName); + _logger = logger; + } + + public async Task CreateMany(IEnumerable items) + { + try + { + await _collection.InsertManyAsync(items); + } + catch (Exception e) + { + throw new GenericServiceException($"Operation failed: {e.Message}"); + } + + } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/GenericServiceException.cs b/Retroactiune.WebAPI/Services/GenericServiceException.cs new file mode 100644 index 0000000..06e9cf3 --- /dev/null +++ b/Retroactiune.WebAPI/Services/GenericServiceException.cs @@ -0,0 +1,11 @@ +using System; + +namespace Retroactiune.Services +{ + public class GenericServiceException : Exception + { + public GenericServiceException(string message) : base(message) + { + } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs b/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs new file mode 100644 index 0000000..101bb42 --- /dev/null +++ b/Retroactiune.WebAPI/Settings/IMongoDbSettings.cs @@ -0,0 +1,16 @@ +using System; + +namespace Retroactiune.Settings +{ + /// + /// Interface for repressing the application's MongoDb settings Options. + /// + public interface IMongoDbSettings + { + public string FeedbackCollectionName { get; set; } + public string TokensCollectionName { get; set; } + public string FeedbackReceiverCollectionName { get; set; } + public string ConnectionString { get; set; } + public string DatabaseName { get; set; } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs b/Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs new file mode 100644 index 0000000..b14e376 --- /dev/null +++ b/Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs @@ -0,0 +1,11 @@ +namespace Retroactiune.Settings +{ + public class RetroactiuneDbSettings : IMongoDbSettings + { + public string FeedbackCollectionName { get; set; } + public string TokensCollectionName { get; set; } + public string FeedbackReceiverCollectionName { get; set; } + public string ConnectionString { get; set; } + public string DatabaseName { get; set; } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Startup.cs b/Retroactiune.WebAPI/Startup.cs index 9dde286..87a780b 100644 --- a/Retroactiune.WebAPI/Startup.cs +++ b/Retroactiune.WebAPI/Startup.cs @@ -1,9 +1,13 @@ +using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Retroactiune.Services; +using Retroactiune.Settings; namespace Retroactiune { @@ -19,6 +23,18 @@ namespace Retroactiune // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + // Database Configuration + services.Configure(Configuration.GetSection(nameof(RetroactiuneDbSettings))); + services.AddSingleton(sp => + sp.GetRequiredService>().Value); + + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + + + // Services + services.AddSingleton(); + + // WebAPI services.AddControllers(); services.AddSwaggerGen(); } @@ -30,14 +46,14 @@ namespace Retroactiune { app.UseDeveloperExceptionPage(); } - + app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Retroactiune API"); c.RoutePrefix = ""; }); - + app.UseHttpsRedirection(); app.UseRouting(); @@ -45,7 +61,7 @@ namespace Retroactiune app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); - + logger.LogInformation("Running"); } } diff --git a/Retroactiune.WebAPI/appsettings.json b/Retroactiune.WebAPI/appsettings.json index d9d9a9b..af55352 100644 --- a/Retroactiune.WebAPI/appsettings.json +++ b/Retroactiune.WebAPI/appsettings.json @@ -1,4 +1,11 @@ { + "RetroactiuneDbSettings": { + "FeedbackCollectionName": "feedback", + "TokensCollectionName": "tokens", + "FeedbackReceiverCollectionName": "feedback_receiver", + "ConnectionString": "mongodb://localhost:27017", + "DatabaseName": "Retroactiune" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..5d08493 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,6 @@ +version: '3.7' +services: + mongodb: + image: mongo:latest + ports: + - 27017:27017 \ No newline at end of file