From 9ad1d509dfacd48f5b5ca9456d928e793df959a6 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 14 Sep 2023 10:27:25 -0400 Subject: [PATCH] Created basic RobotServicew --- .../Services/RobotsServiceTest.cs | 65 +++++++++++++++++++ RobotsWantedLeague/Models/Robot.cs | 27 ++++++++ RobotsWantedLeague/Program.cs | 4 +- RobotsWantedLeague/Services/IRobotsService.cs | 15 +++++ RobotsWantedLeague/Services/RobotsService.cs | 59 +++++++++++++++++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 RobotsWantedLeague.Test/Services/RobotsServiceTest.cs create mode 100644 RobotsWantedLeague/Models/Robot.cs create mode 100644 RobotsWantedLeague/Services/IRobotsService.cs create mode 100644 RobotsWantedLeague/Services/RobotsService.cs diff --git a/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs b/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs new file mode 100644 index 0000000..a80d26e --- /dev/null +++ b/RobotsWantedLeague.Test/Services/RobotsServiceTest.cs @@ -0,0 +1,65 @@ +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/Models/Robot.cs b/RobotsWantedLeague/Models/Robot.cs new file mode 100644 index 0000000..c3bd0f7 --- /dev/null +++ b/RobotsWantedLeague/Models/Robot.cs @@ -0,0 +1,27 @@ +namespace RobotsWantedLeague.Models; + +public class Robot{ + public int Id { get; set;} + public string Name { get; set;} + + /** + * Weight in kg. + */ + public int Weight { get; set;} + + /** + * Height in m. + */ + public int Height { get; set;} + + public string Country { get; set;} + + public Robot(int Id, string Name, int Weight, int Height, string Country){ + this.Id = Id; + this.Name = Name; + this.Weight = Weight; + this.Height = Height; + this.Country = Country; + } + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Program.cs b/RobotsWantedLeague/Program.cs index 0727468..d1668a2 100644 --- a/RobotsWantedLeague/Program.cs +++ b/RobotsWantedLeague/Program.cs @@ -1,8 +1,10 @@ +using RobotsWantedLeague.Services; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); - +builder.Services.AddSingleton(); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/RobotsWantedLeague/Services/IRobotsService.cs b/RobotsWantedLeague/Services/IRobotsService.cs new file mode 100644 index 0000000..d8e3302 --- /dev/null +++ b/RobotsWantedLeague/Services/IRobotsService.cs @@ -0,0 +1,15 @@ +namespace RobotsWantedLeague.Services; +using RobotsWantedLeague.Models; + +public interface IRobotsService{ + public List Robots { get; } + + public Robot CreateRobot(string name, + int weight, + int height, + string country); + + public Robot? GetRobotById(int id); + + public bool DeleteRobotById(int id); +} \ No newline at end of file diff --git a/RobotsWantedLeague/Services/RobotsService.cs b/RobotsWantedLeague/Services/RobotsService.cs new file mode 100644 index 0000000..a110c47 --- /dev/null +++ b/RobotsWantedLeague/Services/RobotsService.cs @@ -0,0 +1,59 @@ +namespace RobotsWantedLeague.Services; + +using RobotsWantedLeague.Models; + +public class RobotsService: IRobotsService +{ + private readonly List robots; + private int idGenerator = 0; + public List Robots { get => robots; } + + public RobotsService() + { + robots = new List(); + } + + private int generateId(){ + idGenerator = idGenerator + 1; + return idGenerator; + } + + public Robot CreateRobot(string name, + int weight, + int height, + string country) + { + var robot = new Robot(generateId(), name, weight, height, country); + robots.Add(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(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; + } + +} \ No newline at end of file