From 2525e37f8376666ace00dfeffc9038c1471a80cb Mon Sep 17 00:00:00 2001 From: Andrey Shabarshov Date: Mon, 27 Dec 2021 16:22:41 +0000 Subject: [PATCH] Initial immplementation of QRBeeAPI added --- .../RegistrationRequest.cs} | 8 +- QRBee.Core/Data/RegistrationResponse.cs | 22 ++++++ QRBee.sln | 16 +++- QRBee.sln.DotSettings | 3 +- QRBeeApi/Controllers/QRBeeController.cs | 28 +++++++ QRBeeApi/DatabaseSettings.cs | 17 +++++ QRBeeApi/Program.cs | 10 +++ QRBeeApi/QRBee.Api.csproj | 19 +++++ QRBeeApi/QRBeeApi.csproj | 13 ---- QRBeeApi/Services/Database/IStorage.cs | 12 +++ QRBeeApi/Services/Database/Storage.cs | 17 +++++ QRBeeApi/Services/Database/UserInfo.cs | 40 ++++++++++ QRBeeApi/Services/IQRBeeAPI.cs | 12 +++ QRBeeApi/Services/QRBeeAPI.cs | 74 +++++++++++++++++++ QRBeeApi/appsettings.json | 8 ++ 15 files changed, 283 insertions(+), 16 deletions(-) rename QRBee.Core/{RegistrationData.cs => Data/RegistrationRequest.cs} (74%) create mode 100644 QRBee.Core/Data/RegistrationResponse.cs create mode 100644 QRBeeApi/Controllers/QRBeeController.cs create mode 100644 QRBeeApi/DatabaseSettings.cs create mode 100644 QRBeeApi/QRBee.Api.csproj delete mode 100644 QRBeeApi/QRBeeApi.csproj create mode 100644 QRBeeApi/Services/Database/IStorage.cs create mode 100644 QRBeeApi/Services/Database/Storage.cs create mode 100644 QRBeeApi/Services/Database/UserInfo.cs create mode 100644 QRBeeApi/Services/IQRBeeAPI.cs create mode 100644 QRBeeApi/Services/QRBeeAPI.cs diff --git a/QRBee.Core/RegistrationData.cs b/QRBee.Core/Data/RegistrationRequest.cs similarity index 74% rename from QRBee.Core/RegistrationData.cs rename to QRBee.Core/Data/RegistrationRequest.cs index b241c58..9286ae6 100644 --- a/QRBee.Core/RegistrationData.cs +++ b/QRBee.Core/Data/RegistrationRequest.cs @@ -1,6 +1,6 @@ namespace QRBee.Core { - internal class RegistrationData + public record RegistrationRequest { public string Name { @@ -26,6 +26,12 @@ set; } + public bool RegisterAsMerchant + { + get; + set; + } + } } diff --git a/QRBee.Core/Data/RegistrationResponse.cs b/QRBee.Core/Data/RegistrationResponse.cs new file mode 100644 index 0000000..945277d --- /dev/null +++ b/QRBee.Core/Data/RegistrationResponse.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace QRBee.Core.Data +{ + public record RegistrationResponse + { + public string ClientId + { + get; + set; + } + + public string Certificate + { + get; + set; + } + + } +} diff --git a/QRBee.sln b/QRBee.sln index f6e179e..442f79c 100644 --- a/QRBee.sln +++ b/QRBee.sln @@ -9,7 +9,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRBee.iOS", "QRBee\QRBee.iO EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRBee", "QRBee\QRBee\QRBee.csproj", "{9C717836-BE20-4721-877B-8B6250245FF9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRBeeApi", "QRBeeApi\QRBeeApi.csproj", "{D985A736-12A3-4A7A-B1A7-502321BD460A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRBee.Api", "QRBeeApi\QRBee.Api.csproj", "{D985A736-12A3-4A7A-B1A7-502321BD460A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRBee.Core", "QRBee.Core\QRBee.Core.csproj", "{7C461562-66EF-4894-8AD8-F27F0B94053F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -87,6 +89,18 @@ Global {D985A736-12A3-4A7A-B1A7-502321BD460A}.Release|iPhone.Build.0 = Release|Any CPU {D985A736-12A3-4A7A-B1A7-502321BD460A}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU {D985A736-12A3-4A7A-B1A7-502321BD460A}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|iPhone.Build.0 = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|Any CPU.Build.0 = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|iPhone.ActiveCfg = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|iPhone.Build.0 = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {7C461562-66EF-4894-8AD8-F27F0B94053F}.Release|iPhoneSimulator.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/QRBee.sln.DotSettings b/QRBee.sln.DotSettings index 7cdc32a..6c57435 100644 --- a/QRBee.sln.DotSettings +++ b/QRBee.sln.DotSettings @@ -1,2 +1,3 @@  - QR \ No newline at end of file + QR + True \ No newline at end of file diff --git a/QRBeeApi/Controllers/QRBeeController.cs b/QRBeeApi/Controllers/QRBeeController.cs new file mode 100644 index 0000000..c10bd13 --- /dev/null +++ b/QRBeeApi/Controllers/QRBeeController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using QRBee.Api.Services; +using QRBee.Core; +using QRBee.Core.Data; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace QRBee.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class QRBeeController : ControllerBase + { + private readonly IQRBeeAPI _service; + + public QRBeeController(IQRBeeAPI service) + { + _service = service; + } + + [HttpPost] + public Task Register([FromBody] RegistrationRequest value) + { + return _service.Register(value); + } + + } +} diff --git a/QRBeeApi/DatabaseSettings.cs b/QRBeeApi/DatabaseSettings.cs new file mode 100644 index 0000000..7d3a856 --- /dev/null +++ b/QRBeeApi/DatabaseSettings.cs @@ -0,0 +1,17 @@ +using MongoDB.Driver; + +namespace QRBee.Api +{ + public class DatabaseSettings + { + public string? ConnectionString { get; set;} + + public MongoClientSettings ToMongoDbSettings() + { + var settings = MongoClientSettings.FromConnectionString(ConnectionString); + + return settings; + + } + } +} diff --git a/QRBeeApi/Program.cs b/QRBeeApi/Program.cs index 48863a6..5ad4e67 100644 --- a/QRBeeApi/Program.cs +++ b/QRBeeApi/Program.cs @@ -1,3 +1,8 @@ +using MongoDB.Driver; +using QRBee.Api; +using QRBee.Api.Services; +using QRBee.Api.Services.Database; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +12,11 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.Configure(builder.Configuration.GetSection("QRBeeDatabase")); +builder.Services.AddSingleton( cfg => new MongoClient(cfg.GetRequiredService().ToMongoDbSettings())); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/QRBeeApi/QRBee.Api.csproj b/QRBeeApi/QRBee.Api.csproj new file mode 100644 index 0000000..dd68915 --- /dev/null +++ b/QRBeeApi/QRBee.Api.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + 3b7dc7f1-0b82-4746-b99b-73c43c8826e0 + + + + + + + + + + + + diff --git a/QRBeeApi/QRBeeApi.csproj b/QRBeeApi/QRBeeApi.csproj deleted file mode 100644 index 60bf9ea..0000000 --- a/QRBeeApi/QRBeeApi.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - diff --git a/QRBeeApi/Services/Database/IStorage.cs b/QRBeeApi/Services/Database/IStorage.cs new file mode 100644 index 0000000..c48a748 --- /dev/null +++ b/QRBeeApi/Services/Database/IStorage.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Identity; + +namespace QRBee.Api.Services +{ + public interface IStorage + { + + void PutUserInfo(UserInfo info); + UserInfo GetUserInfo(string email); + + } +} diff --git a/QRBeeApi/Services/Database/Storage.cs b/QRBeeApi/Services/Database/Storage.cs new file mode 100644 index 0000000..550b43e --- /dev/null +++ b/QRBeeApi/Services/Database/Storage.cs @@ -0,0 +1,17 @@ +namespace QRBee.Api.Services.Database +{ + public class Storage: IStorage + { + public void PutUserInfo(UserInfo info) + { + throw new NotImplementedException(); + } + + public UserInfo GetUserInfo(string email) + { + throw new NotImplementedException(); + } + + + } +} diff --git a/QRBeeApi/Services/Database/UserInfo.cs b/QRBeeApi/Services/Database/UserInfo.cs new file mode 100644 index 0000000..739f523 --- /dev/null +++ b/QRBeeApi/Services/Database/UserInfo.cs @@ -0,0 +1,40 @@ +namespace QRBee.Api.Services; + +public record UserInfo +{ + public UserInfo() + { + + } + + public UserInfo(string name, string email, string dateOfBirth) + { + Name = name; + Email = email; + DateOfBirth = dateOfBirth; + } + + public string? Name + { + get; + set; + } + + public string? Email + { + get; + set; + } + + public string? DateOfBirth + { + get; + set; + } + + public bool RegisterAsMerchant + { + get; + set; + } +} \ No newline at end of file diff --git a/QRBeeApi/Services/IQRBeeAPI.cs b/QRBeeApi/Services/IQRBeeAPI.cs new file mode 100644 index 0000000..af10b6d --- /dev/null +++ b/QRBeeApi/Services/IQRBeeAPI.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; +using QRBee.Core; +using QRBee.Core.Data; + +namespace QRBee.Api.Services +{ + public interface IQRBeeAPI + { + Task Register(RegistrationRequest value); + + } +} diff --git a/QRBeeApi/Services/QRBeeAPI.cs b/QRBeeApi/Services/QRBeeAPI.cs new file mode 100644 index 0000000..64dc427 --- /dev/null +++ b/QRBeeApi/Services/QRBeeAPI.cs @@ -0,0 +1,74 @@ +using System.Text.RegularExpressions; +using QRBee.Core; +using QRBee.Core.Data; + +namespace QRBee.Api.Services +{ + public class QRBeeAPI: IQRBeeAPI + { + private readonly IStorage _storage; + + public QRBeeAPI(IStorage storage) + { + _storage = storage; + } + + public Task Register(RegistrationRequest request) + { + throw new NotImplementedException(); + + Validate(request); + + var info = Convert(request); + + if (UserExists(info)) + { + // throw error + } + else + { + _storage.PutUserInfo(info); + } + + } + + private void Validate(RegistrationRequest request) + { + if (request == null) + { + throw new NullReferenceException(); + } + + var name = request.Name; + var email = request.Email; + var dateOfBirth = request.DateOfBirth; + + if (string.IsNullOrEmpty(name) || name.All(char.IsLetter)==false || name.Length>=30) + { + // throw exception + } + + var freq = Regex.Matches(email, '@'.ToString()).Count; + + if (string.IsNullOrEmpty(email) || email.Contains('@')==false || freq>=2 || email.Length >= 30) + { + // throw exception + } + + // DateOfBirth check + + } + + private UserInfo Convert(RegistrationRequest request) + { + return new UserInfo(request.Name, request.Email, request.DateOfBirth); + } + + private bool UserExists(UserInfo info) + { + var user = _storage.GetUserInfo(info.Email); + + return user == info; + } + } +} diff --git a/QRBeeApi/appsettings.json b/QRBeeApi/appsettings.json index 10f68b8..f13cb12 100644 --- a/QRBeeApi/appsettings.json +++ b/QRBeeApi/appsettings.json @@ -1,4 +1,12 @@ { + + "QRBeeDatabase": { + "ConnectionString": "mongodb://localhost:27017", + "DatabaseName": "QRBee", + "User": "", + "Password": "" + }, + "Logging": { "LogLevel": { "Default": "Information",