Implement Post action for FeedbackReceiverController.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-05-29 20:02:16 +03:00
parent ebd4bd8ac3
commit 1e3f7c74d4
13 changed files with 164 additions and 3 deletions

View file

@ -12,6 +12,7 @@ namespace Retroactiune.Tests
[Test] [Test]
public void Test1() public void Test1()
{ {
// TODO: Test WebAPI, todo, Test service. =) :(
Assert.Pass(); Assert.Pass();
} }
} }

View file

@ -1,5 +1,8 @@
namespace Retroactiune.Models namespace Retroactiune.Models
{ {
/// <summary>
/// Simple response model that contains a message.
/// </summary>
public class BasicResponse public class BasicResponse
{ {
public string Message { get; set; } public string Message { get; set; }

View file

@ -0,0 +1,22 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Retroactiune.Models
{
/// <summary>
/// FeedbackReceiver is the thing that receives feedback.
/// </summary>
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; }
}
}

View file

@ -0,0 +1,12 @@
namespace Retroactiune.Models
{
/// <summary>
/// FeedbackReceiverDto is the DTO for <see cref="FeedbackReceiver"/>
/// </summary>
public class FeedbackReceiverDto
{
public string Name { get; set; }
public string Description { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using AutoMapper;
namespace Retroactiune.Models
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<FeedbackReceiver, FeedbackReceiverDto>().ReverseMap();
}
}
}

View file

@ -6,6 +6,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="MongoDB.Driver" Version="2.12.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup> </ItemGroup>

View file

@ -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
{
/// <summary>
/// Service that simplifies access to the database for managing FeedbackReceiver items.
/// <see cref="FeedbackReceiver"/>
/// </summary>
public class FeedbackReceiverService
{
private readonly IMongoCollection<FeedbackReceiver> _collection;
private readonly ILogger<FeedbackReceiverService> _logger;
public FeedbackReceiverService(IMongoDbSettings settings, ILogger<FeedbackReceiverService> logger)
{
var client = new MongoClient(settings.ConnectionString);
var database = client.GetDatabase(settings.DatabaseName);
_collection = database.GetCollection<FeedbackReceiver>(settings.FeedbackReceiverCollectionName);
_logger = logger;
}
public async Task CreateMany(IEnumerable<FeedbackReceiver> items)
{
try
{
await _collection.InsertManyAsync(items);
}
catch (Exception e)
{
throw new GenericServiceException($"Operation failed: {e.Message}");
}
}
}
}

View file

@ -0,0 +1,11 @@
using System;
namespace Retroactiune.Services
{
public class GenericServiceException : Exception
{
public GenericServiceException(string message) : base(message)
{
}
}
}

View file

@ -0,0 +1,16 @@
using System;
namespace Retroactiune.Settings
{
/// <summary>
/// Interface for repressing the application's MongoDb settings Options.
/// </summary>
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; }
}
}

View file

@ -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; }
}
}

View file

@ -1,9 +1,13 @@
using System;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Retroactiune.Services;
using Retroactiune.Settings;
namespace Retroactiune namespace Retroactiune
{ {
@ -19,6 +23,18 @@ namespace Retroactiune
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
// Database Configuration
services.Configure<RetroactiuneDbSettings>(Configuration.GetSection(nameof(RetroactiuneDbSettings)));
services.AddSingleton<IMongoDbSettings>(sp =>
sp.GetRequiredService<IOptions<RetroactiuneDbSettings>>().Value);
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// Services
services.AddSingleton<FeedbackReceiverService>();
// WebAPI
services.AddControllers(); services.AddControllers();
services.AddSwaggerGen(); services.AddSwaggerGen();
} }
@ -30,14 +46,14 @@ namespace Retroactiune
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => app.UseSwaggerUI(c =>
{ {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Retroactiune API"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "Retroactiune API");
c.RoutePrefix = ""; c.RoutePrefix = "";
}); });
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
@ -45,7 +61,7 @@ namespace Retroactiune
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
logger.LogInformation("Running"); logger.LogInformation("Running");
} }
} }

View file

@ -1,4 +1,11 @@
{ {
"RetroactiuneDbSettings": {
"FeedbackCollectionName": "feedback",
"TokensCollectionName": "tokens",
"FeedbackReceiverCollectionName": "feedback_receiver",
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Retroactiune"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

6
docker-compose.yaml Normal file
View file

@ -0,0 +1,6 @@
version: '3.7'
services:
mongodb:
image: mongo:latest
ports:
- 27017:27017