Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Hless.Api/Controllers/ApplicationController.cs
Original file line number Diff line number Diff line change
@@ -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.Api.Models.Dto;
using Hless.Api.Extensions.Models;
using Hless.Data.Models;

namespace Hless.Api.Controllers
{
[Route("[controller]/[action]")]
public class ApplicationController : BaseController
{
public IApplicationRepository _repository;

public ApplicationController(IApplicationRepository repository)
{
_repository = repository;
}

[HttpGet]
public async Task<IEnumerable<ApplicationDto>> GetApplicationsAsync()
{
var application = (await _repository.GetApplicationsAsync())
.Select(app => app.AsDto());
return application;
}

[HttpPost]
public async void CreateApplicationAsync(ApplicationCreateDto application)
{
await _repository.CreateApplicationAsync(application.Name, application.OwnerId);
}

[HttpGet]
public async Task<Application> GetApplicationAsync(long applicationId)
{
return await _repository.GetApplicationAsync(applicationId);
}

[HttpPut]
public async Task<bool> UpdateApplicationAsync(ApplicationDto application)
{
return await _repository.UpdateApplicationAsync(application.ApplicationId, application.Name, application.OwnerId);
}

[HttpDelete]
public async Task<bool> DeleteApplicationAsync(long applicationId)
{
return await _repository.DeleteApplicationAsync(applicationId);
}
}
}
41 changes: 39 additions & 2 deletions src/Hless.Api/Controllers/SchemaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,58 @@

namespace Hless.Api.Controllers
{
[Route("[controller]")]
[Route("[controller]/[action]")]
public class SchemaController : BaseController
{
readonly ISchemaRepository _repository;
public SchemaController(ISchemaRepository repository)
{
_repository = repository;
}

[HttpGet]
public async Task<IEnumerable<SchemaDto>> GetSchemasAsync()
{
var schemas = (await _repository.GetSchemasAsync())
.Select(schema => schema.AsDto());
return schemas;
}
[HttpGet]
public async Task<Schema> GetSchemaAsync(long schemaId)
{
return await _repository.GetSchemaAsync(schemaId);
}
[HttpPost]
public async Task<Schema> 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<bool> UpdateSchemaAsync([FromBody] Schema schema)
{
return await _repository.UpdateSchemaAsync(schema);
}
[HttpGet]
public async Task<bool> PublishSchemaAsync(long schemaId)
{
return await _repository.PublishSchemaAsync(schemaId);
}
[HttpDelete]
public async Task<bool> DeleteSchemaAsync(long schemaId)
{
return await _repository.DeleteSchemaAsync(schemaId);
}
[HttpGet]
public async Task<Schema> Test([FromBody] Schema schema)
{
return await Task.FromResult(schema);
}
}
}
10 changes: 10 additions & 0 deletions src/Hless.Api/Extensions/Models/DtoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
}
2 changes: 1 addition & 1 deletion src/Hless.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddDependencies(this IServiceCollection services)
{
services.AddSingleton<ISchemaRepository, InMemorySchemaRepository>();
services.AddSingleton<IApplicationRepository, InMemoryApplicationRepository>();
return services;
}

}

}
20 changes: 20 additions & 0 deletions src/Hless.Api/Models/Dto/ApplicationDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hless.Api.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; }
}
}
15 changes: 13 additions & 2 deletions src/Hless.Api/Models/Dto/SchemaDto.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
using Hless.Data.Models;
using System;
using System.Collections.Generic;

namespace Hless.Api.Models.Dto
{
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<string, string> Definition { get; init; }
public Dictionary<string, string> DraftDefinition { get; init; }
public string CreatedBy { get; init; }
public long ApplicationId { get; init; }
}

public record SchemaCreateDto
{
public string Name { get; init; }
public Dictionary<string, string> DraftDefinition { get; init; }
public long ApplicationId { get; init; }
}
}
2 changes: 2 additions & 0 deletions src/Hless.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/Hless.Common/Repositories/IApplicationRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hless.Data.Models;

namespace Hless.Common.Repositories
{
public interface IApplicationRepository
{
Task<IEnumerable<Application>> GetApplicationsAsync();
Task<Application> GetApplicationAsync(long applicationId);
Task CreateApplicationAsync(string name, string OwnerId);
Task<bool> UpdateApplicationAsync(long applicationId, string name, string OwnerId);
Task<bool> DeleteApplicationAsync(long applicationId);
}
}
7 changes: 4 additions & 3 deletions src/Hless.Common/Repositories/ISchemaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public interface ISchemaRepository
{
Task<IEnumerable<Schema>> GetSchemasAsync();
Task<Schema> GetSchemaAsync(long schemaId);
Task CreateSchemaAsync(Schema schema);
Task UpdateSchemaAsync(Schema schema);
Task PublishSchemaAsync(long schemaId);
Task<Schema> CreateSchemaAsync(Schema schema);
Task<bool> UpdateSchemaAsync(Schema schema);
Task<bool> PublishSchemaAsync(long schemaId);
Task<bool> DeleteSchemaAsync(long schemaId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Hless.Common.Repositories;
using Hless.Data.Models;
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<Application> 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(string name, string OwnerId)
{
await Task.Run(() =>
{
Application application = new Application()
{
ApplicationId = applications.Count,
Name = name,
OwnerId = OwnerId,
CreatedBy = "CreatedBy", // TODO: Change to logged in user
CreatedAt = DateTime.Now,
LastModified = DateTime.Now,
};

applications.Add(application);
});
}

public async Task<bool> DeleteApplicationAsync(long applicationId)
{
return await Task.Run(() => applications.Remove(applications.SingleOrDefault(app => app.ApplicationId == applicationId)));
}

public async Task<Application> GetApplicationAsync(long applicationId)
{
return await Task.Run(() => applications.Where(app => app.ApplicationId == applicationId).SingleOrDefault());
}

public async Task<IEnumerable<Application>> GetApplicationsAsync()
{
return await Task.FromResult(applications);
}

public async Task<bool> UpdateApplicationAsync(long applicationId, string name, string OwnerId)
{
return await Task.Run(() =>
{
Application oldApp = applications.SingleOrDefault(app => app.ApplicationId == applicationId);

if (oldApp != null)
{
Application newApp = new Application()
{
ApplicationId = oldApp.ApplicationId,
Name = name,
OwnerId = 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;
}
});
}
}
}
Loading