Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions DeliveryService_ASP.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AuthAPI", "AuthAPI", "{D602
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeliveryService.AuthAPI", "Services\AuthAPI\DeliveryService.AuthAPI\DeliveryService.AuthAPI.csproj", "{65BFA16E-4839-4F88-988B-D922EB90AE6C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BasketAPI", "BasketAPI", "{6EF1F7C5-8F9C-4A0A-868B-4E1C2FB1E8E1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{0C72ACCB-B634-42D0-A957-A1F57C75C11B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{BB8F27D3-8523-42AE-B474-F8386236A1CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeliveryService.Services.BasketAPI.Infrastructure", "Services\BasketAPI\DeliveryService.Services.BasketAPI.Infrastructure\DeliveryService.Services.BasketAPI.Infrastructure.csproj", "{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeliveryService.Services.BasketAPI.Core", "Services\BasketAPI\DeliveryService.Services.BacketAPI.Core\DeliveryService.Services.BasketAPI.Core\DeliveryService.Services.BasketAPI.Core.csproj", "{75E21D7A-A473-4062-9E55-D78BF48527B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -251,6 +261,14 @@ Global
{65BFA16E-4839-4F88-988B-D922EB90AE6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65BFA16E-4839-4F88-988B-D922EB90AE6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65BFA16E-4839-4F88-988B-D922EB90AE6C}.Release|Any CPU.Build.0 = Release|Any CPU
{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE}.Release|Any CPU.Build.0 = Release|Any CPU
{75E21D7A-A473-4062-9E55-D78BF48527B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75E21D7A-A473-4062-9E55-D78BF48527B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75E21D7A-A473-4062-9E55-D78BF48527B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75E21D7A-A473-4062-9E55-D78BF48527B5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -308,6 +326,11 @@ Global
{0615BB07-258F-444C-916D-CAED870A014C} = {91CB81BB-753A-492E-A07F-A89284D5F090}
{D602E2F2-3A5A-483C-A55E-6F65E5597EAC} = {E6E51EE1-41B2-4693-BFCF-2ECDA6A48655}
{65BFA16E-4839-4F88-988B-D922EB90AE6C} = {D602E2F2-3A5A-483C-A55E-6F65E5597EAC}
{6EF1F7C5-8F9C-4A0A-868B-4E1C2FB1E8E1} = {E6E51EE1-41B2-4693-BFCF-2ECDA6A48655}
{0C72ACCB-B634-42D0-A957-A1F57C75C11B} = {6EF1F7C5-8F9C-4A0A-868B-4E1C2FB1E8E1}
{BB8F27D3-8523-42AE-B474-F8386236A1CD} = {6EF1F7C5-8F9C-4A0A-868B-4E1C2FB1E8E1}
{961065EC-6D53-42C9-ACBD-8DA3F8C7BBDE} = {BB8F27D3-8523-42AE-B474-F8386236A1CD}
{75E21D7A-A473-4062-9E55-D78BF48527B5} = {0C72ACCB-B634-42D0-A957-A1F57C75C11B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8C515027-DD6A-4FB2-A343-04CC8DDFD87E}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace DeliveryService.Services.BasketAPI.Core.Entities;

public class ShoppingCart
{
public string UserName { get; set; }

public List<ShoppingCartItem> Items { get; set; } = new List<ShoppingCartItem>();

public ShoppingCart(string userName)
{
UserName = userName;
}

public decimal TotalPrice
{
get
{
decimal totalPrice = 0;
foreach (var item in Items)
totalPrice += item.Price * item.Quantity;

return totalPrice;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DeliveryService.Services.BasketAPI.Core.Entities;

public class ShoppingCartItem
{
public string ProductId { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using DeliveryService.Services.BasketAPI.Core.Entities;

namespace DeliveryService.Services.BasketAPI.Core.Interfaces;

public interface IBasketRepository
{
Task<ShoppingCart> GetBasket(string userName);
Task<ShoppingCart> UpdateBasket(ShoppingCart basket);
Task<bool> DeleteBasket(string userName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols" Version="6.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.10" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DeliveryService.Services.BacketAPI.Core\DeliveryService.Services.BasketAPI.Core\DeliveryService.Services.BasketAPI.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Configuration;
using DeliveryService.Services.BasketAPI.Core.Interfaces;
using DeliveryService.Services.BasketAPI.Infrastructure.Repositories;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;

namespace DeliveryService.Services.BasketAPI.Infrastructure;

public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services,
IConfiguration configuration)
{
services.AddRedis(configuration);
return services;
}

public static IServiceCollection AddRedis(this IServiceCollection services,
IConfiguration configuration)
{
services.AddSingleton<IConnectionMultiplexer>(sp =>
ConnectionMultiplexer.Connect(new ConfigurationOptions
{
EndPoints = { configuration["CacheSettings:ConnectionString"] },
AbortOnConnectFail = false,
}));

services.AddScoped<IBasketRepository, BasketRepository>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using DeliveryService.Services.BasketAPI.Core.Entities;
using DeliveryService.Services.BasketAPI.Core.Interfaces;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace DeliveryService.Services.BasketAPI.Infrastructure.Repositories;

public class BasketRepository : IBasketRepository
{
private readonly IConnectionMultiplexer _connectionMultiplexer;
private readonly IDatabase _database;

public BasketRepository(IConnectionMultiplexer connectionMultiplexer)
{
_connectionMultiplexer = connectionMultiplexer;
_database = _connectionMultiplexer.GetDatabase();
}

public async Task<bool> DeleteBasket(string userName)
{
return await _database.KeyDeleteAsync(userName);
}

public async Task<ShoppingCart> GetBasket(string userName)
{
var basketString = await _database.StringGetAsync(userName);

if (basketString.HasValue)
{
return JsonConvert.DeserializeObject<ShoppingCart>(basketString.ToString());
}

return new ShoppingCart(userName);
}

public async Task<ShoppingCart> UpdateBasket(ShoppingCart basket)
{
var result = await _database.StringSetAsync(basket.UserName, JsonConvert.SerializeObject(basket));
if (result)
return await GetBasket(basket.UserName);

return basket;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IServiceCollection AddInfrastructure(
return services;
}

public static IServiceCollection AddAuth(this IServiceCollection services,
private static IServiceCollection AddAuth(this IServiceCollection services,
ConfigurationManager configuration)
{
var jwtSettings = new JwtSettings();
Expand Down