From e18931b6c06da622d21352a5a56632a7c5d27ea6 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sat, 23 Sep 2023 08:01:20 -0400 Subject: [PATCH 1/2] mondify gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 617cf3b..8b2f5ad 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,5 @@ $RECYCLE.BIN/ _NCrunch* .vscode +.idea +mongodb -- 2.55.0 From a5ed2d866ec436593e65b033fedb508be470842e Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sun, 24 Sep 2023 11:14:23 -0400 Subject: [PATCH 2/2] used mongodb as database --- .../RobotsWantedLeague.Test.csproj | 4 ++ .../Services/NotEmptyRobotsServiceTest.cs | 25 ------- .../Services/RobotsServiceTest.cs | 65 ------------------- .../Controllers/RobotsController.cs | 4 +- RobotsWantedLeague/Models/Robot.cs | 11 +++- .../Models/RobotsDatabaseSettings.cs | 11 ++++ RobotsWantedLeague/Program.cs | 7 ++ RobotsWantedLeague/RobotsWantedLeague.csproj | 9 +++ RobotsWantedLeague/Services/IRobotsService.cs | 4 +- .../Services/NotEmptyRobotsService.cs | 24 +++++-- RobotsWantedLeague/Services/RobotsService.cs | 61 ++++++++--------- .../Views/Shared/_Layout.cshtml | 3 - RobotsWantedLeague/appsettings.json | 5 ++ 13 files changed, 94 insertions(+), 139 deletions(-) delete mode 100644 RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs delete mode 100644 RobotsWantedLeague.Test/Services/RobotsServiceTest.cs create mode 100644 RobotsWantedLeague/Models/RobotsDatabaseSettings.cs diff --git a/RobotsWantedLeague.Test/RobotsWantedLeague.Test.csproj b/RobotsWantedLeague.Test/RobotsWantedLeague.Test.csproj index 7ff1402..64574ca 100644 --- a/RobotsWantedLeague.Test/RobotsWantedLeague.Test.csproj +++ b/RobotsWantedLeague.Test/RobotsWantedLeague.Test.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs b/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs deleted file mode 100644 index ef808a1..0000000 --- a/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace RobotsWantedLeague.Test.Services; -using RobotsWantedLeague.Models; -using RobotsWantedLeague.Services; - -[TestClass] -public class NotEmptyRobotsServiceTest -{ - - [TestMethod] - public void TestHappyPath() - { - IRobotsService service = new NotEmptyRobotsService(); - Assert.AreEqual(3, service.Robots.Count); - Robot robot = service.CreateRobot("paul", 2, 3, "canada"); - Assert.AreEqual(robot.Name, "paul"); - Assert.AreEqual(robot.Weight, 2); - - Assert.AreEqual(4, service.Robots.Count); - - service.DeleteRobotById(robot.Id); - Assert.AreEqual(3, service.Robots.Count); - } - - -} \ No newline at end of file diff --git a/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs b/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs deleted file mode 100644 index a80d26e..0000000 --- a/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs +++ /dev/null @@ -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)); - } - - -} \ No newline at end of file diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index 3ddc848..03f7918 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -37,8 +37,8 @@ public class RobotsController : Controller return View(robotsService.Robots); } - public IActionResult Robot(int id){ - Robot robot = robotsService.GetRobotById(id); + public IActionResult Robot(string id){ + Robot? robot = robotsService.GetRobotById(id); return View(robot); } diff --git a/RobotsWantedLeague/Models/Robot.cs b/RobotsWantedLeague/Models/Robot.cs index c3bd0f7..33922ff 100644 --- a/RobotsWantedLeague/Models/Robot.cs +++ b/RobotsWantedLeague/Models/Robot.cs @@ -1,7 +1,14 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + namespace RobotsWantedLeague.Models; public class Robot{ - public int Id { get; set;} + + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string? Id { get; set;} + public string Name { get; set;} /** @@ -16,7 +23,7 @@ public class Robot{ 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.Name = Name; this.Weight = Weight; diff --git a/RobotsWantedLeague/Models/RobotsDatabaseSettings.cs b/RobotsWantedLeague/Models/RobotsDatabaseSettings.cs new file mode 100644 index 0000000..e74937c --- /dev/null +++ b/RobotsWantedLeague/Models/RobotsDatabaseSettings.cs @@ -0,0 +1,11 @@ +namespace RobotsWantedLeague.Models; + +public class RobotsDatabaseSettings +{ + + public string ConnectionString { get; set; } = null!; + + public string DatabaseName { get; set; } = null!; + + public string RobotsCollectionName { get; set; } = null!; +} \ No newline at end of file diff --git a/RobotsWantedLeague/Program.cs b/RobotsWantedLeague/Program.cs index 5718be6..42607bb 100644 --- a/RobotsWantedLeague/Program.cs +++ b/RobotsWantedLeague/Program.cs @@ -1,9 +1,16 @@ +using RobotsWantedLeague.Models; using RobotsWantedLeague.Services; var builder = WebApplication.CreateBuilder(args); +// database settings + +builder.Services.Configure( + builder.Configuration.GetSection("RobotsDatabase")); + // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/RobotsWantedLeague/RobotsWantedLeague.csproj b/RobotsWantedLeague/RobotsWantedLeague.csproj index 4c2bb77..790f167 100644 --- a/RobotsWantedLeague/RobotsWantedLeague.csproj +++ b/RobotsWantedLeague/RobotsWantedLeague.csproj @@ -6,4 +6,13 @@ enable + + + + + + + + + diff --git a/RobotsWantedLeague/Services/IRobotsService.cs b/RobotsWantedLeague/Services/IRobotsService.cs index d8e3302..45bd46d 100644 --- a/RobotsWantedLeague/Services/IRobotsService.cs +++ b/RobotsWantedLeague/Services/IRobotsService.cs @@ -9,7 +9,7 @@ public interface IRobotsService{ int height, string country); - public Robot? GetRobotById(int id); + public Robot? GetRobotById(string id); - public bool DeleteRobotById(int id); + public bool DeleteRobotById(string id); } \ No newline at end of file diff --git a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs b/RobotsWantedLeague/Services/NotEmptyRobotsService.cs index bfc193e..37e41b9 100644 --- a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs +++ b/RobotsWantedLeague/Services/NotEmptyRobotsService.cs @@ -1,3 +1,6 @@ +using Microsoft.Extensions.Options; +using MongoDB.Driver; + namespace RobotsWantedLeague.Services; using RobotsWantedLeague.Models; @@ -7,12 +10,22 @@ public class NotEmptyRobotsService: IRobotsService private readonly IRobotsService underlyingRobotsService; public List 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) + { + return; + } this.underlyingRobotsService.CreateRobot("Alice", 1050, 2, "Bhutan"); this.underlyingRobotsService.CreateRobot("Bob", 5001, 5, "Vanuatu"); this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan"); + } public Robot CreateRobot(string name, @@ -20,15 +33,16 @@ public class NotEmptyRobotsService: IRobotsService int height, 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); } - public bool DeleteRobotById(int id){ + public bool DeleteRobotById(string id){ return underlyingRobotsService.DeleteRobotById(id); } diff --git a/RobotsWantedLeague/Services/RobotsService.cs b/RobotsWantedLeague/Services/RobotsService.cs index a110c47..368c5da 100644 --- a/RobotsWantedLeague/Services/RobotsService.cs +++ b/RobotsWantedLeague/Services/RobotsService.cs @@ -1,21 +1,25 @@ +using Microsoft.Extensions.Options; +using MongoDB.Driver; + namespace RobotsWantedLeague.Services; using RobotsWantedLeague.Models; public class RobotsService: IRobotsService { - private readonly List robots; - private int idGenerator = 0; - public List Robots { get => robots; } + private readonly IMongoCollection robotsCollection; - public RobotsService() + public List Robots { get => robotsCollection.AsQueryable().ToList(); } + + public RobotsService(IOptions bookStoreDatabaseSettings) { - robots = new List(); - } + var mongoClient = new MongoClient( + bookStoreDatabaseSettings.Value.ConnectionString); - private int generateId(){ - idGenerator = idGenerator + 1; - return idGenerator; + var mongoDatabase = mongoClient.GetDatabase( + bookStoreDatabaseSettings.Value.DatabaseName); + + this.robotsCollection = mongoDatabase.GetCollection(bookStoreDatabaseSettings.Value.RobotsCollectionName); } public Robot CreateRobot(string name, @@ -23,37 +27,24 @@ public class RobotsService: IRobotsService int height, string country) { - var robot = new Robot(generateId(), name, weight, height, country); - robots.Add(robot); + var robot = new Robot(null, name, weight, height, country); + robotsCollection.InsertOne(robot); return robot; } + - private int getIndexOfRobotById(int id){ - int idx = 0; - foreach (Robot robot in robots){ - if (robot.Id == id){ - return idx; - } - idx ++; - } - return -1; + public Robot? GetRobotById(string id) + { + IQueryable q = from robot in robotsCollection.AsQueryable() + where robot.Id == id + select robot; + return q.First(); } - public Robot? GetRobotById(int id){ - int indexToDelete = getIndexOfRobotById(id); - if(indexToDelete == -1){ - return null; - } - return robots[indexToDelete]; - } - - public bool DeleteRobotById(int id){ - int indexToDelete = getIndexOfRobotById(id); - if(indexToDelete == -1){ - return false; - } - robots.RemoveAt(indexToDelete); - return true; + public bool DeleteRobotById(string id) + { + var res = robotsCollection.DeleteOne(robot => robot.Id == id); + return res != null ; } } \ No newline at end of file diff --git a/RobotsWantedLeague/Views/Shared/_Layout.cshtml b/RobotsWantedLeague/Views/Shared/_Layout.cshtml index 9029f1f..4570c8a 100644 --- a/RobotsWantedLeague/Views/Shared/_Layout.cshtml +++ b/RobotsWantedLeague/Views/Shared/_Layout.cshtml @@ -83,9 +83,6 @@
-
- © 2023 - RobotsWantedLeague - Privacy -
diff --git a/RobotsWantedLeague/appsettings.json b/RobotsWantedLeague/appsettings.json index 10f68b8..a7ee69f 100644 --- a/RobotsWantedLeague/appsettings.json +++ b/RobotsWantedLeague/appsettings.json @@ -5,5 +5,10 @@ "Microsoft.AspNetCore": "Warning" } }, + "RobotsDatabase": { + "ConnectionString": "mongodb://localhost:27017", + "DatabaseName": "Robots", + "RobotsCollectionName": "Robots" + }, "AllowedHosts": "*" } -- 2.55.0