diff --git a/API/Controllers/BasketController.cs b/API/Controllers/BasketController.cs new file mode 100644 index 0000000..afe1d51 --- /dev/null +++ b/API/Controllers/BasketController.cs @@ -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> GetBasketById(string id) + { + var basket = await _basketRepository.GetBasketAsync(id); + return Ok(basket ?? new CustomerBasket(id)); + } + + [HttpPost] + public async Task> UpdateBasket(CustomerBasket basket) + { + var updatedBasket = await _basketRepository.UpdateBasketAsync(basket); + return Ok(updatedBasket); + } + + [HttpDelete] + public async Task DeleteBasket(string id) + { + await _basketRepository.DeleteBasketAsysnc(id); + } + } +} \ No newline at end of file diff --git a/API/Startup.cs b/API/Startup.cs index 2604b08..85a6fb1 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -24,7 +24,7 @@ namespace API services.AddApplicationServices(); services.AddSwaggerDocumentation(); services.AddDbContext(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); - services.AddSingleton(c => { + services.AddSingleton(c => { var configuration = ConfigurationOptions.Parse(_config.GetConnectionString("redis"), true); return ConnectionMultiplexer.Connect(configuration); }); diff --git a/Core/Interfaces/IBasketRepository.cs b/Core/Interfaces/IBasketRepository.cs index f89998f..0dc977d 100644 --- a/Core/Interfaces/IBasketRepository.cs +++ b/Core/Interfaces/IBasketRepository.cs @@ -8,7 +8,7 @@ namespace Core.Interfaces { public interface IBasketRepository { - Task GetCustomerBasketAsync(string basketId); + Task GetBasketAsync(string basketId); Task UpdateBasketAsync(CustomerBasket basket); Task DeleteBasketAsysnc(string basketId); diff --git a/Infrastructure/Data/BasketRepository.cs b/Infrastructure/Data/BasketRepository.cs index efb7883..6ca6a87 100644 --- a/Infrastructure/Data/BasketRepository.cs +++ b/Infrastructure/Data/BasketRepository.cs @@ -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 DeleteBasketAsysnc(string basketId) + + private readonly IDatabase _database; + public BasketRepository(IConnectionMultiplexer redis) { - throw new NotImplementedException(); + _database = redis.GetDatabase(); } - public Task GetCustomerBasketAsync(string basketId) + public async Task DeleteBasketAsysnc(string basketId) { - throw new NotImplementedException(); + return await _database.KeyDeleteAsync(basketId); } - public Task UpdateBasketAsync(CustomerBasket basket) + public async Task GetBasketAsync(string basketId) { - throw new NotImplementedException(); + var data = await _database.StringGetAsync(basketId); + return data.IsNullOrEmpty ? null : JsonSerializer.Deserialize(data); + } + + public async Task 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); } } } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..47e047f --- /dev/null +++ b/docker-compose.yml @@ -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: \ No newline at end of file