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

Enhanced code #30

Merged
merged 9 commits into from
Oct 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Build
run: dotnet build src/SharpConnector/SharpConnector.csproj --no-restore
- name: Test
run: dotnet test src/SharpConnector.Test/SharpConnector.Test.csproj --no-build --verbosity normal
run: dotnet test src/SharpConnector.Tests/SharpConnector.Tests.csproj --no-build --verbosity normal
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ SharpConnector provides access to **CRUD** operations to NoSQL databases with **
* **EnyimMemcached**
* **RavenDB**

other connectors and operations are under development.

### How to use it
To use SharpConnector simply configure the *connectionString* to the desired connector and the *instance* type.
Add the *ConnectorConfig* node configuration within the *appsettings.json* file, here is the example for Redis connector:
To get started with SharpConnector, configure your *connectionString* and specify the connector *instance* type.
Then, add the ConnectorConfig node within your appsettings.json file. Here’s an example configuration for a Redis connector:

```json
{
Expand All @@ -36,13 +34,19 @@ Add the *ConnectorConfig* node configuration within the *appsettings.json* file,
}
```

and instantiate a new client specifying the type of data (for example string):
Once configured, create a new SharpConnector client, specifying the payload type (e.g., string):

```csharp
SharpConnectorClient<string> client = new SharpConnectorClient<string>()
```

SharpConnector works with any object that is serializable.
Alternatively, you can integrate SharpConnector client using dependency injection. Here’s how to register the SharpConnector service with a string payload type:

```csharp
// Register the SharpConnector services with string payload type.
builder.Services.AddSharpConnectorServices<string>();
```
This setup provides flexibility in working with different payload types and makes SharpConnector easy to use within dependency injection configurations.

### Contributing
Thank you for considering to help out with the source code!
Expand Down
113 changes: 113 additions & 0 deletions src/SharpConnector.Api/Controllers/ConnectorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// (c) 2020 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using Microsoft.AspNetCore.Mvc;
using SharpConnector.Interfaces;

namespace SharpConnector.Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class ConnectorController : ControllerBase
{
private readonly ISharpConnectorClient<string> _connectorClient;

private readonly ILogger<ConnectorController> _logger;

public ConnectorController(ILogger<ConnectorController> logger, ISharpConnectorClient<string> connectorClient)
{
_logger = logger;
_connectorClient = connectorClient;
}

[HttpGet("{key}", Name = "Get")]
public ActionResult<string> Get(string key)
{
var result = _connectorClient.Get(key);
return result != null ? Ok(result) : NotFound();
}

[HttpGet("async/{key}")]
public async Task<ActionResult<string>> GetAsync(string key)
{
var result = await _connectorClient.GetAsync(key);
return result != null ? Ok(result) : NotFound();
}

[HttpGet("all")]
public ActionResult<IEnumerable<string>> GetAll()
{
var results = _connectorClient.GetAll();
return Ok(results);
}

[HttpPost("insert")]
public ActionResult<bool> Insert(string key, [FromBody] string value)
{
var success = _connectorClient.Insert(key, value);
return success ? Ok(success) : BadRequest("Insert failed.");
}

[HttpPost("insert/with-expiration")]
public ActionResult<bool> Insert(string key, [FromBody] string value, TimeSpan expiration)
{
var success = _connectorClient.Insert(key, value, expiration);
return success ? Ok(success) : BadRequest("Insert with expiration failed.");
}

[HttpPost("insert-async")]
public async Task<ActionResult<bool>> InsertAsync(string key, [FromBody] string value)
{
var success = await _connectorClient.InsertAsync(key, value);
return success ? Ok(success) : BadRequest("Async insert failed.");
}

[HttpPost("insert-async/with-expiration")]
public async Task<ActionResult<bool>> InsertAsync(string key, [FromBody] string value, TimeSpan expiration)
{
var success = await _connectorClient.InsertAsync(key, value, expiration);
return success ? Ok(success) : BadRequest("Async insert with expiration failed.");
}

[HttpPost("insert-many")]
public ActionResult<bool> InsertMany([FromBody] Dictionary<string, string> values)
{
var success = _connectorClient.InsertMany(values);
return success ? Ok(success) : BadRequest("Insert many failed.");
}

[HttpPost("insert-many/with-expiration")]
public ActionResult<bool> InsertMany([FromBody] Dictionary<string, string> values, TimeSpan expiration)
{
var success = _connectorClient.InsertMany(values, expiration);
return success ? Ok(success) : BadRequest("Insert many with expiration failed.");
}

[HttpDelete("{key}")]
public ActionResult<bool> Delete(string key)
{
var success = _connectorClient.Delete(key);
return success ? Ok(success) : NotFound("Delete failed.");
}

[HttpDelete("async/{key}")]
public async Task<ActionResult<bool>> DeleteAsync(string key)
{
var success = await _connectorClient.DeleteAsync(key);
return success ? Ok(success) : NotFound("Async delete failed.");
}

[HttpPut("{key}")]
public ActionResult<bool> Update(string key, [FromBody] string value)
{
var success = _connectorClient.Update(key, value);
return success ? Ok(success) : NotFound("Update failed.");
}

[HttpPut("async/{key}")]
public async Task<ActionResult<bool>> UpdateAsync(string key, [FromBody] string value)
{
var success = await _connectorClient.UpdateAsync(key, value);
return success ? Ok(success) : NotFound("Async update failed.");
}
}
}
32 changes: 32 additions & 0 deletions src/SharpConnector.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// (c) 2020 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using SharpConnector.Middleware;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Register the SharpConnector services with string payload type.
builder.Services.AddSharpConnectorServices<string>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
41 changes: 41 additions & 0 deletions src/SharpConnector.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:44453",
"sslPort": 44308
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5124",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7158;http://localhost:5124",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
44 changes: 44 additions & 0 deletions src/SharpConnector.Api/SharpConnector.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Content Remove="appsettings.litedb.json" />
<Content Remove="appsettings.memcached.json" />
<Content Remove="appsettings.mongodb.json" />
<Content Remove="appsettings.ravendb.json" />
<Content Remove="appsettings.redis.json" />
</ItemGroup>

