Skip to content

Commit a598bb7

Browse files
committed
Add skeleton of project for C# code interview
1 parent 66e79b7 commit a598bb7

15 files changed

Lines changed: 249 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,6 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
# intellij configs
353+
.idea/

WealthKernel.ShoppingCart.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WealthKernel.ShoppingCart", "src\WealthKernel.ShoppingCart.csproj", "{8419B160-5AF8-4F58-ADCC-46A7342B86A3}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{8419B160-5AF8-4F58-ADCC-46A7342B86A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{8419B160-5AF8-4F58-ADCC-46A7342B86A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{8419B160-5AF8-4F58-ADCC-46A7342B86A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{8419B160-5AF8-4F58-ADCC-46A7342B86A3}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

src/Bus/Command.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using MediatR;
2+
3+
namespace WealthKernel.ShoppingCart.Bus
4+
{
5+
public class Command : INotification {}
6+
}

src/Bus/CommandHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MediatR;
2+
3+
namespace WealthKernel.ShoppingCart.Bus
4+
{
5+
public interface ICommandHandler<in T> : INotificationHandler<T> where T : INotification
6+
{
7+
}
8+
}

src/Bus/ExampleCommand.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using MediatR;
4+
5+
namespace WealthKernel.ShoppingCart.Bus
6+
{
7+
/// <summary>
8+
/// To use the command, you just need to inherit from Command
9+
/// and then you can create an ICommandHandler to do something with it
10+
/// </summary>
11+
public class ExampleCommand : Command
12+
{
13+
14+
}
15+
16+
public class ExampleCommandHandler : ICommandHandler<ExampleCommand>
17+
{
18+
public Task Handle(ExampleCommand notification, CancellationToken cancellationToken)
19+
{
20+
// Do something here!
21+
22+
return Task.CompletedTask;
23+
}
24+
}
25+
}

src/Bus/SimpleBus.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Threading.Tasks;
2+
using MediatR;
3+
4+
namespace WealthKernel.ShoppingCart.Bus
5+
{
6+
public class SimpleBus
7+
{
8+
private readonly IMediator _mediator;
9+
10+
public SimpleBus(IMediator mediator)
11+
{
12+
_mediator = mediator;
13+
}
14+
15+
public async Task Send(Command command)
16+
{
17+
await _mediator.Publish(command);
18+
}
19+
}
20+
}

src/Data/ShoppingCartRepository.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using WealthKernel.ShoppingCart.Models;
6+
7+
namespace WealthKernel.ShoppingCart.Data
8+
{
9+
public class ShoppingCartRepository
10+
{
11+
private readonly ConcurrentDictionary<string,CartItem> _items;
12+
13+
public ShoppingCartRepository()
14+
{
15+
_items = new ConcurrentDictionary<string, CartItem>();
16+
}
17+
18+
public void AddItem(CartItem item)
19+
{
20+
var added = _items.TryAdd(item.Id, item);
21+
22+
if (!added)
23+
{
24+
throw new InvalidOperationException($"Item {item.Id} already exists!");
25+
}
26+
}
27+
28+
public CartItem? GetItem(string id)
29+
{
30+
_items.TryGetValue(id, out var item);
31+
return item;
32+
}
33+
34+
public IList<CartItem> SearchItems(string? id, string? name)
35+
{
36+
var itemsQueryable = _items.Values.AsQueryable();
37+
38+
if (!string.IsNullOrEmpty(id))
39+
{
40+
return new[] { itemsQueryable.Single(item => item.Id == id) };
41+
}
42+
43+
if (!string.IsNullOrEmpty(name))
44+
{
45+
itemsQueryable = itemsQueryable.Where(r => r.Name == name);
46+
}
47+
48+
return itemsQueryable.ToArray();
49+
}
50+
}
51+
}

src/Models/CartItem.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace WealthKernel.ShoppingCart.Models
2+
{
3+
public class CartItem
4+
{
5+
public string Id { get; set; }
6+
7+
public string Name { get; set; }
8+
}
9+
}

src/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace WealthKernel.ShoppingCart
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
22+
}
23+
}

src/Properties/launchSettings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"WealthKernel.ShoppingCart": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"applicationUrl": "https://localhost:33333;http://localhost:22222",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)