-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d479fd
commit a086250
Showing
13 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using Elastic.Clients.Elasticsearch; | ||
using Elasticsearch.WebAPI.Dtos; | ||
using Elasticsearch.WebAPI.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Elasticsearch.WebAPI.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ElasticsearchTestController : ControllerBase | ||
{ | ||
private readonly IProductElasticService _elasticService; | ||
|
||
public ElasticsearchTestController(IProductElasticService elasticService) | ||
{ | ||
_elasticService = elasticService; | ||
} | ||
[HttpGet("GetByNameWildcard")] | ||
public async Task<IActionResult> GetByNameWildcard(string value, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.GetByNameWildcard(value, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Documents); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpGet("GetByNameMatch")] | ||
public async Task<IActionResult> GetByNameMatch(string query, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.GetByNameMatch(query, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Documents); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpGet("GetByNameFuzzy")] | ||
public async Task<IActionResult> GetByNameFuzzy(string value, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.GetByNameFuzzy(value, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Documents); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpGet("GetAll")] | ||
public async Task<IActionResult> GetAll(CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.GetAll(cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Documents); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpGet("SeedData")] | ||
public async Task<IActionResult> SeedData(CancellationToken cancellationToken) | ||
{ | ||
await _elasticService.SeedData(cancellationToken); | ||
return Created(); | ||
} | ||
[HttpPost("Create")] | ||
public async Task<IActionResult> Create(CreateProductDto createProductDto, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.Create(createProductDto, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Result); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpPut("Update")] | ||
public async Task<IActionResult> Update(UpdateProductDto updateProductDto, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.Update(updateProductDto, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Result); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
[HttpDelete("DeleteById")] | ||
public async Task<IActionResult> DeleteById(Guid id, CancellationToken cancellationToken) | ||
{ | ||
var response = await _elasticService.DeleteById(id, cancellationToken); | ||
switch (response.IsValidResponse && response.IsSuccess()) | ||
{ | ||
case true: | ||
return Ok(response.Result); | ||
break; | ||
case false: | ||
return BadRequest(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Elasticsearch.WebAPI.Dtos | ||
{ | ||
public record CreateProductDto | ||
{ | ||
public string Name { get; set; } | ||
public decimal Price { get; set; } | ||
public int Stock { get; set; } | ||
public string Description { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Elasticsearch.WebAPI.Dtos | ||
{ | ||
public record UpdateProductDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public decimal Price { get; set; } | ||
public int Stock { get; set; } | ||
public string Description { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Bogus" Version="35.6.0" /> | ||
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.15.2" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@Elasticsearch.WebAPI_HostAddress = http://localhost:5265 | ||
|
||
GET {{Elasticsearch.WebAPI_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.10.35122.118 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elasticsearch.WebAPI", "Elasticsearch.WebAPI.csproj", "{1BB12E41-1706-461F-8897-B3DC89774502}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{1BB12E41-1706-461F-8897-B3DC89774502}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{1BB12E41-1706-461F-8897-B3DC89774502}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{1BB12E41-1706-461F-8897-B3DC89774502}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{1BB12E41-1706-461F-8897-B3DC89774502}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {29721A1A-3AAD-4DE9-BC9F-8DABE9B4C13D} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Elasticsearch.WebAPI.Models | ||
{ | ||
public class Product | ||
{ | ||
public Product() | ||
{ | ||
Id = Guid.NewGuid(); | ||
} | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public decimal Price { get; set; } | ||
public int Stock { get; set; } | ||
public string Description { get; set; } | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Elasticsearch.WebAPI.Services; | ||
|
||
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(); | ||
|
||
builder.Services.AddScoped<IProductElasticService, ProductElasticService>(); | ||
|
||
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:36087", | ||
"sslPort": 44349 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5265", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7058;http://localhost:5265", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Elastic.Clients.Elasticsearch; | ||
using Elasticsearch.WebAPI.Dtos; | ||
using Elasticsearch.WebAPI.Models; | ||
|
||
namespace Elasticsearch.WebAPI.Services | ||
{ | ||
public interface IProductElasticService | ||
{ | ||
Task<CreateResponse> Create(CreateProductDto createProductDto, CancellationToken cancellationToken); | ||
Task<UpdateResponse<Product>> Update(UpdateProductDto updateProductDto, CancellationToken cancellationToken); | ||
Task SeedData(CancellationToken cancellationToken); | ||
Task<SearchResponse<Product>> GetAll(CancellationToken cancellationToken); | ||
Task<DeleteResponse> DeleteById(Guid id, CancellationToken cancellationToken); | ||
Task<SearchResponse<Product>> GetByNameWildcard(string value, CancellationToken cancellationToken); | ||
Task<SearchResponse<Product>> GetByNameMatch(string query, CancellationToken cancellationToken); | ||
Task<SearchResponse<Product>> GetByNameFuzzy(string value, CancellationToken cancellationToken); | ||
} | ||
} |
Oops, something went wrong.