Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkankacan committed Aug 25, 2024
1 parent 8d479fd commit a086250
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 0 deletions.
137 changes: 137 additions & 0 deletions Controllers/ElasticsearchTestController.cs
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;
}
}
}
}
10 changes: 10 additions & 0 deletions Dtos/CreateProductDto.cs
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; }
}
}
11 changes: 11 additions & 0 deletions Dtos/UpdateProductDto.cs
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; }
}
}
15 changes: 15 additions & 0 deletions Elasticsearch.WebAPI.csproj
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>
6 changes: 6 additions & 0 deletions Elasticsearch.WebAPI.http
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

###
25 changes: 25 additions & 0 deletions Elasticsearch.WebAPI.sln
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
17 changes: 17 additions & 0 deletions Models/Product.cs
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; }

}

}
29 changes: 29 additions & 0 deletions Program.cs
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();
41 changes: 41 additions & 0 deletions 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: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"
}
}
}
}
18 changes: 18 additions & 0 deletions Services/IProductElasticService.cs
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);
}
}
Loading

0 comments on commit a086250

Please sign in to comment.