Added Redis, Fixed Redis Delete
This commit is contained in:
parent
0ce1d055c7
commit
bcbad19601
35
API/Controllers/BasketController.cs
Normal file
35
API/Controllers/BasketController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ namespace API
|
||||
services.AddApplicationServices();
|
||||
services.AddSwaggerDocumentation();
|
||||
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);
|
||||
return ConnectionMultiplexer.Connect(configuration);
|
||||
});
|
||||
|
@ -8,7 +8,7 @@ namespace Core.Interfaces
|
||||
{
|
||||
public interface IBasketRepository
|
||||
{
|
||||
Task<CustomerBasket> GetCustomerBasketAsync(string basketId);
|
||||
Task<CustomerBasket> GetBasketAsync(string basketId);
|
||||
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
|
||||
Task<bool> DeleteBasketAsysnc(string basketId);
|
||||
|
||||
|
@ -1,27 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Core.Entities;
|
||||
using Core.Interfaces;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Infrastructure.Data
|
||||
{
|
||||
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
23
docker-compose.yml
Normal 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:
|
Loading…
Reference in New Issue
Block a user