<ItemGroup>
<None Include="appsettings.litedb.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.memcached.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.mongodb.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.ravendb.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.redis.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SharpConnector\SharpConnector.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/SharpConnector.Api/SharpConnector.Api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@SharpConnector.Api_HostAddress = http://localhost:5124

GET {{SharpConnector.Api_HostAddress}}/weatherforecast/
Accept: application/json

###
14 changes: 14 additions & 0 deletions src/SharpConnector.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ConnectorConfig": {
"Instance": "RavenDb",
"DatabaseName": "test",
"ConnectionString": "http://live-test.ravendb.net"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectorConfig": {
"Instance": "LiteDb",
"CollectionName": "test",
"ConnectionString": "LiteDbTest.db"
}
{
"ConnectorConfig": {
"Instance": "LiteDb",
"CollectionName": "test",
"ConnectionString": "LiteDbTest.db"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectorConfig": {
"Instance": "Memcached",
"ConnectionString": "127.0.0.1:11211"
}
{
"ConnectorConfig": {
"Instance": "Memcached",
"ConnectionString": "127.0.0.1:11211"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"ConnectorConfig": {
"Instance": "MongoDb",
"DatabaseName": "test",
"CollectionName": "test",
"ConnectionString": "mongodb_connectionstring_here"
}
{
"ConnectorConfig": {
"Instance": "MongoDb",
"DatabaseName": "test",
"CollectionName": "test",
"ConnectionString": "mongodb_connectionstring_here"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectorConfig": {
"Instance": "RavenDb",
"DatabaseName": "test",
"ConnectionString": "http://live-test.ravendb.net"
}
{
"ConnectorConfig": {
"Instance": "RavenDb",
"DatabaseName": "test",
"ConnectionString": "http://live-test.ravendb.net"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectorConfig": {
"Instance": "Redis",
"DatabaseNumber": 0,
"ConnectionString": "redis_connectionstring_here"
}
{
"ConnectorConfig": {
"Instance": "Redis",
"DatabaseNumber": 0,
"ConnectionString": "redis_connectionstring_here"
}
}
Loading