Added Redis, Fixed Redis Delete

This commit is contained in:
cshowalter 2022-05-17 16:29:34 -07:00
parent 0ce1d055c7
commit bcbad19601
5 changed files with 78 additions and 8 deletions

View File

@ -0,0 +1,35 @@
using Core.Entities;
using Core.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
public class BasketController : BaseApiController
{
private readonly IBasketRepository _basketRepository;
public BasketController(IBasketRepository basketRepository)
{
_basketRepository = basketRepository;
}
[HttpGet]
public async Task<ActionResult<CustomerBasket>> GetBasketById(string id)
{
var basket = await _basketRepository.GetBasketAsync(id);
return Ok(basket ?? new CustomerBasket(id));
}
[HttpPost]
public async Task<ActionResult<CustomerBasket>> UpdateBasket(CustomerBasket basket)
{
var updatedBasket = await _basketRepository.UpdateBasketAsync(basket);
return Ok(updatedBasket);
}
[HttpDelete]
public async Task DeleteBasket(string id)
{
await _basketRepository.DeleteBasketAsysnc(id);
}
}
}

View File

@ -24,7 +24,7 @@ namespace API
services.AddApplicationServices(); services.AddApplicationServices();
services.AddSwaggerDocumentation(); services.AddSwaggerDocumentation();
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
services.AddSingleton<ConnectionMultiplexer>(c => { services.AddSingleton<IConnectionMultiplexer>(c => {
var configuration = ConfigurationOptions.Parse(_config.GetConnectionString("redis"), true); var configuration = ConfigurationOptions.Parse(_config.GetConnectionString("redis"), true);
return ConnectionMultiplexer.Connect(configuration); return ConnectionMultiplexer.Connect(configuration);
}); });

View File

@ -8,7 +8,7 @@ namespace Core.Interfaces
{ {
public interface IBasketRepository public interface IBasketRepository
{ {
Task<CustomerBasket> GetCustomerBasketAsync(string basketId); Task<CustomerBasket> GetBasketAsync(string basketId);
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket); Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
Task<bool> DeleteBasketAsysnc(string basketId); Task<bool> DeleteBasketAsysnc(string basketId);

View File

@ -1,27 +1,39 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Core.Entities; using Core.Entities;
using Core.Interfaces; using Core.Interfaces;
using StackExchange.Redis;
namespace Infrastructure.Data namespace Infrastructure.Data
{ {
public class BasketRepository : IBasketRepository public class BasketRepository : IBasketRepository
{ {
public Task<bool> DeleteBasketAsysnc(string basketId)
private readonly IDatabase _database;
public BasketRepository(IConnectionMultiplexer redis)
{ {
throw new NotImplementedException(); _database = redis.GetDatabase();
} }
public Task<CustomerBasket> GetCustomerBasketAsync(string basketId) public async Task<bool> DeleteBasketAsysnc(string basketId)
{ {
throw new NotImplementedException(); return await _database.KeyDeleteAsync(basketId);
} }
public Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket) public async Task<CustomerBasket> GetBasketAsync(string basketId)
{ {
throw new NotImplementedException(); var data = await _database.StringGetAsync(basketId);
return data.IsNullOrEmpty ? null : JsonSerializer.Deserialize<CustomerBasket>(data);
}
public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket)
{
var created = await _database.StringSetAsync(basket.Id, JsonSerializer.Serialize(basket), TimeSpan.FromDays(1));
if(!created) return null;
return await GetBasketAsync(basket.Id);
} }
} }
} }

23
docker-compose.yml Normal file
View File

@ -0,0 +1,23 @@
services:
redis:
image: redis:latest
ports:
- 6379:6379
command: ["redis-server", "--appendonly", "yes"]
volumes:
- redis-data:/data
redis-commander:
image: rediscommander/redis-commander:latest
environment:
- REDIS_HOSTS=local:redis:6379
- HTTP_USER=root
- HTTP_PASSWORD=secret
ports:
- 8081:8081
depends_on:
- redis
volumes:
redis-data: