Add Token & TokensService.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-10 00:03:07 +03:00
parent 0d1e880d18
commit e2d3ef888d
15 changed files with 117 additions and 13 deletions

View file

@ -14,6 +14,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using Retroactiune.DataTransferObjects;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
using Retroactiune.Settings;

View file

@ -10,7 +10,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
{
public class MongoDbFixture : IAsyncDisposable
{
private IMongoDbSettings _settings;
private readonly IMongoDbSettings _settings;
public IMongoDatabase Database { get; }
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>

View file

@ -5,6 +5,7 @@ using AutoFixture.Xunit2;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Retroactiune.Controllers;
using Retroactiune.DataTransferObjects;
using Retroactiune.Models;
using Retroactiune.Services;
using Xunit;

View file

@ -7,6 +7,7 @@ using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Retroactiune.DataTransferObjects;
using Retroactiune.Models;
using Retroactiune.Services;
@ -34,10 +35,10 @@ namespace Retroactiune.Controllers
/// </summary>
/// <param name="items">The list of FeedbackReceivers</param>
/// <returns>A BasicResponse indicating success.</returns>
/// <response code="201">Returns the newly created item</response>
/// <response code="200">Returns an ok message.</response>
/// <response code="400">If the items is invalid</response>
[HttpPost]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post([Required] IEnumerable<FeedbackReceiverInDto> items)
{

View file

@ -1,4 +1,4 @@
namespace Retroactiune.Models
namespace Retroactiune.DataTransferObjects
{
/// <summary>
/// Simple response model that contains a message.

View file

@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using Retroactiune.Models;
namespace Retroactiune.Models
namespace Retroactiune.DataTransferObjects
{
/// <summary>
/// FeedbackReceiverInDto is the DTO for <see cref="FeedbackReceiver"/>, used in incoming requests.
@ -13,11 +14,4 @@ namespace Retroactiune.Models
[Required]
public string Description { get; set; }
}
/// <summary>
/// FeedbackReceiverDto is the DTO for <see cref="FeedbackReceiver"/>, used in outgoing requests.
/// </summary>
public class FeedbackReceiverOutDto : FeedbackReceiver
{
}
}

View file

@ -0,0 +1,11 @@
using Retroactiune.Models;
namespace Retroactiune.DataTransferObjects
{
/// <summary>
/// FeedbackReceiverDto is the DTO for <see cref="FeedbackReceiver"/>, used in outgoing requests.
/// </summary>
public class FeedbackReceiverOutDto : FeedbackReceiver
{
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.ComponentModel.DataAnnotations;
using Retroactiune.DataAnnotations;
namespace Retroactiune.DataTransferObjects
{
/// <summary>
/// GenerateTokensDto is the payload that is sent by the user for generating tokens.
/// </summary>
public class GenerateTokensDto
{
[Range(1, 1000, ErrorMessage = "numberOfTokens is out of range, allowed ranges [1-1000]")]
public int NumberOfTokens { get; set; } = 1;
[Required, StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
public string FeedbackReceiverId { get; set; }
[DatetimeNotInThePast(ErrorMessage = "expiryTime cannot be in the past!")]
public DateTime? ExpiryTime { get; set; } = null;
}
}

View file

@ -1,4 +1,5 @@
using AutoMapper;
using Retroactiune.DataTransferObjects;
using Retroactiune.Models;
namespace Retroactiune

View file

@ -0,0 +1,7 @@
namespace Retroactiune.Models
{
public class Feedback
{
// TODO: Entity model for feedback.
}
}

View file

@ -6,7 +6,7 @@ using MongoDB.Bson.Serialization.Attributes;
namespace Retroactiune.Models
{
/// <summary>
/// FeedbackReceiver is the thing that receives feedback.
/// FeedbackReceiver is the entity that receives feedback from the users.
/// </summary>
public class FeedbackReceiver
{

View file

@ -0,0 +1,30 @@
using System;
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Retroactiune.Models
{
/// <summary>
/// Token represents a token.
/// Token is used to authorize a <see cref="Feedback"/> for the <see cref="FeedbackReceiver"/>.
/// </summary>
public class Token
{
[BsonId, JsonPropertyName("id")]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonRepresentation(BsonType.ObjectId), JsonPropertyName("feedback_receiver_id")]
public string FeedbackReceiverId { get; set; }
[JsonPropertyName("time_used")]
public DateTime? TimeUsed { get; set; }
[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; }
[JsonPropertyName("expiry_time")]
public DateTime? ExpiryTime { get; set; }
}
}

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Retroactiune.Models;
namespace Retroactiune.Services
{
public interface ITokensService
{
public Task CreateManyAsync(IEnumerable<Token> items);
}
}

View file

@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
using Retroactiune.Models;
using Retroactiune.Settings;
// TODO: Test
namespace Retroactiune.Services
{
public class TokensService : ITokensService
{
private readonly IMongoCollection<Token> _collection;
public TokensService(IMongoClient client, IMongoDbSettings settings)
{
var database = client.GetDatabase(settings.DatabaseName);
_collection = database.GetCollection<Token>(settings.TokensCollectionName);
}
public async Task CreateManyAsync(IEnumerable<Token> items)
{
throw new System.NotImplementedException();
}
}
}

View file

@ -38,6 +38,7 @@ namespace Retroactiune
// Services
services.AddSingleton<IFeedbackReceiverService, FeedbackReceiverService>();
services.AddSingleton<ITokensService, TokensService>();
services.AddSingleton<IMongoClient, MongoClient>(i =>
{
var settings = i.GetService<IOptions<RetroactiuneDbSettings>>();