Adding Redis for Shopping Cart

This commit is contained in:
cshowalter 2022-05-17 14:04:42 -07:00
parent f3ef7e326e
commit 0ce1d055c7
8 changed files with 87 additions and 1 deletions

View File

@ -10,6 +10,7 @@ namespace API.Extensions
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<iProductRepository, ProductRepository>();
services.AddScoped<IBasketRepository, BasketRepository>();
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
services.Configure<ApiBehaviorOptions>(options =>
options.InvalidModelStateResponseFactory = actionContext =>

View File

@ -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<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
services.AddSingleton<ConnectionMultiplexer>(c => {
var configuration = ConfigurationOptions.Parse(_config.GetConnectionString("redis"), true);
return ConnectionMultiplexer.Connect(configuration);
});
services.AddAutoMapper(typeof(MappingProfiles));
services.AddCors(opt =>
{

View File

@ -6,7 +6,8 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Data source=ecommerce.db"
"DefaultConnection": "Data source=ecommerce.db",
"Redis": "localhost"
},
"ApiUrl": "https://localhost:5001/"
}

View File

@ -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; }
}
}

View File

@ -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<BasketItem> Items { get; set; } = new List<BasketItem>();
}
}

View File

@ -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<CustomerBasket> GetCustomerBasketAsync(string basketId);
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
Task<bool> DeleteBasketAsysnc(string basketId);
}
}

View File

@ -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<bool> DeleteBasketAsysnc(string basketId)
{
throw new NotImplementedException();
}
public Task<CustomerBasket> GetCustomerBasketAsync(string basketId)
{
throw new NotImplementedException();
}
public Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket)
{
throw new NotImplementedException();
}
}
}

View File

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.4" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
</ItemGroup>
<PropertyGroup>