From 6d3e4ee0a98112269f7c1d9817f5c7383f066d20 Mon Sep 17 00:00:00 2001 From: De-Hann Date: Fri, 26 Nov 2021 10:52:30 +0200 Subject: [PATCH 1/8] Application initial commit Added routes and most features relating to application. It will have to be updated when implementing database and authentication. Also moved DTO object into Hless.Data. --- .../Controllers/ApplicationController.cs | 55 +++++++++++ src/Hless.Api/Controllers/SchemaController.cs | 4 +- .../Extensions/Models/DtoExtensions.cs | 12 ++- .../Extensions/ServiceCollectionExtensions.cs | 1 + src/Hless.Api/Startup.cs | 2 + .../Repositories/IApplicationRepository.cs | 19 ++++ .../InMemoryApplicationRepository.cs | 91 +++++++++++++++++++ src/Hless.Data/Models/Application.cs | 18 ++++ src/Hless.Data/Models/Dto/ApplicationDto.cs | 20 ++++ .../Models/Dto/SchemaDto.cs | 2 +- src/Hless.Data/Models/Schema.cs | 7 ++ src/HlessApi.sln | 29 ++++-- 12 files changed, 250 insertions(+), 10 deletions(-) create mode 100644 src/Hless.Api/Controllers/ApplicationController.cs create mode 100644 src/Hless.Common/Repositories/IApplicationRepository.cs create mode 100644 src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs create mode 100644 src/Hless.Data/Models/Application.cs create mode 100644 src/Hless.Data/Models/Dto/ApplicationDto.cs rename src/{Hless.Api => Hless.Data}/Models/Dto/SchemaDto.cs (89%) diff --git a/src/Hless.Api/Controllers/ApplicationController.cs b/src/Hless.Api/Controllers/ApplicationController.cs new file mode 100644 index 0000000..653696a --- /dev/null +++ b/src/Hless.Api/Controllers/ApplicationController.cs @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Hless.Common.Repositories; +using Hless.Data.Models.Dto; +using Hless.Api.Extensions.Models; +using Hless.Data.Models; + +namespace Hless.Api.Controllers +{ + [Route("[controller]/[action]")] + public class ApplicationController : Controller + { + public IApplicationRepository _repository; + + public ApplicationController(IApplicationRepository repository) + { + _repository = repository; + } + + [HttpGet] + public async Task> GetApplicationsAsync() + { + var application = (await _repository.GetApplicationsAsync()) + .Select(app => app.AsDto()); + return application; + } + + [HttpPost] + public async void CreateApplicationAsync(ApplicationCreateDto application) + { + await _repository.CreateApplicationAsync(application); + } + + [HttpGet] + public async Task GetApplicationAsync(long applicationId) + { + return await _repository.GetApplicationAsync(applicationId); + } + + [HttpPut] + public async Task UpdateApplicationAsync(ApplicationDto application) + { + return await _repository.UpdateApplicationAsync(application); + } + + [HttpDelete] + public async Task DeleteApplicationAsync(long applicationId) + { + return await _repository.DeleteApplicationAsync(applicationId); + } + } +} diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index 7bde7c5..2e158ae 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -3,14 +3,14 @@ using System.Linq; using System.Threading.Tasks; using Hless.Api.Extensions.Models; -using Hless.Api.Models.Dto; +using Hless.Data.Models.Dto; using Hless.Common.Repositories; using Hless.Data.Models; using Microsoft.AspNetCore.Mvc; namespace Hless.Api.Controllers { - [Route("[controller]")] + [Route("[controller]/[action]")] public class SchemaController : BaseController { readonly ISchemaRepository _repository; diff --git a/src/Hless.Api/Extensions/Models/DtoExtensions.cs b/src/Hless.Api/Extensions/Models/DtoExtensions.cs index bede3f3..623ff10 100644 --- a/src/Hless.Api/Extensions/Models/DtoExtensions.cs +++ b/src/Hless.Api/Extensions/Models/DtoExtensions.cs @@ -1,4 +1,4 @@ -using Hless.Api.Models.Dto; +using Hless.Data.Models.Dto; using Hless.Data.Models; namespace Hless.Api.Extensions.Models @@ -16,5 +16,15 @@ public static SchemaDto AsDto(this Schema schema) SchemaId = schema.SchemaId }; } + + public static ApplicationDto AsDto(this Application application) + { + return new ApplicationDto + { + ApplicationId = application.ApplicationId, + OwnerId = application.OwnerId, + Name = application.Name, + }; + } } } \ No newline at end of file diff --git a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs index 6b902b9..a905960 100644 --- a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs @@ -9,6 +9,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddDependencies(this IServiceCollection services) { services.AddSingleton(); + services.AddSingleton(); return services; } diff --git a/src/Hless.Api/Startup.cs b/src/Hless.Api/Startup.cs index 07cbe40..0e37d4e 100644 --- a/src/Hless.Api/Startup.cs +++ b/src/Hless.Api/Startup.cs @@ -37,6 +37,8 @@ public void ConfigureServices(IServiceCollection services) var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = System.IO.Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); + + c.ResolveConflictingActions(ApiDescriptions => ApiDescriptions.First()); }); } diff --git a/src/Hless.Common/Repositories/IApplicationRepository.cs b/src/Hless.Common/Repositories/IApplicationRepository.cs new file mode 100644 index 0000000..71fbe21 --- /dev/null +++ b/src/Hless.Common/Repositories/IApplicationRepository.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hless.Data.Models.Dto; +using Hless.Data.Models; + +namespace Hless.Common.Repositories +{ + public interface IApplicationRepository + { + Task> GetApplicationsAsync(); + Task GetApplicationAsync(long applicationId); + Task CreateApplicationAsync(ApplicationCreateDto application); + Task UpdateApplicationAsync(ApplicationDto application); + Task DeleteApplicationAsync(long applicationId); + } +} diff --git a/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs new file mode 100644 index 0000000..494fe99 --- /dev/null +++ b/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs @@ -0,0 +1,91 @@ +using Hless.Common.Repositories; +using Hless.Data.Models; +using Hless.Data.Models.Dto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hless.Data.InMemory.Repositories +{ + public class InMemoryApplicationRepository : IApplicationRepository + { + private readonly List applications = new() + { + new Application + { + ApplicationId = 0, + Name = "FirstApp", + OwnerId = "ownerId here", + CreatedBy = "creatorId here", + CreatedAt = new DateTime(01, 02, 03, 04, 05, 06), + LastModified = new DateTime(01, 02, 03, 04, 05, 06) + } + }; + + public async Task CreateApplicationAsync(ApplicationCreateDto app) + { + await Task.Run(() => + { + Application application = new Application() + { + ApplicationId = applications.Count, + Name = app.Name, + OwnerId = app.OwnerId, + CreatedBy = "CreatedBy", // TODO: Change to logged in user + CreatedAt = DateTime.Now, + LastModified = DateTime.Now, + }; + + applications.Add(application); + }); + } + + public async Task DeleteApplicationAsync(long applicationId) + { + return await Task.Run(() => applications.Remove(applications.SingleOrDefault(app => app.ApplicationId == applicationId))); + } + + public async Task GetApplicationAsync(long applicationId) + { + return await Task.Run(() => applications.Where(app => app.ApplicationId == applicationId).SingleOrDefault()); + } + + public async Task> GetApplicationsAsync() + { + return await Task.FromResult(applications); + } + + public async Task UpdateApplicationAsync(ApplicationDto application) + { + return await Task.Run(() => + { + Application oldApp = applications.SingleOrDefault(app => app.ApplicationId == application.ApplicationId); + + if (oldApp != null) + { + Application newApp = new Application() + { + ApplicationId = oldApp.ApplicationId, + Name = application.Name, + OwnerId = application.OwnerId, + CreatedBy = oldApp.CreatedBy, // TODO: Change to logged in user + CreatedAt = oldApp.CreatedAt, + LastModified = DateTime.Now, + }; + + var indexOldApp = applications.IndexOf(oldApp); + + applications[indexOldApp] = newApp; + + return true; + } + else + { + return false; + } + }); + } + } +} diff --git a/src/Hless.Data/Models/Application.cs b/src/Hless.Data/Models/Application.cs new file mode 100644 index 0000000..0ee35f5 --- /dev/null +++ b/src/Hless.Data/Models/Application.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hless.Data.Models +{ + public record Application + { + public long ApplicationId { get; init; } + public string Name { get; init; } + public string OwnerId { get; init; } + public string CreatedBy { get; init; } + public DateTime CreatedAt { get; init; } + public DateTime LastModified { get; init; } + } +} diff --git a/src/Hless.Data/Models/Dto/ApplicationDto.cs b/src/Hless.Data/Models/Dto/ApplicationDto.cs new file mode 100644 index 0000000..7b668fe --- /dev/null +++ b/src/Hless.Data/Models/Dto/ApplicationDto.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Hless.Data.Models.Dto +{ + public record ApplicationDto + { + public long ApplicationId { get; init; } + public string Name { get; init; } + public string OwnerId { get; init; } + } + + public record ApplicationCreateDto + { + public string Name { get; init; } + public string OwnerId { get; init; } + } +} diff --git a/src/Hless.Api/Models/Dto/SchemaDto.cs b/src/Hless.Data/Models/Dto/SchemaDto.cs similarity index 89% rename from src/Hless.Api/Models/Dto/SchemaDto.cs rename to src/Hless.Data/Models/Dto/SchemaDto.cs index 57eded9..7bd7a29 100644 --- a/src/Hless.Api/Models/Dto/SchemaDto.cs +++ b/src/Hless.Data/Models/Dto/SchemaDto.cs @@ -1,4 +1,4 @@ -namespace Hless.Api.Models.Dto +namespace Hless.Data.Models.Dto { public record SchemaDto { diff --git a/src/Hless.Data/Models/Schema.cs b/src/Hless.Data/Models/Schema.cs index 9f07931..c10b285 100644 --- a/src/Hless.Data/Models/Schema.cs +++ b/src/Hless.Data/Models/Schema.cs @@ -1,3 +1,5 @@ +using System; + namespace Hless.Data.Models { public record Schema @@ -7,6 +9,11 @@ public record Schema public string Definition { get; init; } public string DraftDefinition { get; init; } public string CreatedBy { get; init; } + public DateTime CreatedAt { get; init; } + public DateTime LastModified { get; init; } + public DateTime FirstPublished { get; init; } + public DateTime LastPublished { get; init; } + public Application ApplicationId { get; init; } } diff --git a/src/HlessApi.sln b/src/HlessApi.sln index 419d738..881052e 100644 --- a/src/HlessApi.sln +++ b/src/HlessApi.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30114.105 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hless.Api", "Hless.Api\Hless.Api.csproj", "{688F5106-1613-49DA-BF73-BB29A25D8D4C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hless.Api", "Hless.Api\Hless.Api.csproj", "{688F5106-1613-49DA-BF73-BB29A25D8D4C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hless.Common", "Hless.Common\Hless.Common.csproj", "{0B960661-19F2-4CF6-9BD1-6A9D086AE1E7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hless.Common", "Hless.Common\Hless.Common.csproj", "{0B960661-19F2-4CF6-9BD1-6A9D086AE1E7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hless.Data", "Hless.Data\Hless.Data.csproj", "{06C30E8F-87FB-42C1-AFDC-3A39AA82AD66}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hless.Data", "Hless.Data\Hless.Data.csproj", "{06C30E8F-87FB-42C1-AFDC-3A39AA82AD66}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hless.Data.InMemory", "Hless.Data.InMemory\Hless.Data.InMemory.csproj", "{096D6545-66D0-4ABB-B4FF-0D284B65CE23}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -18,9 +20,6 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {688F5106-1613-49DA-BF73-BB29A25D8D4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {688F5106-1613-49DA-BF73-BB29A25D8D4C}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -58,5 +57,23 @@ Global {06C30E8F-87FB-42C1-AFDC-3A39AA82AD66}.Release|x64.Build.0 = Release|Any CPU {06C30E8F-87FB-42C1-AFDC-3A39AA82AD66}.Release|x86.ActiveCfg = Release|Any CPU {06C30E8F-87FB-42C1-AFDC-3A39AA82AD66}.Release|x86.Build.0 = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|x64.ActiveCfg = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|x64.Build.0 = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|x86.ActiveCfg = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Debug|x86.Build.0 = Debug|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|Any CPU.Build.0 = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|x64.ActiveCfg = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|x64.Build.0 = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|x86.ActiveCfg = Release|Any CPU + {096D6545-66D0-4ABB-B4FF-0D284B65CE23}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C122CA47-3D01-445C-B8D0-4E48E795E3B4} EndGlobalSection EndGlobal From e78eafb8395227d8a34a9deaf2b7c3e7367419f5 Mon Sep 17 00:00:00 2001 From: De-Hann Date: Fri, 26 Nov 2021 14:11:26 +0200 Subject: [PATCH 2/8] Reverted Dto move --- .../Controllers/ApplicationController.cs | 6 +++--- src/Hless.Api/Controllers/SchemaController.cs | 2 +- src/Hless.Api/Extensions/Models/DtoExtensions.cs | 2 +- .../Models/Dto/ApplicationDto.cs | 2 +- .../Models/Dto/SchemaDto.cs | 2 +- .../Repositories/IApplicationRepository.cs | 5 ++--- .../Repositories/InMemoryApplicationRepository.cs | 15 +++++++-------- 7 files changed, 16 insertions(+), 18 deletions(-) rename src/{Hless.Data => Hless.Api}/Models/Dto/ApplicationDto.cs (92%) rename src/{Hless.Data => Hless.Api}/Models/Dto/SchemaDto.cs (89%) diff --git a/src/Hless.Api/Controllers/ApplicationController.cs b/src/Hless.Api/Controllers/ApplicationController.cs index 653696a..bf8f04d 100644 --- a/src/Hless.Api/Controllers/ApplicationController.cs +++ b/src/Hless.Api/Controllers/ApplicationController.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Hless.Common.Repositories; -using Hless.Data.Models.Dto; +using Hless.Api.Models.Dto; using Hless.Api.Extensions.Models; using Hless.Data.Models; @@ -31,7 +31,7 @@ public async Task> GetApplicationsAsync() [HttpPost] public async void CreateApplicationAsync(ApplicationCreateDto application) { - await _repository.CreateApplicationAsync(application); + await _repository.CreateApplicationAsync(application.Name, application.OwnerId); } [HttpGet] @@ -43,7 +43,7 @@ public async Task GetApplicationAsync(long applicationId) [HttpPut] public async Task UpdateApplicationAsync(ApplicationDto application) { - return await _repository.UpdateApplicationAsync(application); + return await _repository.UpdateApplicationAsync(application.ApplicationId, application.Name, application.OwnerId); } [HttpDelete] diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index 2e158ae..51d76b5 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Hless.Api.Extensions.Models; -using Hless.Data.Models.Dto; +using Hless.Api.Models.Dto; using Hless.Common.Repositories; using Hless.Data.Models; using Microsoft.AspNetCore.Mvc; diff --git a/src/Hless.Api/Extensions/Models/DtoExtensions.cs b/src/Hless.Api/Extensions/Models/DtoExtensions.cs index 623ff10..22a5d4d 100644 --- a/src/Hless.Api/Extensions/Models/DtoExtensions.cs +++ b/src/Hless.Api/Extensions/Models/DtoExtensions.cs @@ -1,4 +1,4 @@ -using Hless.Data.Models.Dto; +using Hless.Api.Models.Dto; using Hless.Data.Models; namespace Hless.Api.Extensions.Models diff --git a/src/Hless.Data/Models/Dto/ApplicationDto.cs b/src/Hless.Api/Models/Dto/ApplicationDto.cs similarity index 92% rename from src/Hless.Data/Models/Dto/ApplicationDto.cs rename to src/Hless.Api/Models/Dto/ApplicationDto.cs index 7b668fe..9d9a7d4 100644 --- a/src/Hless.Data/Models/Dto/ApplicationDto.cs +++ b/src/Hless.Api/Models/Dto/ApplicationDto.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Threading.Tasks; -namespace Hless.Data.Models.Dto +namespace Hless.Api.Models.Dto { public record ApplicationDto { diff --git a/src/Hless.Data/Models/Dto/SchemaDto.cs b/src/Hless.Api/Models/Dto/SchemaDto.cs similarity index 89% rename from src/Hless.Data/Models/Dto/SchemaDto.cs rename to src/Hless.Api/Models/Dto/SchemaDto.cs index 7bd7a29..57eded9 100644 --- a/src/Hless.Data/Models/Dto/SchemaDto.cs +++ b/src/Hless.Api/Models/Dto/SchemaDto.cs @@ -1,4 +1,4 @@ -namespace Hless.Data.Models.Dto +namespace Hless.Api.Models.Dto { public record SchemaDto { diff --git a/src/Hless.Common/Repositories/IApplicationRepository.cs b/src/Hless.Common/Repositories/IApplicationRepository.cs index 71fbe21..9c735c0 100644 --- a/src/Hless.Common/Repositories/IApplicationRepository.cs +++ b/src/Hless.Common/Repositories/IApplicationRepository.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using Hless.Data.Models.Dto; using Hless.Data.Models; namespace Hless.Common.Repositories @@ -12,8 +11,8 @@ public interface IApplicationRepository { Task> GetApplicationsAsync(); Task GetApplicationAsync(long applicationId); - Task CreateApplicationAsync(ApplicationCreateDto application); - Task UpdateApplicationAsync(ApplicationDto application); + Task CreateApplicationAsync(string name, string OwnerId); + Task UpdateApplicationAsync(long applicationId, string name, string OwnerId); Task DeleteApplicationAsync(long applicationId); } } diff --git a/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs index 494fe99..7435f9b 100644 --- a/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs +++ b/src/Hless.Data.InMemory/Repositories/InMemoryApplicationRepository.cs @@ -1,6 +1,5 @@ using Hless.Common.Repositories; using Hless.Data.Models; -using Hless.Data.Models.Dto; using System; using System.Collections.Generic; using System.Linq; @@ -24,15 +23,15 @@ public class InMemoryApplicationRepository : IApplicationRepository } }; - public async Task CreateApplicationAsync(ApplicationCreateDto app) + public async Task CreateApplicationAsync(string name, string OwnerId) { await Task.Run(() => { Application application = new Application() { ApplicationId = applications.Count, - Name = app.Name, - OwnerId = app.OwnerId, + Name = name, + OwnerId = OwnerId, CreatedBy = "CreatedBy", // TODO: Change to logged in user CreatedAt = DateTime.Now, LastModified = DateTime.Now, @@ -57,19 +56,19 @@ public async Task> GetApplicationsAsync() return await Task.FromResult(applications); } - public async Task UpdateApplicationAsync(ApplicationDto application) + public async Task UpdateApplicationAsync(long applicationId, string name, string OwnerId) { return await Task.Run(() => { - Application oldApp = applications.SingleOrDefault(app => app.ApplicationId == application.ApplicationId); + Application oldApp = applications.SingleOrDefault(app => app.ApplicationId == applicationId); if (oldApp != null) { Application newApp = new Application() { ApplicationId = oldApp.ApplicationId, - Name = application.Name, - OwnerId = application.OwnerId, + Name = name, + OwnerId = OwnerId, CreatedBy = oldApp.CreatedBy, // TODO: Change to logged in user CreatedAt = oldApp.CreatedAt, LastModified = DateTime.Now, From 5d530e8499c4a367db52c6ca210ea8073285190e Mon Sep 17 00:00:00 2001 From: De-Hann Date: Fri, 26 Nov 2021 14:57:14 +0200 Subject: [PATCH 3/8] General fixes --- .../Controllers/ApplicationController.cs | 2 +- src/Hless.Api/Controllers/SchemaController.cs | 31 ++++++++++++++- src/Hless.Api/Models/Dto/SchemaDto.cs | 9 +++++ .../Repositories/ISchemaRepository.cs | 7 ++-- .../Repositories/InMemorySchemaRepository.cs | 39 +++++++++++++++++-- src/Hless.Data/Models/Schema.cs | 6 +-- 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/Hless.Api/Controllers/ApplicationController.cs b/src/Hless.Api/Controllers/ApplicationController.cs index bf8f04d..0909ddc 100644 --- a/src/Hless.Api/Controllers/ApplicationController.cs +++ b/src/Hless.Api/Controllers/ApplicationController.cs @@ -11,7 +11,7 @@ namespace Hless.Api.Controllers { [Route("[controller]/[action]")] - public class ApplicationController : Controller + public class ApplicationController : BaseController { public IApplicationRepository _repository; diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index 51d76b5..00056bf 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -18,7 +18,7 @@ public SchemaController(ISchemaRepository repository) { _repository = repository; } - + [HttpGet] public async Task> GetSchemasAsync() { @@ -26,5 +26,34 @@ public async Task> GetSchemasAsync() .Select(schema => schema.AsDto()); return schemas; } + [HttpGet] + public async Task GetSchemaAsync(long schemaId) + { + return await _repository.GetSchemaAsync(schemaId); + } + [HttpPost] + public async Task CreateSchemaAsync(SchemaCreateDto schema) + { + Schema newSchema = new Schema() + { + Name = schema.Name, + DraftDefinition = schema.DraftDefinition, + ApplicationId = schema.ApplicationId, + }; + + return await _repository.CreateSchemaAsync(newSchema); + } + //[HttpPut] + //public async Task UpdateSchemaAsync(Schema schema) + //{ + //} + //[HttpGet] + //public async Task PublishSchemaAsync(long schemaId) + //{ + //} + //[HttpDelete] + //public async Task DeleteSchemaAsync(long schemaId) + //{ + //} } } \ No newline at end of file diff --git a/src/Hless.Api/Models/Dto/SchemaDto.cs b/src/Hless.Api/Models/Dto/SchemaDto.cs index 57eded9..a26544c 100644 --- a/src/Hless.Api/Models/Dto/SchemaDto.cs +++ b/src/Hless.Api/Models/Dto/SchemaDto.cs @@ -1,3 +1,5 @@ +using Hless.Data.Models; + namespace Hless.Api.Models.Dto { public record SchemaDto @@ -7,6 +9,13 @@ public record SchemaDto public string Definition { get; init; } public string DraftDefinition { get; init; } public string CreatedBy { get; init; } + public long ApplicationId { get; init; } + } + public record SchemaCreateDto + { + public string Name { get; init; } + public string DraftDefinition { get; init; } + public long ApplicationId { get; init; } } } \ No newline at end of file diff --git a/src/Hless.Common/Repositories/ISchemaRepository.cs b/src/Hless.Common/Repositories/ISchemaRepository.cs index c61b021..4b6c7ea 100644 --- a/src/Hless.Common/Repositories/ISchemaRepository.cs +++ b/src/Hless.Common/Repositories/ISchemaRepository.cs @@ -8,8 +8,9 @@ public interface ISchemaRepository { Task> GetSchemasAsync(); Task GetSchemaAsync(long schemaId); - Task CreateSchemaAsync(Schema schema); - Task UpdateSchemaAsync(Schema schema); - Task PublishSchemaAsync(long schemaId); + Task CreateSchemaAsync(Schema schema); + Task UpdateSchemaAsync(Schema schema); + Task PublishSchemaAsync(long schemaId); + Task DeleteSchemaAsync(long schemaId); } } \ No newline at end of file diff --git a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs index 4701e37..eb0e50f 100644 --- a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs +++ b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -12,10 +13,40 @@ public class InMemorySchemaRepository : ISchemaRepository { private readonly List schemas = new() { - new Schema { SchemaId = 1, Name = "HomePage", Definition = "{\n\"fields\":[\n{\n\"name\":\"Title\",\n\"type\":\"text\"\n}\n]\n}" } + new Schema { SchemaId = 0, Name = "HomePage", Definition = "{\n\"fields\":[\n{\n\"name\":\"Title\",\n\"type\":\"text\"\n}\n]\n}" } }; - public Task CreateSchemaAsync(Schema schema) + public async Task CreateSchemaAsync(Schema schema) + { + return await Task.Run(() => + { + try + { + Schema newSchema = new Schema() + { + SchemaId = schemas.Count, + Name = schema.Name, + Definition = "", + DraftDefinition = schema.DraftDefinition, + CreatedBy = "CreatedBy", // Update with logged in user + CreatedAt = DateTime.Now, + LastModified = DateTime.Now, + FirstPublished = null, + LastPublished = null, + ApplicationId = schema.ApplicationId, + }; + schemas.Add(newSchema); + + return newSchema; + } + catch + { + return null; + } + }); + } + + public Task DeleteSchemaAsync(long schemaId) { throw new System.NotImplementedException(); } @@ -31,12 +62,12 @@ public async Task> GetSchemasAsync() return await Task.FromResult(schemas); } - public Task PublishSchemaAsync(long schemaId) + public Task PublishSchemaAsync(long schemaId) { throw new System.NotImplementedException(); } - public Task UpdateSchemaAsync(Schema schema) + public Task UpdateSchemaAsync(Schema schema) { throw new System.NotImplementedException(); } diff --git a/src/Hless.Data/Models/Schema.cs b/src/Hless.Data/Models/Schema.cs index c10b285..2493fa8 100644 --- a/src/Hless.Data/Models/Schema.cs +++ b/src/Hless.Data/Models/Schema.cs @@ -11,9 +11,9 @@ public record Schema public string CreatedBy { get; init; } public DateTime CreatedAt { get; init; } public DateTime LastModified { get; init; } - public DateTime FirstPublished { get; init; } - public DateTime LastPublished { get; init; } - public Application ApplicationId { get; init; } + public DateTime? FirstPublished { get; init; } + public DateTime? LastPublished { get; init; } + public long ApplicationId { get; init; } } From 819ab6d358b14b04d32e4a35078315e6626940c3 Mon Sep 17 00:00:00 2001 From: De-Hann Date: Mon, 29 Nov 2021 14:20:13 +0200 Subject: [PATCH 4/8] Started implementing Content classes --- .../Controllers/ContentController.cs | 15 ++ src/Hless.Api/Controllers/SchemaController.cs | 27 ++-- .../Extensions/ServiceCollectionExtensions.cs | 2 +- src/Hless.Api/Models/Dto/ContentDto.cs | 11 ++ .../Repositories/IContentRepository.cs | 20 +++ .../Repositories/InMemoryContentRepository.cs | 135 ++++++++++++++++++ .../Repositories/InMemorySchemaRepository.cs | 3 +- src/Hless.Data/Models/Content.cs | 21 +++ 8 files changed, 220 insertions(+), 14 deletions(-) create mode 100644 src/Hless.Api/Controllers/ContentController.cs create mode 100644 src/Hless.Api/Models/Dto/ContentDto.cs create mode 100644 src/Hless.Common/Repositories/IContentRepository.cs create mode 100644 src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs create mode 100644 src/Hless.Data/Models/Content.cs diff --git a/src/Hless.Api/Controllers/ContentController.cs b/src/Hless.Api/Controllers/ContentController.cs new file mode 100644 index 0000000..b5d4d8e --- /dev/null +++ b/src/Hless.Api/Controllers/ContentController.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Hless.Api.Controllers +{ + [Route("[controller]/[action]")] + [ApiController] + public class ContentController : BaseController + { + } +} diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index 00056bf..a386cc9 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -43,17 +43,20 @@ public async Task CreateSchemaAsync(SchemaCreateDto schema) return await _repository.CreateSchemaAsync(newSchema); } - //[HttpPut] - //public async Task UpdateSchemaAsync(Schema schema) - //{ - //} - //[HttpGet] - //public async Task PublishSchemaAsync(long schemaId) - //{ - //} - //[HttpDelete] - //public async Task DeleteSchemaAsync(long schemaId) - //{ - //} + [HttpPut] + public async Task UpdateSchemaAsync(Schema schema) + { + return await _repository.UpdateSchemaAsync(schema); + } + [HttpGet] + public async Task PublishSchemaAsync(long schemaId) + { + return await _repository.PublishSchemaAsync(schemaId); + } + [HttpDelete] + public async Task DeleteSchemaAsync(long schemaId) + { + return await _repository.DeleteSchemaAsync(schemaId); + } } } \ No newline at end of file diff --git a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs index a905960..162958f 100644 --- a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs @@ -10,9 +10,9 @@ public static IServiceCollection AddDependencies(this IServiceCollection service { services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); return services; } - } } \ No newline at end of file diff --git a/src/Hless.Api/Models/Dto/ContentDto.cs b/src/Hless.Api/Models/Dto/ContentDto.cs new file mode 100644 index 0000000..62d494e --- /dev/null +++ b/src/Hless.Api/Models/Dto/ContentDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Hless.Api.Models.Dto +{ + public record ContentDto + { + } +} diff --git a/src/Hless.Common/Repositories/IContentRepository.cs b/src/Hless.Common/Repositories/IContentRepository.cs new file mode 100644 index 0000000..ee75283 --- /dev/null +++ b/src/Hless.Common/Repositories/IContentRepository.cs @@ -0,0 +1,20 @@ +using Hless.Data.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hless.Common.Repositories +{ + public interface IContentRepository + { + Task> GetContentsAsync(); + Task> GetContentsAsync(long schemaId); + Task GetContentAsync(long contentId); + Task CreateContentAsync(Content content); + Task UpdateContentAsync(Content content); + Task PublishContentAsync(long contentId); + Task DeleteContentAsync(long contentId); + } +} diff --git a/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs new file mode 100644 index 0000000..670c253 --- /dev/null +++ b/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hless.Common.Repositories; +using Hless.Data.Models; + +namespace Hless.Data.InMemory.Repositories +{ + public class InMemoryContentRepository : IContentRepository + { + private readonly List lstContent = new() + { + new Content + { + ContentId = 0, + ContentFinal = "{}", + DraftContent = "", + CreatedBy = "UserId", + CreatedAt = new DateTime(1, 2, 3, 4, 5, 6), + LastModified = new DateTime(2, 3, 4, 5, 6, 7), + FirstPublished = new DateTime(1, 2, 3, 4, 5, 6), + LastPublished = new DateTime(2, 3, 4, 5, 6, 6), + SchemaId = 0 + } + }; + + public async Task CreateContentAsync(Content content) + { + return await Task.Run(() => + { + try + { + Content newContent = new Content() + { + ContentId = lstContent.Count, + ContentFinal = "{}", + DraftContent = content.DraftContent, + CreatedBy = "UserId", //Updated with logged in user + CreatedAt = DateTime.Now, + LastModified = DateTime.Now, + FirstPublished = null, + LastPublished = null, + SchemaId = 0 + }; + lstContent.Add(newContent); + + return newContent; + } + catch + { + return null; + } + }); + } + + public async Task DeleteContentAsync(long contentId) + { + return await Task.Run(() => + { + return lstContent.Remove(lstContent.Find(c => c.ContentId == contentId)); + }); + } + + public async Task GetContentAsync(long contentId) + { + return await Task.Run(() => lstContent.Find(c => c.ContentId == contentId)); + } + + public async Task> GetContentsAsync() + { + return await Task.FromResult(lstContent); + } + + public async Task> GetContentsAsync(long schemaId) + { + return await Task.FromResult(lstContent.Where(x => x.SchemaId == schemaId)); + } + + public async Task PublishContentAsync(long contentId) + { + return await Task.Run(() => + { + try + { + int index = lstContent.FindIndex(x => x.ContentId == contentId); + + Content newContent = new Content() + { + ContentId = lstContent[index].ContentId, + ContentFinal = lstContent[index].DraftContent, + DraftContent = "", + CreatedBy = lstContent[index].CreatedBy, + CreatedAt = lstContent[index].CreatedAt, + LastModified = DateTime.Now, + FirstPublished = lstContent[index].FirstPublished == null ? lstContent[index].FirstPublished : DateTime.Now, + LastPublished = DateTime.Now, + SchemaId = 0 + }; + + lstContent[index] = newContent; + + return true; + } + catch + { + return false; + } + }); + } + + + public async Task UpdateContentAsync(Content content) + { + return await Task.Run(() => + { + try + { + int i = lstContent.FindIndex(c => c.ContentId == content.ContentId); + lstContent[i].DraftContent = content.DraftContent; + lstContent[i].LastModified = DateTime.Now; + + return true; + } + catch + { + return false; + } + }); + } + + } +} + diff --git a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs index eb0e50f..f0b1ec2 100644 --- a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs +++ b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs @@ -48,7 +48,8 @@ public async Task CreateSchemaAsync(Schema schema) public Task DeleteSchemaAsync(long schemaId) { - throw new System.NotImplementedException(); + + return Task.Run(() => schemas.Remove(schemas.Find(s => s.SchemaId == schemaId))); } public async Task GetSchemaAsync(long schemaId) diff --git a/src/Hless.Data/Models/Content.cs b/src/Hless.Data/Models/Content.cs new file mode 100644 index 0000000..adc3d80 --- /dev/null +++ b/src/Hless.Data/Models/Content.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hless.Data.Models +{ + public record Content + { + public long ContentId { get; init; } + public string ContentFinal { get; set; } + public string DraftContent { get; set; } + public string CreatedBy { get; init; } + public DateTime CreatedAt { get; init; } + public DateTime LastModified { get; set; } + public DateTime? FirstPublished { get; init; } + public DateTime? LastPublished { get; init; } + public long SchemaId { get; init; } + } +} From 275bf192507818a300cce46ce5a76000473924be Mon Sep 17 00:00:00 2001 From: De-Hann Date: Thu, 2 Dec 2021 13:27:05 +0200 Subject: [PATCH 5/8] Content --- src/Hless.Api/Controllers/ContentController.cs | 16 ++++++++++++++-- .../Repositories/InMemorySchemaRepository.cs | 10 ++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Hless.Api/Controllers/ContentController.cs b/src/Hless.Api/Controllers/ContentController.cs index b5d4d8e..87d227c 100644 --- a/src/Hless.Api/Controllers/ContentController.cs +++ b/src/Hless.Api/Controllers/ContentController.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Http; +using Hless.Common.Repositories; +using Hless.Data.Models; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; @@ -8,8 +10,18 @@ namespace Hless.Api.Controllers { [Route("[controller]/[action]")] - [ApiController] public class ContentController : BaseController { + private IContentRepository _reposistory; + public ContentController(IContentRepository repository) + { + _reposistory = repository; + } + + [HttpGet] + public async Task> GetContentsAsync() + { + return await _reposistory.GetContentsAsync(); + } } } diff --git a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs index f0b1ec2..cf57e54 100644 --- a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs +++ b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs @@ -13,7 +13,11 @@ public class InMemorySchemaRepository : ISchemaRepository { private readonly List schemas = new() { - new Schema { SchemaId = 0, Name = "HomePage", Definition = "{\n\"fields\":[\n{\n\"name\":\"Title\",\n\"type\":\"text\"\n}\n]\n}" } + new Schema { + SchemaId = 0, + Name = "HomePage", + Definition = "{\n\"fields\":[\n{\n\"name\":\"Title\",\n\"type\":\"text\"\n}\n]\n}" + } }; public async Task CreateSchemaAsync(Schema schema) @@ -22,11 +26,13 @@ public async Task CreateSchemaAsync(Schema schema) { try { + + Schema newSchema = new Schema() { SchemaId = schemas.Count, Name = schema.Name, - Definition = "", + Definition = null, DraftDefinition = schema.DraftDefinition, CreatedBy = "CreatedBy", // Update with logged in user CreatedAt = DateTime.Now, From 834098f134830dbc282bd199092e43fd8bbafb4e Mon Sep 17 00:00:00 2001 From: De-Hann Date: Tue, 7 Dec 2021 16:31:34 +0200 Subject: [PATCH 6/8] Reverted schema and content --- .../Controllers/ContentController.cs | 27 ---- src/Hless.Api/Controllers/SchemaController.cs | 32 ----- .../Extensions/ServiceCollectionExtensions.cs | 1 - src/Hless.Api/Models/Dto/ContentDto.cs | 11 -- src/Hless.Api/Models/Dto/SchemaDto.cs | 7 - .../Repositories/IContentRepository.cs | 20 --- .../Repositories/InMemoryContentRepository.cs | 135 ------------------ src/Hless.Data/Models/Content.cs | 21 --- 8 files changed, 254 deletions(-) delete mode 100644 src/Hless.Api/Controllers/ContentController.cs delete mode 100644 src/Hless.Api/Models/Dto/ContentDto.cs delete mode 100644 src/Hless.Common/Repositories/IContentRepository.cs delete mode 100644 src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs delete mode 100644 src/Hless.Data/Models/Content.cs diff --git a/src/Hless.Api/Controllers/ContentController.cs b/src/Hless.Api/Controllers/ContentController.cs deleted file mode 100644 index 87d227c..0000000 --- a/src/Hless.Api/Controllers/ContentController.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Hless.Common.Repositories; -using Hless.Data.Models; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Hless.Api.Controllers -{ - [Route("[controller]/[action]")] - public class ContentController : BaseController - { - private IContentRepository _reposistory; - public ContentController(IContentRepository repository) - { - _reposistory = repository; - } - - [HttpGet] - public async Task> GetContentsAsync() - { - return await _reposistory.GetContentsAsync(); - } - } -} diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index a386cc9..e391f73 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -26,37 +26,5 @@ public async Task> GetSchemasAsync() .Select(schema => schema.AsDto()); return schemas; } - [HttpGet] - public async Task GetSchemaAsync(long schemaId) - { - return await _repository.GetSchemaAsync(schemaId); - } - [HttpPost] - public async Task CreateSchemaAsync(SchemaCreateDto schema) - { - Schema newSchema = new Schema() - { - Name = schema.Name, - DraftDefinition = schema.DraftDefinition, - ApplicationId = schema.ApplicationId, - }; - - return await _repository.CreateSchemaAsync(newSchema); - } - [HttpPut] - public async Task UpdateSchemaAsync(Schema schema) - { - return await _repository.UpdateSchemaAsync(schema); - } - [HttpGet] - public async Task PublishSchemaAsync(long schemaId) - { - return await _repository.PublishSchemaAsync(schemaId); - } - [HttpDelete] - public async Task DeleteSchemaAsync(long schemaId) - { - return await _repository.DeleteSchemaAsync(schemaId); - } } } \ No newline at end of file diff --git a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs index 162958f..9d7285a 100644 --- a/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/Hless.Api/Extensions/ServiceCollectionExtensions.cs @@ -10,7 +10,6 @@ public static IServiceCollection AddDependencies(this IServiceCollection service { services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); return services; } } diff --git a/src/Hless.Api/Models/Dto/ContentDto.cs b/src/Hless.Api/Models/Dto/ContentDto.cs deleted file mode 100644 index 62d494e..0000000 --- a/src/Hless.Api/Models/Dto/ContentDto.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Hless.Api.Models.Dto -{ - public record ContentDto - { - } -} diff --git a/src/Hless.Api/Models/Dto/SchemaDto.cs b/src/Hless.Api/Models/Dto/SchemaDto.cs index a26544c..541ebc1 100644 --- a/src/Hless.Api/Models/Dto/SchemaDto.cs +++ b/src/Hless.Api/Models/Dto/SchemaDto.cs @@ -11,11 +11,4 @@ public record SchemaDto public string CreatedBy { get; init; } public long ApplicationId { get; init; } } - - public record SchemaCreateDto - { - public string Name { get; init; } - public string DraftDefinition { get; init; } - public long ApplicationId { get; init; } - } } \ No newline at end of file diff --git a/src/Hless.Common/Repositories/IContentRepository.cs b/src/Hless.Common/Repositories/IContentRepository.cs deleted file mode 100644 index ee75283..0000000 --- a/src/Hless.Common/Repositories/IContentRepository.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Hless.Data.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Hless.Common.Repositories -{ - public interface IContentRepository - { - Task> GetContentsAsync(); - Task> GetContentsAsync(long schemaId); - Task GetContentAsync(long contentId); - Task CreateContentAsync(Content content); - Task UpdateContentAsync(Content content); - Task PublishContentAsync(long contentId); - Task DeleteContentAsync(long contentId); - } -} diff --git a/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs deleted file mode 100644 index 670c253..0000000 --- a/src/Hless.Data.InMemory/Repositories/InMemoryContentRepository.cs +++ /dev/null @@ -1,135 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Hless.Common.Repositories; -using Hless.Data.Models; - -namespace Hless.Data.InMemory.Repositories -{ - public class InMemoryContentRepository : IContentRepository - { - private readonly List lstContent = new() - { - new Content - { - ContentId = 0, - ContentFinal = "{}", - DraftContent = "", - CreatedBy = "UserId", - CreatedAt = new DateTime(1, 2, 3, 4, 5, 6), - LastModified = new DateTime(2, 3, 4, 5, 6, 7), - FirstPublished = new DateTime(1, 2, 3, 4, 5, 6), - LastPublished = new DateTime(2, 3, 4, 5, 6, 6), - SchemaId = 0 - } - }; - - public async Task CreateContentAsync(Content content) - { - return await Task.Run(() => - { - try - { - Content newContent = new Content() - { - ContentId = lstContent.Count, - ContentFinal = "{}", - DraftContent = content.DraftContent, - CreatedBy = "UserId", //Updated with logged in user - CreatedAt = DateTime.Now, - LastModified = DateTime.Now, - FirstPublished = null, - LastPublished = null, - SchemaId = 0 - }; - lstContent.Add(newContent); - - return newContent; - } - catch - { - return null; - } - }); - } - - public async Task DeleteContentAsync(long contentId) - { - return await Task.Run(() => - { - return lstContent.Remove(lstContent.Find(c => c.ContentId == contentId)); - }); - } - - public async Task GetContentAsync(long contentId) - { - return await Task.Run(() => lstContent.Find(c => c.ContentId == contentId)); - } - - public async Task> GetContentsAsync() - { - return await Task.FromResult(lstContent); - } - - public async Task> GetContentsAsync(long schemaId) - { - return await Task.FromResult(lstContent.Where(x => x.SchemaId == schemaId)); - } - - public async Task PublishContentAsync(long contentId) - { - return await Task.Run(() => - { - try - { - int index = lstContent.FindIndex(x => x.ContentId == contentId); - - Content newContent = new Content() - { - ContentId = lstContent[index].ContentId, - ContentFinal = lstContent[index].DraftContent, - DraftContent = "", - CreatedBy = lstContent[index].CreatedBy, - CreatedAt = lstContent[index].CreatedAt, - LastModified = DateTime.Now, - FirstPublished = lstContent[index].FirstPublished == null ? lstContent[index].FirstPublished : DateTime.Now, - LastPublished = DateTime.Now, - SchemaId = 0 - }; - - lstContent[index] = newContent; - - return true; - } - catch - { - return false; - } - }); - } - - - public async Task UpdateContentAsync(Content content) - { - return await Task.Run(() => - { - try - { - int i = lstContent.FindIndex(c => c.ContentId == content.ContentId); - lstContent[i].DraftContent = content.DraftContent; - lstContent[i].LastModified = DateTime.Now; - - return true; - } - catch - { - return false; - } - }); - } - - } -} - diff --git a/src/Hless.Data/Models/Content.cs b/src/Hless.Data/Models/Content.cs deleted file mode 100644 index adc3d80..0000000 --- a/src/Hless.Data/Models/Content.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Hless.Data.Models -{ - public record Content - { - public long ContentId { get; init; } - public string ContentFinal { get; set; } - public string DraftContent { get; set; } - public string CreatedBy { get; init; } - public DateTime CreatedAt { get; init; } - public DateTime LastModified { get; set; } - public DateTime? FirstPublished { get; init; } - public DateTime? LastPublished { get; init; } - public long SchemaId { get; init; } - } -} From 4c7d937bdd9256a89bc52bd7a7c21e2cb2b39138 Mon Sep 17 00:00:00 2001 From: De-Hann Date: Tue, 7 Dec 2021 16:33:57 +0200 Subject: [PATCH 7/8] Started Schema --- src/Hless.Api/Controllers/SchemaController.cs | 32 +++++++++++++++++++ src/Hless.Api/Models/Dto/SchemaDto.cs | 7 ++++ 2 files changed, 39 insertions(+) diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index e391f73..a386cc9 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -26,5 +26,37 @@ public async Task> GetSchemasAsync() .Select(schema => schema.AsDto()); return schemas; } + [HttpGet] + public async Task GetSchemaAsync(long schemaId) + { + return await _repository.GetSchemaAsync(schemaId); + } + [HttpPost] + public async Task CreateSchemaAsync(SchemaCreateDto schema) + { + Schema newSchema = new Schema() + { + Name = schema.Name, + DraftDefinition = schema.DraftDefinition, + ApplicationId = schema.ApplicationId, + }; + + return await _repository.CreateSchemaAsync(newSchema); + } + [HttpPut] + public async Task UpdateSchemaAsync(Schema schema) + { + return await _repository.UpdateSchemaAsync(schema); + } + [HttpGet] + public async Task PublishSchemaAsync(long schemaId) + { + return await _repository.PublishSchemaAsync(schemaId); + } + [HttpDelete] + public async Task DeleteSchemaAsync(long schemaId) + { + return await _repository.DeleteSchemaAsync(schemaId); + } } } \ No newline at end of file diff --git a/src/Hless.Api/Models/Dto/SchemaDto.cs b/src/Hless.Api/Models/Dto/SchemaDto.cs index 541ebc1..a26544c 100644 --- a/src/Hless.Api/Models/Dto/SchemaDto.cs +++ b/src/Hless.Api/Models/Dto/SchemaDto.cs @@ -11,4 +11,11 @@ public record SchemaDto public string CreatedBy { get; init; } public long ApplicationId { get; init; } } + + public record SchemaCreateDto + { + public string Name { get; init; } + public string DraftDefinition { get; init; } + public long ApplicationId { get; init; } + } } \ No newline at end of file From a62b3eb9f2f5ce96ec0fb4e5c73e1aa8a2d7d02e Mon Sep 17 00:00:00 2001 From: De-Hann Date: Tue, 14 Dec 2021 12:13:24 +0200 Subject: [PATCH 8/8] Converted DraftDefinition and Definition to Dictionaries and finished SchemaInMemory Class --- src/Hless.Api/Controllers/SchemaController.cs | 7 +- src/Hless.Api/Models/Dto/SchemaDto.cs | 8 +- .../Repositories/InMemorySchemaRepository.cs | 80 ++++++++++++++++--- src/Hless.Data/Models/Schema.cs | 15 ++-- 4 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/Hless.Api/Controllers/SchemaController.cs b/src/Hless.Api/Controllers/SchemaController.cs index a386cc9..c1a3ca3 100644 --- a/src/Hless.Api/Controllers/SchemaController.cs +++ b/src/Hless.Api/Controllers/SchemaController.cs @@ -44,7 +44,7 @@ public async Task CreateSchemaAsync(SchemaCreateDto schema) return await _repository.CreateSchemaAsync(newSchema); } [HttpPut] - public async Task UpdateSchemaAsync(Schema schema) + public async Task UpdateSchemaAsync([FromBody] Schema schema) { return await _repository.UpdateSchemaAsync(schema); } @@ -58,5 +58,10 @@ public async Task DeleteSchemaAsync(long schemaId) { return await _repository.DeleteSchemaAsync(schemaId); } + [HttpGet] + public async Task Test([FromBody] Schema schema) + { + return await Task.FromResult(schema); + } } } \ No newline at end of file diff --git a/src/Hless.Api/Models/Dto/SchemaDto.cs b/src/Hless.Api/Models/Dto/SchemaDto.cs index a26544c..a8b9023 100644 --- a/src/Hless.Api/Models/Dto/SchemaDto.cs +++ b/src/Hless.Api/Models/Dto/SchemaDto.cs @@ -1,4 +1,6 @@ using Hless.Data.Models; +using System; +using System.Collections.Generic; namespace Hless.Api.Models.Dto { @@ -6,8 +8,8 @@ public record SchemaDto { public long SchemaId { get; init; } public string Name { get; init; } - public string Definition { get; init; } - public string DraftDefinition { get; init; } + public Dictionary Definition { get; init; } + public Dictionary DraftDefinition { get; init; } public string CreatedBy { get; init; } public long ApplicationId { get; init; } } @@ -15,7 +17,7 @@ public record SchemaDto public record SchemaCreateDto { public string Name { get; init; } - public string DraftDefinition { get; init; } + public Dictionary DraftDefinition { get; init; } public long ApplicationId { get; init; } } } \ No newline at end of file diff --git a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs index cf57e54..2df2648 100644 --- a/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs +++ b/src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs @@ -14,9 +14,29 @@ public class InMemorySchemaRepository : ISchemaRepository private readonly List schemas = new() { new Schema { - SchemaId = 0, - Name = "HomePage", - Definition = "{\n\"fields\":[\n{\n\"name\":\"Title\",\n\"type\":\"text\"\n}\n]\n}" + SchemaId = 1, + Name = "Users", + DraftDefinition = new Dictionary { + { "Id", "number" }, + { "Title", "text" }, + { "Firstname", "text" }, + { "Surname", "text" } + }, + CreatedBy = "Admin", + CreatedAt = DateTime.Now, + }, + new Schema + { + SchemaId = 2, + Name = "Car", + DraftDefinition = new Dictionary { + { "Id", "number" }, + { "UsersId", "number" }, + { "Name", "text" }, + { "Licence", "text" } + }, + CreatedBy = "Admin", + CreatedAt = DateTime.Now, } }; @@ -26,11 +46,9 @@ public async Task CreateSchemaAsync(Schema schema) { try { - - Schema newSchema = new Schema() { - SchemaId = schemas.Count, + SchemaId = schemas.Count + 1, Name = schema.Name, Definition = null, DraftDefinition = schema.DraftDefinition, @@ -54,13 +72,12 @@ public async Task CreateSchemaAsync(Schema schema) public Task DeleteSchemaAsync(long schemaId) { - return Task.Run(() => schemas.Remove(schemas.Find(s => s.SchemaId == schemaId))); } public async Task GetSchemaAsync(long schemaId) { - var schema = schemas.Where(schema => schema.SchemaId == schemaId).SingleOrDefault(); + var schema = schemas.Find(schema => schema.SchemaId == schemaId); return await Task.FromResult(schema); } @@ -71,12 +88,55 @@ public async Task> GetSchemasAsync() public Task PublishSchemaAsync(long schemaId) { - throw new System.NotImplementedException(); + return Task.Run(() => { + try + { + int i = schemas.IndexOf(schemas.Find(s => s.SchemaId == schemaId)); + + if (i == -1) + return false; + + schemas[i].Definition = schemas[i].DraftDefinition; + schemas[i].LastPublished = DateTime.Now; + + if (schemas[i].FirstPublished == null) + schemas[i].FirstPublished = schemas[i].LastPublished; + + return true; + } + catch { return false; } + }); } public Task UpdateSchemaAsync(Schema schema) { - throw new System.NotImplementedException(); + return Task.Run(() => + { + try + { + if(schema.SchemaId == 0) + return false; + + int i = schemas.IndexOf(schemas.Find(s => s.SchemaId == schema.SchemaId)); + + if (i == -1) + return false; + + if(schema.Name != null) + schemas[i].Name = schema.Name; + + if(schema.DraftDefinition != null) + schemas[i].DraftDefinition = schema.DraftDefinition; + + if(schema.ApplicationId != 0) + schemas[i].ApplicationId = schema.ApplicationId; + + schemas[i].LastModified = DateTime.Now; + + return true; + } + catch { return false; } + }); } } } \ No newline at end of file diff --git a/src/Hless.Data/Models/Schema.cs b/src/Hless.Data/Models/Schema.cs index 2493fa8..8d28acf 100644 --- a/src/Hless.Data/Models/Schema.cs +++ b/src/Hless.Data/Models/Schema.cs @@ -1,19 +1,20 @@ using System; +using System.Collections.Generic; namespace Hless.Data.Models { public record Schema { public long SchemaId { get; init; } - public string Name { get; init; } - public string Definition { get; init; } - public string DraftDefinition { get; init; } + public string Name { get; set; } + public Dictionary Definition { get; set; } + public Dictionary DraftDefinition { get; set; } public string CreatedBy { get; init; } public DateTime CreatedAt { get; init; } - public DateTime LastModified { get; init; } - public DateTime? FirstPublished { get; init; } - public DateTime? LastPublished { get; init; } - public long ApplicationId { get; init; } + public DateTime LastModified { get; set; } + public DateTime? FirstPublished { get; set; } + public DateTime? LastPublished { get; set; } + public long ApplicationId { get; set; } }