From 0ce1d055c7cd5444cdd8ca682f611f667a301e1b Mon Sep 17 00:00:00 2001 From: cshowalter Date: Tue, 17 May 2022 14:04:42 -0700 Subject: [PATCH] Adding Redis for Shopping Cart --- .../ApplicationServicesExtensions.cs | 1 + API/Startup.cs | 5 ++++ API/appsettings.Development.json | 3 ++- Core/Entities/BasketItem.cs | 13 +++++++++ Core/Entities/CustomerBasket.cs | 22 +++++++++++++++ Core/Interfaces/IBasketRepository.cs | 16 +++++++++++ Infrastructure/Data/BasketRepository.cs | 27 +++++++++++++++++++ Infrastructure/Infrastructure.csproj | 1 + 8 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Core/Entities/BasketItem.cs create mode 100644 Core/Entities/CustomerBasket.cs create mode 100644 Core/Interfaces/IBasketRepository.cs create mode 100644 Infrastructure/Data/BasketRepository.cs diff --git a/API/Extensions/ApplicationServicesExtensions.cs b/API/Extensions/ApplicationServicesExtensions.cs index a870430..48be291 100644 --- a/API/Extensions/ApplicationServicesExtensions.cs +++ b/API/Extensions/ApplicationServicesExtensions.cs @@ -10,6 +10,7 @@ namespace API.Extensions public static IServiceCollection AddApplicationServices(this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>))); services.Configure(options => options.InvalidModelStateResponseFactory = actionContext => diff --git a/API/Startup.cs b/API/Startup.cs index b8d3d3a..2604b08 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -3,6 +3,7 @@ using API.Helpers; using API.Middleware; using Infrastructure.Data; using Microsoft.EntityFrameworkCore; +using StackExchange.Redis; namespace API { @@ -23,6 +24,10 @@ namespace API services.AddApplicationServices(); services.AddSwaggerDocumentation(); services.AddDbContext(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); + services.AddSingleton(c => { + var configuration = ConfigurationOptions.Parse(_config.GetConnectionString("redis"), true); + return ConnectionMultiplexer.Connect(configuration); + }); services.AddAutoMapper(typeof(MappingProfiles)); services.AddCors(opt => { diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index f9dcb95..1029181 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -6,7 +6,8 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Data source=ecommerce.db" + "DefaultConnection": "Data source=ecommerce.db", + "Redis": "localhost" }, "ApiUrl": "https://localhost:5001/" } diff --git a/Core/Entities/BasketItem.cs b/Core/Entities/BasketItem.cs new file mode 100644 index 0000000..a5118c4 --- /dev/null +++ b/Core/Entities/BasketItem.cs @@ -0,0 +1,13 @@ +namespace Core.Entities +{ + public class BasketItem + { + public int Id { get; set; } + public string productName { get; set; } + public decimal Price { get; set; } + public int Quantity { get; set; } + public string PictureUrl { get; set; } + public string Brand { get; set; } + public string Type { get; set; } + } +} \ No newline at end of file diff --git a/Core/Entities/CustomerBasket.cs b/Core/Entities/CustomerBasket.cs new file mode 100644 index 0000000..ef196bd --- /dev/null +++ b/Core/Entities/CustomerBasket.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Core.Entities +{ + public class CustomerBasket + { + public CustomerBasket() + { + } + + public CustomerBasket(string id) + { + Id = id; + } + + public string Id { get; set; } + public List Items { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/Core/Interfaces/IBasketRepository.cs b/Core/Interfaces/IBasketRepository.cs new file mode 100644 index 0000000..f89998f --- /dev/null +++ b/Core/Interfaces/IBasketRepository.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Core.Entities; + +namespace Core.Interfaces +{ + public interface IBasketRepository + { + Task GetCustomerBasketAsync(string basketId); + Task UpdateBasketAsync(CustomerBasket basket); + Task DeleteBasketAsysnc(string basketId); + + } +} \ No newline at end of file diff --git a/Infrastructure/Data/BasketRepository.cs b/Infrastructure/Data/BasketRepository.cs new file mode 100644 index 0000000..efb7883 --- /dev/null +++ b/Infrastructure/Data/BasketRepository.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Core.Entities; +using Core.Interfaces; + +namespace Infrastructure.Data +{ + public class BasketRepository : IBasketRepository + { + public Task DeleteBasketAsysnc(string basketId) + { + throw new NotImplementedException(); + } + + public Task GetCustomerBasketAsync(string basketId) + { + throw new NotImplementedException(); + } + + public Task UpdateBasketAsync(CustomerBasket basket) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index 269da8a..f5a3568 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -6,6 +6,7 @@ +