Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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);
}
}
}
4 changes: 2 additions & 2 deletions src/Hless.Api/Controllers/SchemaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

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()
{
Expand Down
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; }
}
}
4 changes: 3 additions & 1 deletion src/Hless.Api/Models/Dto/SchemaDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Hless.Data.Models;

namespace Hless.Api.Models.Dto
{
public record SchemaDto
Expand All @@ -7,6 +9,6 @@ public record SchemaDto
public string Definition { get; init; }
public string DraftDefinition { get; init; }
public string CreatedBy { 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;
}
});
}
}
}
48 changes: 43 additions & 5 deletions src/Hless.Data.InMemory/Repositories/InMemorySchemaRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,12 +13,49 @@ public class InMemorySchemaRepository : ISchemaRepository
{
private readonly List<Schema> 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<Schema> CreateSchemaAsync(Schema schema)
{
throw new System.NotImplementedException();
return await Task.Run(() =>
{
try
{


Schema newSchema = new Schema()
{
SchemaId = schemas.Count,
Name = schema.Name,
Definition = null,
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<bool> DeleteSchemaAsync(long schemaId)
{

return Task.Run(() => schemas.Remove(schemas.Find(s => s.SchemaId == schemaId)));
}

public async Task<Schema> GetSchemaAsync(long schemaId)
Expand All @@ -31,12 +69,12 @@ public async Task<IEnumerable<Schema>> GetSchemasAsync()
return await Task.FromResult(schemas);
}

public Task PublishSchemaAsync(long schemaId)
public Task<bool> PublishSchemaAsync(long schemaId)
{
throw new System.NotImplementedException();
}

public Task UpdateSchemaAsync(Schema schema)
public Task<bool> UpdateSchemaAsync(Schema schema)
{
throw new System.NotImplementedException();
}
Expand Down
18 changes: 18 additions & 0 deletions src/Hless.Data/Models/Application.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;

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; }
}
}
7 changes: 7 additions & 0 deletions src/Hless.Data/Models/Schema.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Hless.Data.Models
{
public record Schema
Expand All @@ -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 long ApplicationId { get; init; }

}

Expand Down
Loading