From 06332ebdf56e4f4b76fa87758e240b211012f857 Mon Sep 17 00:00:00 2001 From: marcus Date: Sun, 25 Feb 2024 13:22:48 +0100 Subject: [PATCH] init api --- Weifer.Database.EF.sln | 8 +++- .../Controllers/AuthenticationController.cs | 15 +++++++ Weifer.ShoppingApp.API/Models/CustomerDto.cs | 11 +++++ .../Models/ShoppingItemDto.cs | 11 +++++ .../Models/ShoppingListDot.cs | 9 ++++ Weifer.ShoppingApp.API/Program.cs | 43 ++++++++++++++++++ .../Properties/launchSettings.json | 41 +++++++++++++++++ .../RestApi/AuthenticationApiController.cs | 6 +++ .../RestApi/CustomerApiController.cs | 44 +++++++++++++++++++ .../RestApi/ShoppingItemApiControlle.cs | 14 ++++++ .../RestApi/ShoppingListApiController.cs | 14 ++++++ .../Weifer.ShoppingApp.API.csproj | 25 +++++++++++ .../Weifer.ShoppingApp.API.http | 6 +++ .../appsettings.Development.json | 8 ++++ Weifer.ShoppingApp.API/appsettings.json | 12 +++++ 15 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 Weifer.ShoppingApp.API/Controllers/AuthenticationController.cs create mode 100644 Weifer.ShoppingApp.API/Models/CustomerDto.cs create mode 100644 Weifer.ShoppingApp.API/Models/ShoppingItemDto.cs create mode 100644 Weifer.ShoppingApp.API/Models/ShoppingListDot.cs create mode 100644 Weifer.ShoppingApp.API/Program.cs create mode 100644 Weifer.ShoppingApp.API/Properties/launchSettings.json create mode 100644 Weifer.ShoppingApp.API/RestApi/AuthenticationApiController.cs create mode 100644 Weifer.ShoppingApp.API/RestApi/CustomerApiController.cs create mode 100644 Weifer.ShoppingApp.API/RestApi/ShoppingItemApiControlle.cs create mode 100644 Weifer.ShoppingApp.API/RestApi/ShoppingListApiController.cs create mode 100644 Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.csproj create mode 100644 Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.http create mode 100644 Weifer.ShoppingApp.API/appsettings.Development.json create mode 100644 Weifer.ShoppingApp.API/appsettings.json diff --git a/Weifer.Database.EF.sln b/Weifer.Database.EF.sln index a8652f5..c3479f8 100644 --- a/Weifer.Database.EF.sln +++ b/Weifer.Database.EF.sln @@ -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 diff --git a/Weifer.ShoppingApp.API/Controllers/AuthenticationController.cs b/Weifer.ShoppingApp.API/Controllers/AuthenticationController.cs new file mode 100644 index 0000000..ddce3cc --- /dev/null +++ b/Weifer.ShoppingApp.API/Controllers/AuthenticationController.cs @@ -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); + } + } +} diff --git a/Weifer.ShoppingApp.API/Models/CustomerDto.cs b/Weifer.ShoppingApp.API/Models/CustomerDto.cs new file mode 100644 index 0000000..cd1f0df --- /dev/null +++ b/Weifer.ShoppingApp.API/Models/CustomerDto.cs @@ -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; } + } +} diff --git a/Weifer.ShoppingApp.API/Models/ShoppingItemDto.cs b/Weifer.ShoppingApp.API/Models/ShoppingItemDto.cs new file mode 100644 index 0000000..9152b4b --- /dev/null +++ b/Weifer.ShoppingApp.API/Models/ShoppingItemDto.cs @@ -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; + } +} diff --git a/Weifer.ShoppingApp.API/Models/ShoppingListDot.cs b/Weifer.ShoppingApp.API/Models/ShoppingListDot.cs new file mode 100644 index 0000000..e766bf1 --- /dev/null +++ b/Weifer.ShoppingApp.API/Models/ShoppingListDot.cs @@ -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; } + } +} diff --git a/Weifer.ShoppingApp.API/Program.cs b/Weifer.ShoppingApp.API/Program.cs new file mode 100644 index 0000000..17026bd --- /dev/null +++ b/Weifer.ShoppingApp.API/Program.cs @@ -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(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(); diff --git a/Weifer.ShoppingApp.API/Properties/launchSettings.json b/Weifer.ShoppingApp.API/Properties/launchSettings.json new file mode 100644 index 0000000..986d134 --- /dev/null +++ b/Weifer.ShoppingApp.API/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/Weifer.ShoppingApp.API/RestApi/AuthenticationApiController.cs b/Weifer.ShoppingApp.API/RestApi/AuthenticationApiController.cs new file mode 100644 index 0000000..e654f46 --- /dev/null +++ b/Weifer.ShoppingApp.API/RestApi/AuthenticationApiController.cs @@ -0,0 +1,6 @@ +namespace Weifer.ShoppingApp.API.RestApi +{ + public class AuthenticationApiController + { + } +} diff --git a/Weifer.ShoppingApp.API/RestApi/CustomerApiController.cs b/Weifer.ShoppingApp.API/RestApi/CustomerApiController.cs new file mode 100644 index 0000000..89d523e --- /dev/null +++ b/Weifer.ShoppingApp.API/RestApi/CustomerApiController.cs @@ -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 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); + } + } +} diff --git a/Weifer.ShoppingApp.API/RestApi/ShoppingItemApiControlle.cs b/Weifer.ShoppingApp.API/RestApi/ShoppingItemApiControlle.cs new file mode 100644 index 0000000..8a66113 --- /dev/null +++ b/Weifer.ShoppingApp.API/RestApi/ShoppingItemApiControlle.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Weifer.ShoppingApp.API.RestApi +{ + [ApiController] + [Route("[controller]")] + public class ShoppingItemApiControlle : ControllerBase + { + + + + + } +} diff --git a/Weifer.ShoppingApp.API/RestApi/ShoppingListApiController.cs b/Weifer.ShoppingApp.API/RestApi/ShoppingListApiController.cs new file mode 100644 index 0000000..3b0acba --- /dev/null +++ b/Weifer.ShoppingApp.API/RestApi/ShoppingListApiController.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Weifer.ShoppingApp.API.RestApi +{ + [ApiController] + [Route("[controller]")] + public class ShoppingListApiController : ControllerBase + { + + + + + } +} diff --git a/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.csproj b/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.csproj new file mode 100644 index 0000000..9ed2ad3 --- /dev/null +++ b/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + ..\Weifer.Database.EF\bin\Release\net8.0\Weifer.Database.EF.dll + + + + diff --git a/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.http b/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.http new file mode 100644 index 0000000..eefa7fa --- /dev/null +++ b/Weifer.ShoppingApp.API/Weifer.ShoppingApp.API.http @@ -0,0 +1,6 @@ +@Weifer.ShoppingApp.API_HostAddress = http://localhost:5164 + +GET {{Weifer.ShoppingApp.API_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Weifer.ShoppingApp.API/appsettings.Development.json b/Weifer.ShoppingApp.API/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Weifer.ShoppingApp.API/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Weifer.ShoppingApp.API/appsettings.json b/Weifer.ShoppingApp.API/appsettings.json new file mode 100644 index 0000000..728e982 --- /dev/null +++ b/Weifer.ShoppingApp.API/appsettings.json @@ -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" + } +}