Use mongodb as backend database #1

Open
didiercrunch wants to merge 2 commits from mongodb into main
pull from: mongodb
13 changed files with 94 additions and 139 deletions
Showing only changes of commit a5ed2d866e - Show all commits

No files matched your search

@@ -20,4 +20,8 @@
<ProjectReference Include="..\RobotsWantedLeague\RobotsWantedLeague.csproj" /> <ProjectReference Include="..\RobotsWantedLeague\RobotsWantedLeague.csproj" />
</ItemGroup> </ItemGroup>
didiercrunch commented 2023-09-24 11:17:24 -04:00 (Migrated from github.com)
Review

No idea why this change is here. I hope it does not break anything.

No idea why this change is here. I hope it does not break anything.
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
</Project> </Project>
@@ -1,25 +0,0 @@
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
namespace RobotsWantedLeague.Test.Services;
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
using RobotsWantedLeague.Models;
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
using RobotsWantedLeague.Services;
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
[TestClass]
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
public class NotEmptyRobotsServiceTest
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
{
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
[TestMethod]
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
public void TestHappyPath()
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
{
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
IRobotsService service = new NotEmptyRobotsService();
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Assert.AreEqual(3, service.Robots.Count);
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Robot robot = service.CreateRobot("paul", 2, 3, "canada");
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Assert.AreEqual(robot.Name, "paul");
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Assert.AreEqual(robot.Weight, 2);
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Assert.AreEqual(4, service.Robots.Count);
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
service.DeleteRobotById(robot.Id);
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
Assert.AreEqual(3, service.Robots.Count);
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
}
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
}
didiercrunch commented 2023-09-24 11:18:57 -04:00 (Migrated from github.com)
Review

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
@@ -1,65 +0,0 @@
namespace RobotsWantedLeague.Test.Services;
using RobotsWantedLeague.Models;
using RobotsWantedLeague.Services;
[TestClass]
public class RobotsServiceTest
{
[TestMethod]
public void TestCreateRobot()
{
RobotsService service = new RobotsService();
Robot robot = service.CreateRobot("paul", 2, 3, "canada");
Assert.AreEqual(robot.Name, "paul");
Assert.AreEqual(robot.Weight, 2);
}
[TestMethod]
public void TestGetRobotById()
{
RobotsService service = new RobotsService();
Assert.IsNull(service.GetRobotById(98));
Robot robot = service.CreateRobot("paul", 2, 3, "canada");
Robot? ret = service.GetRobotById(robot.Id);
Assert.IsNotNull(ret);
Assert.AreEqual(robot.Name, ret.Name);
}
[TestMethod]
public void TestGetRobotById_MultipleRobots()
{
RobotsService service = new RobotsService();
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
Robot? ret = service.GetRobotById(robot2.Id);
Assert.IsNotNull(ret);
Assert.AreEqual(robot2.Name, ret.Name);
}
[TestMethod]
public void TestListRobots()
{
RobotsService service = new RobotsService();
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
Assert.AreEqual(2, service.Robots.Count);
}
[TestMethod]
public void TestDeleteRobotById()
{
RobotsService service = new RobotsService();
Robot robot1 = service.CreateRobot("paul", 2, 3, "canada");
Robot robot2 = service.CreateRobot("emanuel", 20, 30, "france");
Robot robot3 = service.CreateRobot("xu", 1, 9, "chine");
Assert.IsTrue(service.DeleteRobotById(robot2.Id));
Assert.AreEqual(2, service.Robots.Count);
Assert.IsNull(service.GetRobotById(robot2.Id));
}
}
@@ -37,8 +37,8 @@ public class RobotsController : Controller
return View(robotsService.Robots); return View(robotsService.Robots);
} }
public IActionResult Robot(int id){ public IActionResult Robot(string id){
didiercrunch commented 2023-09-24 11:19:17 -04:00 (Migrated from github.com)
Review

With mongodb, robot Ids are now strings.

With mongodb, robot Ids are now strings.
Robot robot = robotsService.GetRobotById(id); Robot? robot = robotsService.GetRobotById(id);
didiercrunch commented 2023-09-24 11:19:27 -04:00 (Migrated from github.com)
Review

fixed typing.

fixed typing.
return View(robot); return View(robot);
} }
+9 -2
View File
@@ -1,7 +1,14 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace RobotsWantedLeague.Models; namespace RobotsWantedLeague.Models;
public class Robot{ public class Robot{
public int Id { get; set;}
didiercrunch commented 2023-09-24 11:20:01 -04:00 (Migrated from github.com)
Review

This is important. I add custom annotation to make sure the robot ID matches the _id in mongodb.

This is important. I add custom annotation to make sure the robot ID matches the _id in mongodb.
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set;}
public string Name { get; set;} public string Name { get; set;}
/** /**
@@ -16,7 +23,7 @@ public class Robot{
public string Country { get; set;} public string Country { get; set;}
public Robot(int Id, string Name, int Weight, int Height, string Country){ public Robot(string? Id, string Name, int Weight, int Height, string Country){
this.Id = Id; this.Id = Id;
this.Name = Name; this.Name = Name;
this.Weight = Weight; this.Weight = Weight;
@@ -0,0 +1,11 @@
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
namespace RobotsWantedLeague.Models;
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
public class RobotsDatabaseSettings
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
{
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
public string ConnectionString { get; set; } = null!;
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
public string DatabaseName { get; set; } = null!;
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
public string RobotsCollectionName { get; set; } = null!;
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
}
didiercrunch commented 2023-09-24 11:21:39 -04:00 (Migrated from github.com)
Review

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
+7
View File
@@ -1,9 +1,16 @@
using RobotsWantedLeague.Models;
using RobotsWantedLeague.Services; using RobotsWantedLeague.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// database settings
builder.Services.Configure<RobotsDatabaseSettings>(
didiercrunch commented 2023-09-24 11:22:54 -04:00 (Migrated from github.com)
Review

This is the important part of the config change. This is what is needed to wire up mongodb as an ASP service.

This is the important part of the config change. This is what is needed to wire up mongodb as an ASP service.
builder.Configuration.GetSection("RobotsDatabase"));
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<RobotsService>();
didiercrunch commented 2023-09-24 11:23:22 -04:00 (Migrated from github.com)
Review

Little quirk to keep NotEmptyRobotsService working.

Little quirk to keep NotEmptyRobotsService working.
builder.Services.AddSingleton<IRobotsService, NotEmptyRobotsService>(); builder.Services.AddSingleton<IRobotsService, NotEmptyRobotsService>();
var app = builder.Build(); var app = builder.Build();
@@ -6,4 +6,13 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
didiercrunch commented 2023-09-24 11:24:51 -04:00 (Migrated from github.com)
Review

This adds mongodb as a dependency to the project. This file was changed automagically via

dotnet add package MongoDB.Driver
This adds mongodb as a dependency to the project. This file was changed automagically via ```shell dotnet add package MongoDB.Driver ```
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Entities\" />
<Folder Include="Repositories\" />
</ItemGroup>
</Project> </Project>
@@ -9,7 +9,7 @@ public interface IRobotsService{
int height, int height,
string country); string country);
public Robot? GetRobotById(int id); public Robot? GetRobotById(string id);
didiercrunch commented 2023-09-24 11:25:06 -04:00 (Migrated from github.com)
Review

Migrated ids from int to string.

Migrated ids from int to string.
public bool DeleteRobotById(int id); public bool DeleteRobotById(string id);
} }
@@ -1,3 +1,6 @@
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace RobotsWantedLeague.Services; namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models; using RobotsWantedLeague.Models;
@@ -7,12 +10,22 @@ public class NotEmptyRobotsService: IRobotsService
private readonly IRobotsService underlyingRobotsService; private readonly IRobotsService underlyingRobotsService;
public List<Robot> Robots { get => underlyingRobotsService.Robots; } public List<Robot> Robots { get => underlyingRobotsService.Robots; }
public NotEmptyRobotsService() public NotEmptyRobotsService(RobotsService underlyingRobotsService)
{ {
this.underlyingRobotsService = new RobotsService(); this.underlyingRobotsService = underlyingRobotsService;
fillDefaultRobotsWhenUnderlyingRobotServiceIsEmpty();
}
private void fillDefaultRobotsWhenUnderlyingRobotServiceIsEmpty()
{
if (underlyingRobotsService.Robots.Count() != 0)
didiercrunch commented 2023-09-24 11:26:38 -04:00 (Migrated from github.com)
Review

We changed a little bit how we build NotEmptyRobotsService. Now we ask ASP to auto wire the RobotsService for us. We need to do this because RobotsService depends on mongodb which can only be build via ASP auto wiring mechanism.

We changed a little bit how we build `NotEmptyRobotsService`. Now we ask ASP to auto wire the `RobotsService` for us. We need to do this because `RobotsService` depends on mongodb which can only be build via ASP auto wiring mechanism.
{
return;
}
this.underlyingRobotsService.CreateRobot("Alice", 1050, 2, "Bhutan"); this.underlyingRobotsService.CreateRobot("Alice", 1050, 2, "Bhutan");
this.underlyingRobotsService.CreateRobot("Bob", 5001, 5, "Vanuatu"); this.underlyingRobotsService.CreateRobot("Bob", 5001, 5, "Vanuatu");
this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan"); this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan");
} }
didiercrunch commented 2023-09-24 11:26:57 -04:00 (Migrated from github.com)
Review

Made sure to only add new robots if the db is empty.

Made sure to only add new robots if the db is empty.
public Robot CreateRobot(string name, public Robot CreateRobot(string name,
@@ -20,15 +33,16 @@ public class NotEmptyRobotsService: IRobotsService
int height, int height,
string country) string country)
{ {
return underlyingRobotsService.CreateRobot(name, weight, height, country); var robot = underlyingRobotsService.CreateRobot(name, weight, height, country);
return robot;
} }
public Robot? GetRobotById(int id){ public Robot? GetRobotById(string id){
return underlyingRobotsService.GetRobotById(id); return underlyingRobotsService.GetRobotById(id);
} }
public bool DeleteRobotById(int id){ public bool DeleteRobotById(string id){
return underlyingRobotsService.DeleteRobotById(id); return underlyingRobotsService.DeleteRobotById(id);
} }
+26 -35
View File
@@ -1,21 +1,25 @@
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace RobotsWantedLeague.Services; namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models; using RobotsWantedLeague.Models;
public class RobotsService: IRobotsService public class RobotsService: IRobotsService
{ {
private readonly List<Robot> robots; private readonly IMongoCollection<Robot> robotsCollection;
private int idGenerator = 0;
public List<Robot> Robots { get => robots; }
public RobotsService() public List<Robot> Robots { get => robotsCollection.AsQueryable().ToList(); }
didiercrunch commented 2023-09-24 11:28:05 -04:00 (Migrated from github.com)
Review

So instead of a List<robot> we now depend on a IMongoCollection<Robot>. The api of IMongoCollection<Robot> is quite nice.

So instead of a `List<robot>` we now depend on a `IMongoCollection<Robot>`. The api of `IMongoCollection<Robot>` is quite nice.
public RobotsService(IOptions<RobotsDatabaseSettings> bookStoreDatabaseSettings)
{ {
didiercrunch commented 2023-09-24 11:29:37 -04:00 (Migrated from github.com)
Review

To return the list of all the robot we need to first fetch them. robotsCollection.AsQueryable() returns an object which is queryable via linq. The query here is empty (aka take all) and the method to realize the query is ToList.

To return the list of all the robot we need to first fetch them. `robotsCollection.AsQueryable()` returns an object which is queryable via linq. The query here is empty (aka take all) and the method to realize the query is `ToList`.
robots = new List<Robot>(); var mongoClient = new MongoClient(
} bookStoreDatabaseSettings.Value.ConnectionString);
private int generateId(){ var mongoDatabase = mongoClient.GetDatabase(
idGenerator = idGenerator + 1; bookStoreDatabaseSettings.Value.DatabaseName);
return idGenerator;
this.robotsCollection = mongoDatabase.GetCollection<Robot>(bookStoreDatabaseSettings.Value.RobotsCollectionName);
didiercrunch commented 2023-09-24 11:30:27 -04:00 (Migrated from github.com)
Review

This is how we manage to build the IMongoCollection. The only logic here is googlin g + copy/pasting

This is how we manage to build the `IMongoCollection`. The only logic here is googlin g + copy/pasting
} }
public Robot CreateRobot(string name, public Robot CreateRobot(string name,
@@ -23,37 +27,24 @@ public class RobotsService: IRobotsService
int height, int height,
string country) string country)
{ {
var robot = new Robot(generateId(), name, weight, height, country); var robot = new Robot(null, name, weight, height, country);
robots.Add(robot); robotsCollection.InsertOne(robot);
return robot; return robot;
} }
private int getIndexOfRobotById(int id){
int idx = 0; public Robot? GetRobotById(string id)
foreach (Robot robot in robots){ {
if (robot.Id == id){ IQueryable<Robot> q = from robot in robotsCollection.AsQueryable()
return idx; where robot.Id == id
} select robot;
idx ++; return q.First();
}
return -1;
} }
didiercrunch commented 2023-09-24 11:30:51 -04:00 (Migrated from github.com)
Review

To insert, we now need to call InsertOne, as in mongodb

To insert, we now need to call InsertOne, as in mongodb
public Robot? GetRobotById(int id){ public bool DeleteRobotById(string id)
int indexToDelete = getIndexOfRobotById(id); {
if(indexToDelete == -1){ var res = robotsCollection.DeleteOne(robot => robot.Id == id);
return null; return res != null ;
}
return robots[indexToDelete];
}
public bool DeleteRobotById(int id){
int indexToDelete = getIndexOfRobotById(id);
if(indexToDelete == -1){
return false;
}
robots.RemoveAt(indexToDelete);
return true;
} }
} }
@@ -83,9 +83,6 @@
</div> </div>
<footer class="border-top footer text-muted"> <footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - RobotsWantedLeague - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer> </footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
+5
View File
@@ -5,5 +5,10 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"RobotsDatabase": {
didiercrunch commented 2023-09-24 11:32:32 -04:00 (Migrated from github.com)
Review

This is the configuration needed to run the program using a mongodb instance running locally on mongodb's default port.

This is the configuration needed to run the program using a mongodb instance running locally on mongodb's default port.
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Robots",
"RobotsCollectionName": "Robots"
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }