Implement Post action for FeedbackReceiverController.cs
This commit is contained in:
parent
ebd4bd8ac3
commit
1e3f7c74d4
13 changed files with 164 additions and 3 deletions
|
@ -12,6 +12,7 @@ namespace Retroactiune.Tests
|
|||
[Test]
|
||||
public void Test1()
|
||||
{
|
||||
// TODO: Test WebAPI, todo, Test service. =) :(
|
||||
Assert.Pass();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
namespace Retroactiune.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple response model that contains a message.
|
||||
/// </summary>
|
||||
public class BasicResponse
|
||||
{
|
||||
public string Message { get; set; }
|
||||
|
|
22
Retroactiune.WebAPI/Models/FeedbackReceiver.cs
Normal file
22
Retroactiune.WebAPI/Models/FeedbackReceiver.cs
Normal 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; }
|
||||
}
|
||||
}
|
12
Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs
Normal file
12
Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs
Normal 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; }
|
||||
}
|
||||
}
|
12
Retroactiune.WebAPI/Models/MappingProfile.cs
Normal file
12
Retroactiune.WebAPI/Models/MappingProfile.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using AutoMapper;
|
||||
|
||||
namespace Retroactiune.Models
|
||||
{
|
||||
public class MappingProfile : Profile
|
||||
{
|
||||
public MappingProfile()
|
||||
{
|
||||
CreateMap<FeedbackReceiver, FeedbackReceiverDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,9 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
41
Retroactiune.WebAPI/Services/FeedbackReceiverService.cs
Normal file
41
Retroactiune.WebAPI/Services/FeedbackReceiverService.cs
Normal 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}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
11
Retroactiune.WebAPI/Services/GenericServiceException.cs
Normal file
11
Retroactiune.WebAPI/Services/GenericServiceException.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace Retroactiune.Services
|
||||
{
|
||||
public class GenericServiceException : Exception
|
||||
{
|
||||
public GenericServiceException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
16
Retroactiune.WebAPI/Settings/IMongoDbSettings.cs
Normal file
16
Retroactiune.WebAPI/Settings/IMongoDbSettings.cs
Normal 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; }
|
||||
}
|
||||
}
|
11
Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs
Normal file
11
Retroactiune.WebAPI/Settings/RetroactiuneDbSettings.cs
Normal 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; }
|
||||
}
|
||||
}
|
|
@ -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<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.AddSwaggerGen();
|
||||
}
|
||||
|
|
|
@ -1,4 +1,11 @@
|
|||
{
|
||||
"RetroactiuneDbSettings": {
|
||||
"FeedbackCollectionName": "feedback",
|
||||
"TokensCollectionName": "tokens",
|
||||
"FeedbackReceiverCollectionName": "feedback_receiver",
|
||||
"ConnectionString": "mongodb://localhost:27017",
|
||||
"DatabaseName": "Retroactiune"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
|
6
docker-compose.yaml
Normal file
6
docker-compose.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
version: '3.7'
|
||||
services:
|
||||
mongodb:
|
||||
image: mongo:latest
|
||||
ports:
|
||||
- 27017:27017
|
Loading…
Reference in a new issue