This commit is contained in:
Marcus Ferl 2024-02-25 13:22:48 +01:00
parent c06f4645a9
commit 06332ebdf5
15 changed files with 266 additions and 1 deletions

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34526.213
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weifer.Database.EF", "Weifer.Database.EF\Weifer.Database.EF.csproj", "{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Weifer.Database.EF", "Weifer.Database.EF\Weifer.Database.EF.csproj", "{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weifer.ShoppingApp.API", "Weifer.ShoppingApp.API\Weifer.ShoppingApp.API.csproj", "{3CBCA49C-A7DE-48E4-85F9-7B721C4E0D4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +17,10 @@ Global
{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20B939C6-8D62-4DCA-B47D-AE17FE96A0CA}.Release|Any CPU.Build.0 = Release|Any CPU
{3CBCA49C-A7DE-48E4-85F9-7B721C4E0D4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CBCA49C-A7DE-48E4-85F9-7B721C4E0D4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CBCA49C-A7DE-48E4-85F9-7B721C4E0D4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CBCA49C-A7DE-48E4-85F9-7B721C4E0D4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,15 @@
namespace Weifer.ShoppingApp.API.Controllers
{
public class AuthenticationController
{
public string HashPassword(string password)
{
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);
return hashedPassword;
}
public bool VerifyPassword(string password, string hashedPassword)
{
return BCrypt.Net.BCrypt.Verify(password, hashedPassword);
}
}
}

View File

@ -0,0 +1,11 @@
namespace Weifer.ShoppingApp.API.Models
{
public class CustomerDto
{
public Guid CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace Weifer.ShoppingApp.API.Models
{
public class ShoppingItemDto
{
public Guid ShoppingItemId { get; set; }
public Guid ShoppingListId { get; set; }
public string Description { get; set; }
public int Number { get; set; } = 0;
public bool Purchased { get; set; } = false;
}
}

View File

@ -0,0 +1,9 @@
namespace Weifer.ShoppingApp.API.Models
{
public class ShoppingListDot
{
public Guid ShoppingListId { get; set; }
public Guid CustomerId { get; set; }
public string ShoppingListName { get; set; }
}
}

View File

@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using Weifer.Database.EF;
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();
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var connectionString = configuration.GetConnectionString("ShoppingApp");
builder.Services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(connectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
app.UseCors("EnableCORS");
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56549",
"sslPort": 44347
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5164",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7098;http://localhost:5164",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,6 @@
namespace Weifer.ShoppingApp.API.RestApi
{
public class AuthenticationApiController
{
}
}

View File

@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Mvc;
using Weifer.Database.EF;
using Weifer.Database.EF.Entitys;
using Weifer.ShoppingApp.API.Controllers;
using Weifer.ShoppingApp.API.Models;
namespace Weifer.ShoppingApp.API.RestApi
{
[ApiController]
[Route("[controller]")]
public class CustomerApiController : ControllerBase
{
public readonly AuthenticationController authenticationController;
public readonly DatabaseContext dbContext;
public CustomerApiController(DatabaseContext dbContext)
{
authenticationController = new AuthenticationController();
this.dbContext = dbContext;
}
[HttpPost]
public async Task <IActionResult> CreateCustomer(CustomerDto customerDto)
{
if(customerDto == null)
{
return BadRequest();
}
var customer = new Customer
{
CustomerId = new Guid(),
FirstName = customerDto.FirstName,
LastName = customerDto.LastName,
Email = customerDto.Email,
PasswordHash = authenticationController.HashPassword(customerDto.Password),
CreatedOn = DateTime.UtcNow,
};
dbContext.Customers.Add(customer);
dbContext.SaveChanges();
return Ok(customerDto);
}
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc;
namespace Weifer.ShoppingApp.API.RestApi
{
[ApiController]
[Route("[controller]")]
public class ShoppingItemApiControlle : ControllerBase
{
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc;
namespace Weifer.ShoppingApp.API.RestApi
{
[ApiController]
[Route("[controller]")]
public class ShoppingListApiController : ControllerBase
{
}
}

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Weifer.Database.EF\Weifer.Database.EF.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Weifer.Database.EF">
<HintPath>..\Weifer.Database.EF\bin\Release\net8.0\Weifer.Database.EF.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
@Weifer.ShoppingApp.API_HostAddress = http://localhost:5164
GET {{Weifer.ShoppingApp.API_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"ShoppingApp": "Server=192.168.0.187,1433;Database=ShoppingApp;User Id=sa;Password=Su746MoWyM8CtqgiCc3oWzsb;Trusted_Connection=False;TrustServerCertificate=True"
}
}