From 1324b63df8c5d0e68e6ae63ff5c94ec27bc7204e Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sat, 30 Mar 2024 17:47:14 +0100 Subject: [PATCH 01/24] rm task 0 leftovers --- Task_0/DistanceCalculator.sln | 31 --------- .../DistanceCalculator.csproj | 10 --- Task_0/DistanceCalculator/PointSample.cs | 66 ------------------- .../DistanceCalculatorTests.cs | 50 -------------- .../DistanceCalculatorTests.csproj | 27 -------- 5 files changed, 184 deletions(-) delete mode 100644 Task_0/DistanceCalculator.sln delete mode 100644 Task_0/DistanceCalculator/DistanceCalculator.csproj delete mode 100644 Task_0/DistanceCalculator/PointSample.cs delete mode 100644 Task_0/DistanceCalculatorTests/DistanceCalculatorTests.cs delete mode 100644 Task_0/DistanceCalculatorTests/DistanceCalculatorTests.csproj diff --git a/Task_0/DistanceCalculator.sln b/Task_0/DistanceCalculator.sln deleted file mode 100644 index 5e69c28..0000000 --- a/Task_0/DistanceCalculator.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.5.002.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DistanceCalculatorTests", "DistanceCalculatorTests\DistanceCalculatorTests.csproj", "{C669DFFA-E38F-4496-AA5C-EFA20D77D4E0}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DistanceCalculator", "DistanceCalculator\DistanceCalculator.csproj", "{E1B0F168-4A94-476E-B3E9-1F1D65DF6C25}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C669DFFA-E38F-4496-AA5C-EFA20D77D4E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C669DFFA-E38F-4496-AA5C-EFA20D77D4E0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C669DFFA-E38F-4496-AA5C-EFA20D77D4E0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C669DFFA-E38F-4496-AA5C-EFA20D77D4E0}.Release|Any CPU.Build.0 = Release|Any CPU - {E1B0F168-4A94-476E-B3E9-1F1D65DF6C25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E1B0F168-4A94-476E-B3E9-1F1D65DF6C25}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E1B0F168-4A94-476E-B3E9-1F1D65DF6C25}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E1B0F168-4A94-476E-B3E9-1F1D65DF6C25}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {0BA0AB8E-FD21-491D-A0A8-97992369C07E} - EndGlobalSection -EndGlobal diff --git a/Task_0/DistanceCalculator/DistanceCalculator.csproj b/Task_0/DistanceCalculator/DistanceCalculator.csproj deleted file mode 100644 index 2150e37..0000000 --- a/Task_0/DistanceCalculator/DistanceCalculator.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - Exe - net8.0 - enable - enable - - - diff --git a/Task_0/DistanceCalculator/PointSample.cs b/Task_0/DistanceCalculator/PointSample.cs deleted file mode 100644 index 2ca152b..0000000 --- a/Task_0/DistanceCalculator/PointSample.cs +++ /dev/null @@ -1,66 +0,0 @@ - -namespace PointSample -{ - public class Point(int x, int y) - { - private int _x = x; - private int _y = y; - - public int GetX() - { - return _x; - } - - public void SetX(int x) - { - _x = x; - } - - public int GetY() - { - return _y; - } - - public void SetY(int y) - { - _y = y; - } - } - - public class DistanceCalculator - { - public static double CalculateDistanceBetween2Points(Point point1, Point point2) - { - if (point1 == null) - { - throw new ArgumentNullException(nameof(point1), "Point 1 cannot be null"); - } - - if (point2 == null) - { - throw new ArgumentNullException(nameof(point2), "Point 2 cannot be null"); - } - - int deltaX = point1.GetX() - point2.GetX(); - int deltaY = point1.GetY() - point2.GetY(); - - return Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2)); - } - } - - - class Program - { - static void Main(string[] args) - { - Point point1 = new Point(1, 1); - Point point2 = new Point(4, 5); - - double distance = DistanceCalculator.CalculateDistanceBetween2Points(point1, point2); - - Console.WriteLine($"Distance between point1 and point2: {distance}"); - } - } -} - - diff --git a/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.cs b/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.cs deleted file mode 100644 index 14f9bc8..0000000 --- a/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.cs +++ /dev/null @@ -1,50 +0,0 @@ -using PointSample; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace PointSample.DistanceCalculatorTests -{ - - [TestClass] - public class DistanceCalculatorTests - { - [TestMethod] - public void CalculateDistanceBetween2Points_SamePoint_ReturnsZero() - { - Point point1 = new Point(3, 4); - Point point2 = new Point(3, 4); - - double distance = DistanceCalculator.CalculateDistanceBetween2Points(point1, point2); - - Assert.AreEqual(0, distance); - } - - [TestMethod] - public void CalculateDistanceBetween2Points_ValidPoints_ReturnsCorrectDistance() - { - Point point1 = new Point(1, 1); - Point point2 = new Point(4, 5); - - double distance = DistanceCalculator.CalculateDistanceBetween2Points(point1, point2); - - Assert.AreEqual(5, distance); - } - - [TestMethod] - public void CalculateDistanceBetween2Points_NullPoint1_ThrowsArgumentNullException() - { - Point point1 = null; - Point point2 = new Point(4, 5); - - Assert.ThrowsException(() => DistanceCalculator.CalculateDistanceBetween2Points(point1, point2)); - } - - [TestMethod] - public void CalculateDistanceBetween2Points_NullPoint2_ThrowsArgumentNullException() - { - Point point1 = new Point(1, 1); - Point point2 = null; - - Assert.ThrowsException(() => DistanceCalculator.CalculateDistanceBetween2Points(point1, point2)); - } - } -} diff --git a/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.csproj b/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.csproj deleted file mode 100644 index b8f9c03..0000000 --- a/Task_0/DistanceCalculatorTests/DistanceCalculatorTests.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - - net8.0 - enable - enable - - false - true - - - - - - - - - - - - - - - - - - \ No newline at end of file From 1294cb8567258d5b327b3c114b9775f2c3624c78 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sat, 30 Mar 2024 20:53:33 +0100 Subject: [PATCH 02/24] init DataLayer in Library business process --- Library.sln | 31 ++++++++++++++++++++++++++++++ Library/DataLayer/DataLayer.csproj | 9 +++++++++ 2 files changed, 40 insertions(+) create mode 100644 Library.sln create mode 100644 Library/DataLayer/DataLayer.csproj diff --git a/Library.sln b/Library.sln new file mode 100644 index 0000000..5c23fc1 --- /dev/null +++ b/Library.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLayer", "Library\DataLayer\DataLayer.csproj", "{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Library\Tests\Tests.csproj", "{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Release|Any CPU.Build.0 = Release|Any CPU + {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B8DC2F37-8FC7-433C-BA78-56CAE077B47B} + EndGlobalSection +EndGlobal diff --git a/Library/DataLayer/DataLayer.csproj b/Library/DataLayer/DataLayer.csproj new file mode 100644 index 0000000..e1b91f9 --- /dev/null +++ b/Library/DataLayer/DataLayer.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + \ No newline at end of file From 2d387710dc72e3f4b7038b98bfff00f05888fdaf Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sat, 30 Mar 2024 20:54:08 +0100 Subject: [PATCH 03/24] add few business process interfaces --- Library/DataLayer/API/IBook.cs | 10 ++++++++++ Library/DataLayer/API/IDataContext.cs | 9 +++++++++ Library/DataLayer/API/IDataRepository.cs | 22 ++++++++++++++++++++++ Library/DataLayer/API/IProduct.cs | 9 +++++++++ Library/DataLayer/API/IUser.cs | 16 ++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 Library/DataLayer/API/IBook.cs create mode 100644 Library/DataLayer/API/IDataContext.cs create mode 100644 Library/DataLayer/API/IDataRepository.cs create mode 100644 Library/DataLayer/API/IProduct.cs create mode 100644 Library/DataLayer/API/IUser.cs diff --git a/Library/DataLayer/API/IBook.cs b/Library/DataLayer/API/IBook.cs new file mode 100644 index 0000000..fdfd9b4 --- /dev/null +++ b/Library/DataLayer/API/IBook.cs @@ -0,0 +1,10 @@ +namespace DataLayer.API +{ + public interface IBook : IProduct + { + string Author { get; set; } + string Publisher { get; set; } + int Pages { get; set; } + DateTime PublicationDate { get; set; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/API/IDataContext.cs b/Library/DataLayer/API/IDataContext.cs new file mode 100644 index 0000000..efbc77a --- /dev/null +++ b/Library/DataLayer/API/IDataContext.cs @@ -0,0 +1,9 @@ +namespace DataLayer.API +{ + public interface IDataContext + { + public List Users { get; set; } + + public List Products { get; set; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs new file mode 100644 index 0000000..30d8b5c --- /dev/null +++ b/Library/DataLayer/API/IDataRepository.cs @@ -0,0 +1,22 @@ +using DataLayer.Implementations; + +namespace DataLayer.API +{ + public interface IDataRepository + { + static IDataRepository CreateDataRepository(IDataContext dataContext) + { + return new DataRepository(dataContext); + } + + void AddUser(IUser user); + IUser GetUser(string guid); + List GetAllUsers(); + + + + void AddProduct(IProduct product); + IProduct GetProduct(string guid); + List GetAllProducts(); + } +} \ No newline at end of file diff --git a/Library/DataLayer/API/IProduct.cs b/Library/DataLayer/API/IProduct.cs new file mode 100644 index 0000000..3e971ce --- /dev/null +++ b/Library/DataLayer/API/IProduct.cs @@ -0,0 +1,9 @@ +namespace DataLayer.API +{ + public interface IProduct + { + string Guid { get; } + string Name { get; set; } + double Price { get; set; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/API/IUser.cs b/Library/DataLayer/API/IUser.cs new file mode 100644 index 0000000..5df25fe --- /dev/null +++ b/Library/DataLayer/API/IUser.cs @@ -0,0 +1,16 @@ +namespace DataLayer.API +{ + public interface IUser + { + // guid, because is more commonly used in contexts of the Microsoft ecosystem + string Guid { get; } + string FirstName { get; set; } + string LastName { get; set; } + string Email { get; set; } + double Balance { get; set; } + // stick to E.164 standard? + int PhoneNumber { get; set; } + // string Role { get; set; } + Dictionary ProductsDic { get; set; } + } +} \ No newline at end of file From 092cd7b5f48f31341f4a3623b2d56582cae4e497 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sat, 30 Mar 2024 20:54:53 +0100 Subject: [PATCH 04/24] add business process API implementations --- Library/DataLayer/Implementations/Book.cs | 25 +++++++++ .../DataLayer/Implementations/DataContex.cs | 17 ++++++ .../Implementations/DataRepository.cs | 52 +++++++++++++++++++ Library/DataLayer/Implementations/User.cs | 32 ++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 Library/DataLayer/Implementations/Book.cs create mode 100644 Library/DataLayer/Implementations/DataContex.cs create mode 100644 Library/DataLayer/Implementations/DataRepository.cs create mode 100644 Library/DataLayer/Implementations/User.cs diff --git a/Library/DataLayer/Implementations/Book.cs b/Library/DataLayer/Implementations/Book.cs new file mode 100644 index 0000000..3b30b73 --- /dev/null +++ b/Library/DataLayer/Implementations/Book.cs @@ -0,0 +1,25 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class Book : IBook + { + public Book(string name, double price, string author, string publisher, int pages, DateTime publicationDate) + { + Guid = System.Guid.NewGuid().ToString(); + Name = name; + Price = price; + Author = author; + Publisher = publisher; + Pages = pages; + PublicationDate = publicationDate; + } + public string Guid { get; } + public string Name { get; set; } + public double Price { get; set; } + public string Author { get; set; } + public string Publisher { get; set; } + public int Pages { get; set; } + public DateTime PublicationDate { get; set; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataContex.cs b/Library/DataLayer/Implementations/DataContex.cs new file mode 100644 index 0000000..3c38332 --- /dev/null +++ b/Library/DataLayer/Implementations/DataContex.cs @@ -0,0 +1,17 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class DataContext : IDataContext + { + public List Users { get; set; } + public List Products { get; set; } + + public DataContext() + { + Users = new List(); + Products = new List(); + } + + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs new file mode 100644 index 0000000..9289c27 --- /dev/null +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -0,0 +1,52 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class DataRepository : IDataRepository + { + private IDataContext _dataContext; + + // Init the data context + public DataRepository(IDataContext dataContext) + { + _dataContext = dataContext; + } + + // -----> User methods + public void AddUser(IUser user) + { + _dataContext.Users.Add(user); + } + + public List GetAllUsers() + { + return _dataContext.Users; + } + + public IUser GetUser(string guid) + { + IUser? user = _dataContext.Users.FirstOrDefault(u => u.Guid == guid) ?? throw new Exception("User does not exist"); + + return user; + } + + // -----> Product methods + public void AddProduct(IProduct product) + { + _dataContext.Products.Add(product); + } + + public List GetAllProducts() + { + return _dataContext.Products; + } + + public IProduct GetProduct(string guid) + { + IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); + + return product; + + } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/User.cs b/Library/DataLayer/Implementations/User.cs new file mode 100644 index 0000000..916033f --- /dev/null +++ b/Library/DataLayer/Implementations/User.cs @@ -0,0 +1,32 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class User : IUser + { + public User(string firstName, string lastName, string email, double balance, int phoneNumber, Dictionary? productsDic) + { + Guid = System.Guid.NewGuid().ToString(); + FirstName = firstName; + LastName = lastName; + Email = email; + Balance = balance; + PhoneNumber = phoneNumber; + ProductsDic = productsDic ?? new Dictionary(); + } + + public string Guid { get; } + + public string FirstName { get; set; } + + public string LastName { get; set; } + + public string Email { get; set; } + + public double Balance { get; set; } + + public int PhoneNumber { get; set; } + + public Dictionary ProductsDic { get; set; } + } +} \ No newline at end of file From a659599bd5fcfb79d705bb2683501cb4fee7ebca Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sat, 30 Mar 2024 20:55:25 +0100 Subject: [PATCH 05/24] add tests to existing data layer implementations --- Library/Tests/DataLayerTests.cs | 82 +++++++++++++++++++++++++++++++++ Library/Tests/Tests.csproj | 27 +++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Library/Tests/DataLayerTests.cs create mode 100644 Library/Tests/Tests.csproj diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs new file mode 100644 index 0000000..f32f154 --- /dev/null +++ b/Library/Tests/DataLayerTests.cs @@ -0,0 +1,82 @@ +using DataLayer.Implementations; +using DataLayer.API; + +namespace Tests; + +[TestClass] +public class DataLayerTests +{ + [TestMethod] + public void UserTests() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + Assert.AreEqual(firstName, user.FirstName); + Assert.AreEqual(lastName, user.LastName); + Assert.AreEqual(email, user.Email); + Assert.AreEqual(balance, user.Balance); + Assert.AreEqual(phoneNumber, user.PhoneNumber); + Assert.IsNotNull(user.Guid); + + Book book1 = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); + Book book2 = new Book("Book2", 20.0, "Author2", "Publisher2", 200, new DateTime(2022, 2, 2)); + + Dictionary BooksCollection = new Dictionary + { + { book1.Guid, book1 }, + { book2.Guid, book2 } + }; + + user.ProductsDic = BooksCollection; + + Assert.AreEqual(book1, user.ProductsDic[book1.Guid]); + Assert.AreEqual(book2, user.ProductsDic[book2.Guid]); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + + dataRepository.AddUser(user); + + Assert.IsTrue(dataRepository.GetAllUsers().Contains(user)); + + Assert.ThrowsException(() => dataRepository.GetUser("For sure not even a valid guid")); + + + } + + [TestMethod] + public void BookTests() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + + IBook book = new Book(name, price, author, publisher, pages, publicationDate); + + Assert.AreEqual(name, book.Name); + Assert.AreEqual(author, book.Author); + Assert.AreEqual(publisher, book.Publisher); + Assert.AreEqual(price, book.Price); + Assert.AreEqual(pages, book.Pages); + Assert.AreEqual(publicationDate, book.PublicationDate); + + + Book book1 = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + + dataRepository.AddProduct(book); + + Assert.IsTrue(dataRepository.GetAllProducts().Contains(book)); + + Assert.ThrowsException(() => dataRepository.GetProduct("For sure not even a valid guid")); + } +} diff --git a/Library/Tests/Tests.csproj b/Library/Tests/Tests.csproj new file mode 100644 index 0000000..033d57e --- /dev/null +++ b/Library/Tests/Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + \ No newline at end of file From 393ef46975a14ecd4decca831c495ebc5be8c23a Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sat, 30 Mar 2024 22:53:05 +0100 Subject: [PATCH 06/24] add remaining business process APIs their implementations and tests --- Library/DataLayer/API/IEvent.cs | 9 ++++++ Library/DataLayer/API/IStatus.cs | 10 ++++++ Library/DataLayer/Implementations/Event.cs | 17 ++++++++++ Library/DataLayer/Implementations/Status.cs | 19 +++++++++++ Library/Tests/DataLayerTests.cs | 35 +++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 Library/DataLayer/API/IEvent.cs create mode 100644 Library/DataLayer/API/IStatus.cs create mode 100644 Library/DataLayer/Implementations/Event.cs create mode 100644 Library/DataLayer/Implementations/Status.cs diff --git a/Library/DataLayer/API/IEvent.cs b/Library/DataLayer/API/IEvent.cs new file mode 100644 index 0000000..fd612e8 --- /dev/null +++ b/Library/DataLayer/API/IEvent.cs @@ -0,0 +1,9 @@ +namespace DataLayer.API +{ + public interface IEvent + { + IUser User { get; set; } + IStatus Status { get; set; } + DateTime Date { get; set; } + } +} diff --git a/Library/DataLayer/API/IStatus.cs b/Library/DataLayer/API/IStatus.cs new file mode 100644 index 0000000..2235fe4 --- /dev/null +++ b/Library/DataLayer/API/IStatus.cs @@ -0,0 +1,10 @@ +namespace DataLayer.API +{ + public interface IStatus + { + IProduct Product { get; set; } + int Quantity { get; set; } + DateTime Date { get; set; } + double Price { get; set; } + } +} diff --git a/Library/DataLayer/Implementations/Event.cs b/Library/DataLayer/Implementations/Event.cs new file mode 100644 index 0000000..74fa476 --- /dev/null +++ b/Library/DataLayer/Implementations/Event.cs @@ -0,0 +1,17 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class Event : IEvent + { + public Event(IUser user, IStatus status, DateTime date) + { + User = user; + Status = status; + Date = date; + } + public IUser User { get; set; } + public IStatus Status { get; set; } + public DateTime Date { get; set; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Status.cs b/Library/DataLayer/Implementations/Status.cs new file mode 100644 index 0000000..46f8c38 --- /dev/null +++ b/Library/DataLayer/Implementations/Status.cs @@ -0,0 +1,19 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class Status : IStatus + { + public Status(IProduct product, int quantity, DateTime date, double price) + { + Product = product; + Quantity = quantity; + Date = date; + Price = price; + } + public IProduct Product { get; set; } + public int Quantity { get; set; } + public DateTime Date { get; set; } + public double Price { get; set; } + } +} \ No newline at end of file diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index f32f154..4d0f0bb 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -79,4 +79,39 @@ public void BookTests() Assert.ThrowsException(() => dataRepository.GetProduct("For sure not even a valid guid")); } + + [TestMethod] + public void StatusTests() + { + IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); + int quantity = 10; + DateTime date = new DateTime(2022, 1, 1); + double price = 100.0; + + IStatus status = new Status(product, quantity, date, price); + + Assert.AreEqual(product, status.Product); + Assert.AreEqual(quantity, status.Quantity); + Assert.AreEqual(date, status.Date); + Assert.AreEqual(price, status.Price); + } + + [TestMethod] + public void EventTests() + { + IUser user = new User("John", "Doe", "Doe", 100.0, 1234567890, null); + DateTime EventDate = new DateTime(2022, 1, 1); + + IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); + int quantity = 10; + DateTime StatusDate = new DateTime(2020, 1, 1); + double price = 100.0; + IStatus status = new Status(product, quantity, StatusDate, price); + + IEvent myEvent = new Event(user, status, EventDate); + + Assert.AreEqual(user, myEvent.User); + Assert.AreEqual(status, myEvent.Status); + Assert.AreEqual(EventDate, myEvent.Date); + } } From 4b9327c4f76364452aa3e6559d89f0e6815c355e Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Wed, 3 Apr 2024 10:08:20 +0200 Subject: [PATCH 07/24] Fixed IStatus and Status naming --- Library/DataLayer/API/IEvent.cs | 2 +- Library/DataLayer/API/IStatus.cs | 2 +- Library/DataLayer/Implementations/Event.cs | 6 +++--- Library/DataLayer/Implementations/Status.cs | 4 ++-- Library/Tests/DataLayerTests.cs | 20 ++++++++++---------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Library/DataLayer/API/IEvent.cs b/Library/DataLayer/API/IEvent.cs index fd612e8..78c1bf0 100644 --- a/Library/DataLayer/API/IEvent.cs +++ b/Library/DataLayer/API/IEvent.cs @@ -3,7 +3,7 @@ namespace DataLayer.API public interface IEvent { IUser User { get; set; } - IStatus Status { get; set; } + IState State { get; set; } DateTime Date { get; set; } } } diff --git a/Library/DataLayer/API/IStatus.cs b/Library/DataLayer/API/IStatus.cs index 2235fe4..57d93ba 100644 --- a/Library/DataLayer/API/IStatus.cs +++ b/Library/DataLayer/API/IStatus.cs @@ -1,6 +1,6 @@ namespace DataLayer.API { - public interface IStatus + public interface IState { IProduct Product { get; set; } int Quantity { get; set; } diff --git a/Library/DataLayer/Implementations/Event.cs b/Library/DataLayer/Implementations/Event.cs index 74fa476..020c5f1 100644 --- a/Library/DataLayer/Implementations/Event.cs +++ b/Library/DataLayer/Implementations/Event.cs @@ -4,14 +4,14 @@ namespace DataLayer.Implementations { public class Event : IEvent { - public Event(IUser user, IStatus status, DateTime date) + public Event(IUser user, IState state, DateTime date) { User = user; - Status = status; + State = state; Date = date; } public IUser User { get; set; } - public IStatus Status { get; set; } + public IState State { get; set; } public DateTime Date { get; set; } } } \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Status.cs b/Library/DataLayer/Implementations/Status.cs index 46f8c38..e2ff455 100644 --- a/Library/DataLayer/Implementations/Status.cs +++ b/Library/DataLayer/Implementations/Status.cs @@ -2,9 +2,9 @@ namespace DataLayer.Implementations { - public class Status : IStatus + public class State : IState { - public Status(IProduct product, int quantity, DateTime date, double price) + public State(IProduct product, int quantity, DateTime date, double price) { Product = product; Quantity = quantity; diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index 4d0f0bb..f744298 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -81,19 +81,19 @@ public void BookTests() } [TestMethod] - public void StatusTests() + public void StateTests() { IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); int quantity = 10; DateTime date = new DateTime(2022, 1, 1); double price = 100.0; - IStatus status = new Status(product, quantity, date, price); + IState state = new State(product, quantity, date, price); - Assert.AreEqual(product, status.Product); - Assert.AreEqual(quantity, status.Quantity); - Assert.AreEqual(date, status.Date); - Assert.AreEqual(price, status.Price); + Assert.AreEqual(product, state.Product); + Assert.AreEqual(quantity, state.Quantity); + Assert.AreEqual(date, state.Date); + Assert.AreEqual(price, state.Price); } [TestMethod] @@ -104,14 +104,14 @@ public void EventTests() IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); int quantity = 10; - DateTime StatusDate = new DateTime(2020, 1, 1); + DateTime StateDate = new DateTime(2020, 1, 1); double price = 100.0; - IStatus status = new Status(product, quantity, StatusDate, price); + IState state = new State(product, quantity, StateDate, price); - IEvent myEvent = new Event(user, status, EventDate); + IEvent myEvent = new Event(user, state, EventDate); Assert.AreEqual(user, myEvent.User); - Assert.AreEqual(status, myEvent.Status); + Assert.AreEqual(state, myEvent.State); Assert.AreEqual(EventDate, myEvent.Date); } } From d2d1c3873b09c82584b4b50cf7f9e040511b8327 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Wed, 3 Apr 2024 10:08:20 +0200 Subject: [PATCH 08/24] names --- Library/DataLayer/API/IEvent.cs | 2 +- .../DataLayer/API/{IStatus.cs => IState.cs} | 2 +- Library/DataLayer/Implementations/Event.cs | 6 +++--- .../Implementations/{Status.cs => State.cs} | 4 ++-- Library/Tests/DataLayerTests.cs | 20 +++++++++---------- 5 files changed, 17 insertions(+), 17 deletions(-) rename Library/DataLayer/API/{IStatus.cs => IState.cs} (86%) rename Library/DataLayer/Implementations/{Status.cs => State.cs} (76%) diff --git a/Library/DataLayer/API/IEvent.cs b/Library/DataLayer/API/IEvent.cs index fd612e8..78c1bf0 100644 --- a/Library/DataLayer/API/IEvent.cs +++ b/Library/DataLayer/API/IEvent.cs @@ -3,7 +3,7 @@ namespace DataLayer.API public interface IEvent { IUser User { get; set; } - IStatus Status { get; set; } + IState State { get; set; } DateTime Date { get; set; } } } diff --git a/Library/DataLayer/API/IStatus.cs b/Library/DataLayer/API/IState.cs similarity index 86% rename from Library/DataLayer/API/IStatus.cs rename to Library/DataLayer/API/IState.cs index 2235fe4..57d93ba 100644 --- a/Library/DataLayer/API/IStatus.cs +++ b/Library/DataLayer/API/IState.cs @@ -1,6 +1,6 @@ namespace DataLayer.API { - public interface IStatus + public interface IState { IProduct Product { get; set; } int Quantity { get; set; } diff --git a/Library/DataLayer/Implementations/Event.cs b/Library/DataLayer/Implementations/Event.cs index 74fa476..020c5f1 100644 --- a/Library/DataLayer/Implementations/Event.cs +++ b/Library/DataLayer/Implementations/Event.cs @@ -4,14 +4,14 @@ namespace DataLayer.Implementations { public class Event : IEvent { - public Event(IUser user, IStatus status, DateTime date) + public Event(IUser user, IState state, DateTime date) { User = user; - Status = status; + State = state; Date = date; } public IUser User { get; set; } - public IStatus Status { get; set; } + public IState State { get; set; } public DateTime Date { get; set; } } } \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Status.cs b/Library/DataLayer/Implementations/State.cs similarity index 76% rename from Library/DataLayer/Implementations/Status.cs rename to Library/DataLayer/Implementations/State.cs index 46f8c38..e2ff455 100644 --- a/Library/DataLayer/Implementations/Status.cs +++ b/Library/DataLayer/Implementations/State.cs @@ -2,9 +2,9 @@ namespace DataLayer.Implementations { - public class Status : IStatus + public class State : IState { - public Status(IProduct product, int quantity, DateTime date, double price) + public State(IProduct product, int quantity, DateTime date, double price) { Product = product; Quantity = quantity; diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index 4d0f0bb..f744298 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -81,19 +81,19 @@ public void BookTests() } [TestMethod] - public void StatusTests() + public void StateTests() { IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); int quantity = 10; DateTime date = new DateTime(2022, 1, 1); double price = 100.0; - IStatus status = new Status(product, quantity, date, price); + IState state = new State(product, quantity, date, price); - Assert.AreEqual(product, status.Product); - Assert.AreEqual(quantity, status.Quantity); - Assert.AreEqual(date, status.Date); - Assert.AreEqual(price, status.Price); + Assert.AreEqual(product, state.Product); + Assert.AreEqual(quantity, state.Quantity); + Assert.AreEqual(date, state.Date); + Assert.AreEqual(price, state.Price); } [TestMethod] @@ -104,14 +104,14 @@ public void EventTests() IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); int quantity = 10; - DateTime StatusDate = new DateTime(2020, 1, 1); + DateTime StateDate = new DateTime(2020, 1, 1); double price = 100.0; - IStatus status = new Status(product, quantity, StatusDate, price); + IState state = new State(product, quantity, StateDate, price); - IEvent myEvent = new Event(user, status, EventDate); + IEvent myEvent = new Event(user, state, EventDate); Assert.AreEqual(user, myEvent.User); - Assert.AreEqual(status, myEvent.Status); + Assert.AreEqual(state, myEvent.State); Assert.AreEqual(EventDate, myEvent.Date); } } From 0df8f84a480b57143a3e10e3a232906af05ebbcf Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sat, 6 Apr 2024 12:13:54 +0200 Subject: [PATCH 09/24] Added GUIDs to remaining classes, created Borrow and Return, Started DataService --- Library/DataLayer/API/IEvent.cs | 1 + Library/DataLayer/API/IState.cs | 1 + Library/DataLayer/Implementations/Borrow.cs | 22 ++++++++ Library/DataLayer/Implementations/Return.cs | 22 ++++++++ Library/DataLayer/Implementations/State.cs | 4 +- Library/LogicLayer/API/IDataService.cs | 34 ++++++++++++ .../LogicLayer/Implementations/DataService.cs | 54 +++++++++++++++++++ 7 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 Library/DataLayer/Implementations/Borrow.cs create mode 100644 Library/DataLayer/Implementations/Return.cs create mode 100644 Library/LogicLayer/API/IDataService.cs create mode 100644 Library/LogicLayer/Implementations/DataService.cs diff --git a/Library/DataLayer/API/IEvent.cs b/Library/DataLayer/API/IEvent.cs index 78c1bf0..81b2a35 100644 --- a/Library/DataLayer/API/IEvent.cs +++ b/Library/DataLayer/API/IEvent.cs @@ -5,5 +5,6 @@ public interface IEvent IUser User { get; set; } IState State { get; set; } DateTime Date { get; set; } + string Guid { get; } } } diff --git a/Library/DataLayer/API/IState.cs b/Library/DataLayer/API/IState.cs index 57d93ba..8b88652 100644 --- a/Library/DataLayer/API/IState.cs +++ b/Library/DataLayer/API/IState.cs @@ -6,5 +6,6 @@ public interface IState int Quantity { get; set; } DateTime Date { get; set; } double Price { get; set; } + string Guid { get; } } } diff --git a/Library/DataLayer/Implementations/Borrow.cs b/Library/DataLayer/Implementations/Borrow.cs new file mode 100644 index 0000000..8dac4f8 --- /dev/null +++ b/Library/DataLayer/Implementations/Borrow.cs @@ -0,0 +1,22 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class Borrow : IEvent + { + public Borrow(IUser user, IState state, DateTime date, string guid) + { + User = user; + State = state; + Date = date; + Guid = guid; + + user.ProductsDic.Add(state.Product.Guid, state.Product); + state.Quantity--; + } + public IUser User { get; set; } + public IState State { get; set; } + public DateTime Date { get; set; } + public string Guid { get; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Return.cs b/Library/DataLayer/Implementations/Return.cs new file mode 100644 index 0000000..b6b1e0c --- /dev/null +++ b/Library/DataLayer/Implementations/Return.cs @@ -0,0 +1,22 @@ +using DataLayer.API; + +namespace DataLayer.Implementations +{ + public class Return : IEvent + { + public Return(IUser user, IState state, DateTime date, string guid) + { + User = user; + State = state; + Date = date; + Guid = guid; + + user.ProductsDic.Remove(state.Product.Guid); + state.Quantity++; + } + public IUser User { get; set; } + public IState State { get; set; } + public DateTime Date { get; set; } + public string Guid { get; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/State.cs b/Library/DataLayer/Implementations/State.cs index e2ff455..af31769 100644 --- a/Library/DataLayer/Implementations/State.cs +++ b/Library/DataLayer/Implementations/State.cs @@ -4,16 +4,18 @@ namespace DataLayer.Implementations { public class State : IState { - public State(IProduct product, int quantity, DateTime date, double price) + public State(IProduct product, int quantity, DateTime date, double price, string guid) { Product = product; Quantity = quantity; Date = date; Price = price; + Guid = guid; } public IProduct Product { get; set; } public int Quantity { get; set; } public DateTime Date { get; set; } public double Price { get; set; } + public string Guid { get; } } } \ No newline at end of file diff --git a/Library/LogicLayer/API/IDataService.cs b/Library/LogicLayer/API/IDataService.cs new file mode 100644 index 0000000..aa375b0 --- /dev/null +++ b/Library/LogicLayer/API/IDataService.cs @@ -0,0 +1,34 @@ +namespace LogicLayer.API +{ + public interface IDataService + { + #region Find + IProduct FindProduct(string guid); + IState FindState(string guid); + IUser FindUser(string guid); + IEvent FindEvent(string guid); + #endregion + + #region Add + void AddProduct(IProduct product); + void AddState(IState state); + void AddUser(IUser user); + void AddEvent(IEvent @event); + #endregion + + #region Remove + void RemoveProduct(string guid); + void RemoveState(string guid); + void RemoveUser(string guid); + void RemoveEvent(string guid); + #endregion + + #region Create + IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate); + IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid); + IEvent CreateReturn(IUser user, IState state, DateTime date, string guid); + IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber); + IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid); + #endregion + } +} \ No newline at end of file diff --git a/Library/LogicLayer/Implementations/DataService.cs b/Library/LogicLayer/Implementations/DataService.cs new file mode 100644 index 0000000..f3d9d83 --- /dev/null +++ b/Library/LogicLayer/Implementations/DataService.cs @@ -0,0 +1,54 @@ +using LogicLayer.API; + +namespace LogicLayer.Implementations +{ + public class DataService : IDataService + { + private IDataRepository _dataRepository; + public DataService(IDataRepository dataRepository) + { + _dataRepository = dataRepository; + } + + #region User + public void AddUser(IUser user) + { + DataRepository.AddUser(user); + } + public IUser GetUser(string guid) + { + return DataRepository.GetUser(guid); + } + public List GetAllUsers() + { + return DataRepository.GetAllUsers(); + } + #endregion + + #region Product + public void AddProduct(IProduct product) + { + DataRepository.AddProduct(product); + } + public IProduct GetProduct(string guid) + { + return DataRepository.GetProduct(guid); + } + public List GetAllProducts() + { + return DataRepository.GetAllProducts(); + } + #endregion + + #region State + public void AddState(IState state) + { + DataRepository.AddState(state); + } + public IState GetState(string guid) + { + return DataRepository.GetState(guid); + } + #endregion + } +} \ No newline at end of file From f9d0bf2f480840ff13037051e39105909fe39871 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Fri, 12 Apr 2024 15:55:51 +0200 Subject: [PATCH 10/24] Add Event and State implementations in DataRepository, changes in DataService --- Library/DataLayer/API/IDataContext.cs | 4 +++ Library/DataLayer/API/IDataRepository.cs | 18 ++++++++-- .../DataLayer/Implementations/DataContex.cs | 4 +++ .../Implementations/DataRepository.cs | 36 +++++++++++++++++++ Library/LogicLayer/API/IDataService.cs | 19 ++++++---- .../LogicLayer/Implementations/DataService.cs | 21 +++++------ 6 files changed, 82 insertions(+), 20 deletions(-) diff --git a/Library/DataLayer/API/IDataContext.cs b/Library/DataLayer/API/IDataContext.cs index efbc77a..29f8bcd 100644 --- a/Library/DataLayer/API/IDataContext.cs +++ b/Library/DataLayer/API/IDataContext.cs @@ -5,5 +5,9 @@ public interface IDataContext public List Users { get; set; } public List Products { get; set; } + + public List Events { get; set; } + + public List States { get; set; } } } \ No newline at end of file diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs index 30d8b5c..2b0a839 100644 --- a/Library/DataLayer/API/IDataRepository.cs +++ b/Library/DataLayer/API/IDataRepository.cs @@ -9,14 +9,28 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) return new DataRepository(dataContext); } + #region User void AddUser(IUser user); IUser GetUser(string guid); List GetAllUsers(); + #endregion - - + #region Product void AddProduct(IProduct product); IProduct GetProduct(string guid); List GetAllProducts(); + #endregion + + #region Event + void AddEvent(IEvent @event); + IEvent GetEvent(string guid); + List GetAllEvents(); + #endregion + + #region State + void AddState(IState state); + IState GetState(string guid); + List GetAllStates(); + #endregion } } \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataContex.cs b/Library/DataLayer/Implementations/DataContex.cs index 3c38332..2885775 100644 --- a/Library/DataLayer/Implementations/DataContex.cs +++ b/Library/DataLayer/Implementations/DataContex.cs @@ -6,11 +6,15 @@ public class DataContext : IDataContext { public List Users { get; set; } public List Products { get; set; } + public List Events { get; set; } + public List States { get; set; } public DataContext() { Users = new List(); Products = new List(); + Events = new List(); + States = new List(); } } diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index 9289c27..a2805d3 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -48,5 +48,41 @@ public IProduct GetProduct(string guid) return product; } + + // -----> Event methods + public void AddEvent(IEvent @event) + { + _dataContext.Events.Add(@event); + } + + public List GetAllEvents() + { + return _dataContext.Events; + } + + public IEvent GetEvent(string guid) + { + IEvent @event = _dataContext.Events.FirstOrDefault(@event => @event.Guid == guid) ?? throw new Exception("Event does not exist"); + + return @event; + } + + // -----> State methods + public void AddState(IState state) + { + _dataContext.States.Add(state); + } + + public List GetAllStates() + { + return _dataContext.States; + } + + public IState GetState(string guid) + { + IState state = _dataContext.States.FirstOrDefault(state => state.Guid == guid) ?? throw new Exception("State does not exist"); + + return state; + } } } \ No newline at end of file diff --git a/Library/LogicLayer/API/IDataService.cs b/Library/LogicLayer/API/IDataService.cs index aa375b0..609fb16 100644 --- a/Library/LogicLayer/API/IDataService.cs +++ b/Library/LogicLayer/API/IDataService.cs @@ -9,12 +9,12 @@ public interface IDataService IEvent FindEvent(string guid); #endregion - #region Add - void AddProduct(IProduct product); - void AddState(IState state); - void AddUser(IUser user); - void AddEvent(IEvent @event); - #endregion + // #region Add + // void AddProduct(IProduct product); + // void AddState(IState state); + // void AddUser(IUser user); + // void AddEvent(IEvent @event); + // #endregion #region Remove void RemoveProduct(string guid); @@ -30,5 +30,12 @@ public interface IDataService IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber); IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid); #endregion + + #region GetBy + List GetEventsByUser(string userGuid); + List GetEventsByProduct(string productGuid); + IProduct GetProductByState(string stateGuid); + List GetEventsByState(string stateGuid); + #endregion } } \ No newline at end of file diff --git a/Library/LogicLayer/Implementations/DataService.cs b/Library/LogicLayer/Implementations/DataService.cs index f3d9d83..2133ab5 100644 --- a/Library/LogicLayer/Implementations/DataService.cs +++ b/Library/LogicLayer/Implementations/DataService.cs @@ -11,10 +11,6 @@ public DataService(IDataRepository dataRepository) } #region User - public void AddUser(IUser user) - { - DataRepository.AddUser(user); - } public IUser GetUser(string guid) { return DataRepository.GetUser(guid); @@ -26,10 +22,6 @@ public List GetAllUsers() #endregion #region Product - public void AddProduct(IProduct product) - { - DataRepository.AddProduct(product); - } public IProduct GetProduct(string guid) { return DataRepository.GetProduct(guid); @@ -41,14 +33,19 @@ public List GetAllProducts() #endregion #region State - public void AddState(IState state) - { - DataRepository.AddState(state); - } public IState GetState(string guid) { return DataRepository.GetState(guid); } + public List GetAllStates() + { + return DataRepository.GetAllStates(); + } #endregion + + public List getEventsByUser(string userGuid) + { + return DataRepository.getEventsByUser(userGuid); + } } } \ No newline at end of file From cc4edab27f7bb690b95abb3c64de3413af72fcbd Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 15:47:54 +0200 Subject: [PATCH 11/24] Add GetProductByState, GetEventsByUser, GetEventsByProduct, and GetEventsByState methods in IDataRepository and IDataService --- Library/DataLayer/API/IDataRepository.cs | 4 + .../Implementations/DataRepository.cs | 31 +++++++ Library/LogicLayer/API/IDataService.cs | 17 +++- .../LogicLayer/Implementations/DataService.cs | 93 ++++++++++++++++--- 4 files changed, 127 insertions(+), 18 deletions(-) diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs index 2b0a839..e9dc922 100644 --- a/Library/DataLayer/API/IDataRepository.cs +++ b/Library/DataLayer/API/IDataRepository.cs @@ -19,12 +19,16 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) void AddProduct(IProduct product); IProduct GetProduct(string guid); List GetAllProducts(); + IProduct GetProductByState(string stateGuid); #endregion #region Event void AddEvent(IEvent @event); IEvent GetEvent(string guid); List GetAllEvents(); + List GetEventsByUser(string userGuid); + List GetEventsByProduct(string productGuid); + List GetEventsByState(string stateGuid); #endregion #region State diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index a2805d3..1b3c6b3 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -46,7 +46,17 @@ public IProduct GetProduct(string guid) IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); return product; + } + + public IProduct GetProductByState(string stateGuid) + { + IProduct? product = _dataContext.States.FirstOrDefault(state => state.Guid == stateGuid)?.Product; + if (product == null) + { + throw new Exception("Product does not exist"); + } + return product; } // -----> Event methods @@ -67,6 +77,27 @@ public IEvent GetEvent(string guid) return @event; } + public List GetEventsByUser(string userGuid) + { + List events = _dataContext.Events.Where(@event => @event.User.Guid == userGuid).ToList(); + + return events; + } + + public List GetEventsByProduct(string productGuid) + { + List events = _dataContext.Events.Where(@event => @event.State.Product.Guid == productGuid).ToList(); + + return events; + } + + public List GetEventsByState(string stateGuid) + { + List events = _dataContext.Events.Where(@event => @event.State.Guid == stateGuid).ToList(); + + return events; + } + // -----> State methods public void AddState(IState state) { diff --git a/Library/LogicLayer/API/IDataService.cs b/Library/LogicLayer/API/IDataService.cs index 609fb16..821aa94 100644 --- a/Library/LogicLayer/API/IDataService.cs +++ b/Library/LogicLayer/API/IDataService.cs @@ -2,11 +2,11 @@ namespace LogicLayer.API { public interface IDataService { - #region Find - IProduct FindProduct(string guid); - IState FindState(string guid); - IUser FindUser(string guid); - IEvent FindEvent(string guid); + #region Get + IProduct GetProduct(string guid); + IState GetState(string guid); + IUser GetUser(string guid); + IEvent GetEvent(string guid); #endregion // #region Add @@ -37,5 +37,12 @@ public interface IDataService IProduct GetProductByState(string stateGuid); List GetEventsByState(string stateGuid); #endregion + + #region GetAll + List GetAllProducts(); + List GetAllStates(); + List GetAllUsers(); + List GetAllEvents(); + #endregion } } \ No newline at end of file diff --git a/Library/LogicLayer/Implementations/DataService.cs b/Library/LogicLayer/Implementations/DataService.cs index 2133ab5..a8de3ad 100644 --- a/Library/LogicLayer/Implementations/DataService.cs +++ b/Library/LogicLayer/Implementations/DataService.cs @@ -10,42 +10,109 @@ public DataService(IDataRepository dataRepository) _dataRepository = dataRepository; } - #region User + #region Get public IUser GetUser(string guid) { return DataRepository.GetUser(guid); } - public List GetAllUsers() + public IProduct GetProduct(string guid) { - return DataRepository.GetAllUsers(); + return DataRepository.GetProduct(guid); + } + public IState GetState(string guid) + { + return DataRepository.GetState(guid); + } + public IEvent GetEvent(string guid) + { + return DataRepository.GetEvent(guid); } #endregion - #region Product - public IProduct GetProduct(string guid) + #region GetAll + public List GetAllUsers() { - return DataRepository.GetProduct(guid); + return DataRepository.GetAllUsers(); } public List GetAllProducts() { return DataRepository.GetAllProducts(); } + public List GetAllStates() + { + return DataRepository.GetAllStates(); + } #endregion - #region State - public IState GetState(string guid) + #region GetBy + public List GetEventsByUser(string userGuid) { - return DataRepository.GetState(guid); + return DataRepository.GetEventsByUser(userGuid); } - public List GetAllStates() + + public List GetEventsByProduct(string productGuid) { - return DataRepository.GetAllStates(); + return DataRepository.GetEventsByProduct(productGuid); + } + + public IProduct GetProductByState(string stateGuid) + { + return DataRepository.GetProductByState(stateGuid); + } + + public List GetEventsByState(string stateGuid) + { + return DataRepository.GetEventsByState(stateGuid); } #endregion - public List getEventsByUser(string userGuid) + #region Create + public IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber) + { + return DataRepository.CreateUser(guid, firstName, lastName, email, phoneNumber); + } + + public IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate) + { + return DataRepository.CreateBook(name, guid, price, author, publisher, pages, publicationDate); + } + + public IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid) { - return DataRepository.getEventsByUser(userGuid); + return DataRepository.CreateState(product, quantity, date, price, guid); } + + public IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid) + { + return DataRepository.CreateBorrow(user, state, date, guid); + } + + public IEvent CreateReturn(IUser user, IState state, DateTime date, string guid) + { + return DataRepository.CreateReturn(user, state, date, guid); + } + #endregion + + #region Remove + public void RemoveUser(string guid) + { + DataRepository.RemoveUser(guid); + } + + public void RemoveProduct(string guid) + { + DataRepository.RemoveProduct(guid); + } + + public void RemoveState(string guid) + { + DataRepository.RemoveState(guid); + } + + public void RemoveEvent(string guid) + { + DataRepository.RemoveEvent(guid); + } + #endregion } } \ No newline at end of file From 936611b2e007eaecf56d9f8f7213deb407a3d5b2 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 16:18:56 +0200 Subject: [PATCH 12/24] Added LogicLayer to solution, fixed existing tests --- .vscode/launch.json | 26 ++++++++++++++++++ .vscode/tasks.json | 41 ++++++++++++++++++++++++++++ Library.sln | 11 ++++++++ Library/LogicLayer/LogicLayer.csproj | 10 +++++++ Library/LogicLayer/Program.cs | 2 ++ Library/Tests/DataLayerTests.cs | 7 +++-- Library/Tests/LogicLayerTests.cs | 0 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Library/LogicLayer/LogicLayer.csproj create mode 100644 Library/LogicLayer/Program.cs create mode 100644 Library/Tests/LogicLayerTests.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..74cd58d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Library/Tests/bin/Debug/net8.0/Tests.dll", + "args": [], + "cwd": "${workspaceFolder}/Library/Tests", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..5ee20c9 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Library.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Library.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Library.sln" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Library.sln b/Library.sln index 5c23fc1..e82fbbc 100644 --- a/Library.sln +++ b/Library.sln @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLayer", "Library\DataLa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Library\Tests\Tests.csproj", "{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Library", "Library", "{B858A003-F5DD-4F95-99D9-5A7560A0B8CF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogicLayer", "Library\LogicLayer\LogicLayer.csproj", "{34D82E95-A0C6-4C12-8BB0-6836280E2491}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +25,10 @@ Global {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU {E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.Build.0 = Release|Any CPU + {34D82E95-A0C6-4C12-8BB0-6836280E2491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34D82E95-A0C6-4C12-8BB0-6836280E2491}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34D82E95-A0C6-4C12-8BB0-6836280E2491}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34D82E95-A0C6-4C12-8BB0-6836280E2491}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -28,4 +36,7 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B8DC2F37-8FC7-433C-BA78-56CAE077B47B} EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {34D82E95-A0C6-4C12-8BB0-6836280E2491} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF} + EndGlobalSection EndGlobal diff --git a/Library/LogicLayer/LogicLayer.csproj b/Library/LogicLayer/LogicLayer.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/Library/LogicLayer/LogicLayer.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Library/LogicLayer/Program.cs b/Library/LogicLayer/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/Library/LogicLayer/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index f744298..f596be0 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -87,8 +87,9 @@ public void StateTests() int quantity = 10; DateTime date = new DateTime(2022, 1, 1); double price = 100.0; + string guid = "1234567890"; - IState state = new State(product, quantity, date, price); + IState state = new State(product, quantity, date, price, guid); Assert.AreEqual(product, state.Product); Assert.AreEqual(quantity, state.Quantity); @@ -106,7 +107,9 @@ public void EventTests() int quantity = 10; DateTime StateDate = new DateTime(2020, 1, 1); double price = 100.0; - IState state = new State(product, quantity, StateDate, price); + string guid = "1234567890"; + + IState state = new State(product, quantity, StateDate, price, guid); IEvent myEvent = new Event(user, state, EventDate); diff --git a/Library/Tests/LogicLayerTests.cs b/Library/Tests/LogicLayerTests.cs new file mode 100644 index 0000000..e69de29 From 18ff2bde6fe29b797041025153b565f073048315 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 18:06:17 +0200 Subject: [PATCH 13/24] Fixed namespace errors as well as naming and compilation errors --- Library/DataLayer/API/IDataRepository.cs | 4 + .../Implementations/DataRepository.cs | 36 ++++--- Library/DataLayer/Implementations/Event.cs | 4 +- Library/LogicLayer/API/IDataService.cs | 16 ++-- .../LogicLayer/Implementations/DataService.cs | 94 +++++++++---------- Library/LogicLayer/LogicLayer.csproj | 4 + Library/Tests/DataLayerTests.cs | 7 +- 7 files changed, 95 insertions(+), 70 deletions(-) diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs index e9dc922..d052d19 100644 --- a/Library/DataLayer/API/IDataRepository.cs +++ b/Library/DataLayer/API/IDataRepository.cs @@ -13,6 +13,7 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) void AddUser(IUser user); IUser GetUser(string guid); List GetAllUsers(); + void RemoveUser(string guid); #endregion #region Product @@ -20,6 +21,7 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) IProduct GetProduct(string guid); List GetAllProducts(); IProduct GetProductByState(string stateGuid); + void RemoveProduct(string guid); #endregion #region Event @@ -29,12 +31,14 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) List GetEventsByUser(string userGuid); List GetEventsByProduct(string productGuid); List GetEventsByState(string stateGuid); + void RemoveEvent(string guid); #endregion #region State void AddState(IState state); IState GetState(string guid); List GetAllStates(); + void RemoveState(string guid); #endregion } } \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index 1b3c6b3..cfa05b6 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -17,37 +17,38 @@ public void AddUser(IUser user) { _dataContext.Users.Add(user); } - public List GetAllUsers() { return _dataContext.Users; } - public IUser GetUser(string guid) { IUser? user = _dataContext.Users.FirstOrDefault(u => u.Guid == guid) ?? throw new Exception("User does not exist"); return user; } + public void RemoveUser(string guid) + { + IUser user = _dataContext.Users.FirstOrDefault(user => user.Guid == guid) ?? throw new Exception("User does not exist"); + + _dataContext.Users.Remove(user); + } // -----> Product methods public void AddProduct(IProduct product) { _dataContext.Products.Add(product); } - public List GetAllProducts() { return _dataContext.Products; } - public IProduct GetProduct(string guid) { IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); return product; } - public IProduct GetProductByState(string stateGuid) { IProduct? product = _dataContext.States.FirstOrDefault(state => state.Guid == stateGuid)?.Product; @@ -58,62 +59,73 @@ public IProduct GetProductByState(string stateGuid) return product; } + public void RemoveProduct(string guid) + { + IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); + + _dataContext.Products.Remove(product); + } // -----> Event methods public void AddEvent(IEvent @event) { _dataContext.Events.Add(@event); } - public List GetAllEvents() { return _dataContext.Events; } - public IEvent GetEvent(string guid) { IEvent @event = _dataContext.Events.FirstOrDefault(@event => @event.Guid == guid) ?? throw new Exception("Event does not exist"); return @event; } - public List GetEventsByUser(string userGuid) { List events = _dataContext.Events.Where(@event => @event.User.Guid == userGuid).ToList(); return events; } - public List GetEventsByProduct(string productGuid) { List events = _dataContext.Events.Where(@event => @event.State.Product.Guid == productGuid).ToList(); return events; } - public List GetEventsByState(string stateGuid) { List events = _dataContext.Events.Where(@event => @event.State.Guid == stateGuid).ToList(); return events; } + public void RemoveEvent(string guid) + { + IEvent @event = _dataContext.Events.FirstOrDefault(@event => @event.Guid == guid) ?? throw new Exception("Event does not exist"); + + _dataContext.Events.Remove(@event); + } // -----> State methods public void AddState(IState state) { _dataContext.States.Add(state); } - public List GetAllStates() { return _dataContext.States; } - public IState GetState(string guid) { IState state = _dataContext.States.FirstOrDefault(state => state.Guid == guid) ?? throw new Exception("State does not exist"); return state; } + public void RemoveState(string guid) + { + IState state = _dataContext.States.FirstOrDefault(state => state.Guid == guid) ?? throw new Exception("State does not exist"); + + _dataContext.States.Remove(state); + } } } \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Event.cs b/Library/DataLayer/Implementations/Event.cs index 020c5f1..8231f46 100644 --- a/Library/DataLayer/Implementations/Event.cs +++ b/Library/DataLayer/Implementations/Event.cs @@ -4,14 +4,16 @@ namespace DataLayer.Implementations { public class Event : IEvent { - public Event(IUser user, IState state, DateTime date) + public Event(IUser user, IState state, DateTime date, string guid) { User = user; State = state; Date = date; + Guid = guid; } public IUser User { get; set; } public IState State { get; set; } public DateTime Date { get; set; } + public string Guid { get; } } } \ No newline at end of file diff --git a/Library/LogicLayer/API/IDataService.cs b/Library/LogicLayer/API/IDataService.cs index 821aa94..50a208b 100644 --- a/Library/LogicLayer/API/IDataService.cs +++ b/Library/LogicLayer/API/IDataService.cs @@ -1,3 +1,5 @@ +using DataLayer.API; + namespace LogicLayer.API { public interface IDataService @@ -23,13 +25,13 @@ public interface IDataService void RemoveEvent(string guid); #endregion - #region Create - IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate); - IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid); - IEvent CreateReturn(IUser user, IState state, DateTime date, string guid); - IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber); - IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid); - #endregion + // #region Create + // IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate); + // IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid); + // IEvent CreateReturn(IUser user, IState state, DateTime date, string guid); + // IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber); + // IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid); + // #endregion #region GetBy List GetEventsByUser(string userGuid); diff --git a/Library/LogicLayer/Implementations/DataService.cs b/Library/LogicLayer/Implementations/DataService.cs index a8de3ad..7aecb73 100644 --- a/Library/LogicLayer/Implementations/DataService.cs +++ b/Library/LogicLayer/Implementations/DataService.cs @@ -1,3 +1,4 @@ +using DataLayer.API; using LogicLayer.API; namespace LogicLayer.Implementations @@ -13,105 +14,104 @@ public DataService(IDataRepository dataRepository) #region Get public IUser GetUser(string guid) { - return DataRepository.GetUser(guid); + return _dataRepository.GetUser(guid); } public IProduct GetProduct(string guid) { - return DataRepository.GetProduct(guid); + return _dataRepository.GetProduct(guid); } public IState GetState(string guid) { - return DataRepository.GetState(guid); + return _dataRepository.GetState(guid); } public IEvent GetEvent(string guid) { - return DataRepository.GetEvent(guid); + return _dataRepository.GetEvent(guid); } #endregion #region GetAll public List GetAllUsers() { - return DataRepository.GetAllUsers(); + return _dataRepository.GetAllUsers(); } public List GetAllProducts() { - return DataRepository.GetAllProducts(); + return _dataRepository.GetAllProducts(); } public List GetAllStates() { - return DataRepository.GetAllStates(); + return _dataRepository.GetAllStates(); + } + public List GetAllEvents() + { + return _dataRepository.GetAllEvents(); } #endregion #region GetBy public List GetEventsByUser(string userGuid) { - return DataRepository.GetEventsByUser(userGuid); + return _dataRepository.GetEventsByUser(userGuid); } - public List GetEventsByProduct(string productGuid) { - return DataRepository.GetEventsByProduct(productGuid); + return _dataRepository.GetEventsByProduct(productGuid); } - public IProduct GetProductByState(string stateGuid) { - return DataRepository.GetProductByState(stateGuid); + return _dataRepository.GetProductByState(stateGuid); } - public List GetEventsByState(string stateGuid) { - return DataRepository.GetEventsByState(stateGuid); + return _dataRepository.GetEventsByState(stateGuid); } #endregion - #region Create - public IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber) - { - return DataRepository.CreateUser(guid, firstName, lastName, email, phoneNumber); - } - - public IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate) - { - return DataRepository.CreateBook(name, guid, price, author, publisher, pages, publicationDate); - } - - public IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid) - { - return DataRepository.CreateState(product, quantity, date, price, guid); - } - - public IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid) - { - return DataRepository.CreateBorrow(user, state, date, guid); - } - - public IEvent CreateReturn(IUser user, IState state, DateTime date, string guid) - { - return DataRepository.CreateReturn(user, state, date, guid); - } - #endregion + // #region Create + // public IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber) + // { + // IUser user = new User(guid, firstName, lastName, email, phoneNumber); + // return _dataRepository.AddUser(new User(guid, firstName, lastName, email, phoneNumber)); + // } + + // public IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate) + // { + // return _dataRepository.CreateBook(name, guid, price, author, publisher, pages, publicationDate); + // } + + // public IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid) + // { + // return _dataRepository.CreateState(product, quantity, date, price, guid); + // } + + // public IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid) + // { + // return _dataRepository.CreateBorrow(user, state, date, guid); + // } + + // public IEvent CreateReturn(IUser user, IState state, DateTime date, string guid) + // { + // return _dataRepository.CreateReturn(user, state, date, guid); + // } + // #endregion #region Remove public void RemoveUser(string guid) { - DataRepository.RemoveUser(guid); + _dataRepository.RemoveUser(guid); } - public void RemoveProduct(string guid) { - DataRepository.RemoveProduct(guid); + _dataRepository.RemoveProduct(guid); } - public void RemoveState(string guid) { - DataRepository.RemoveState(guid); + _dataRepository.RemoveState(guid); } - public void RemoveEvent(string guid) { - DataRepository.RemoveEvent(guid); + _dataRepository.RemoveEvent(guid); } #endregion } diff --git a/Library/LogicLayer/LogicLayer.csproj b/Library/LogicLayer/LogicLayer.csproj index 2150e37..987cdc2 100644 --- a/Library/LogicLayer/LogicLayer.csproj +++ b/Library/LogicLayer/LogicLayer.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index f596be0..6853926 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -107,11 +107,12 @@ public void EventTests() int quantity = 10; DateTime StateDate = new DateTime(2020, 1, 1); double price = 100.0; - string guid = "1234567890"; + string guid1 = "1234567890"; + string guid2 = "0987654321"; - IState state = new State(product, quantity, StateDate, price, guid); + IState state = new State(product, quantity, StateDate, price, guid1); - IEvent myEvent = new Event(user, state, EventDate); + IEvent myEvent = new Event(user, state, EventDate, guid2); Assert.AreEqual(user, myEvent.User); Assert.AreEqual(state, myEvent.State); From a4d1e4ff4f91cf33813abfddf9131fd88412ccb5 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 18:23:34 +0200 Subject: [PATCH 14/24] Added some tests for DataService.cs --- Library/Tests/DataLayerTests.cs | 52 ++++++ Library/Tests/LogicLayerTests.cs | 304 +++++++++++++++++++++++++++++++ Library/Tests/Tests.csproj | 1 + 3 files changed, 357 insertions(+) diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index 6853926..e0ced58 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -118,4 +118,56 @@ public void EventTests() Assert.AreEqual(state, myEvent.State); Assert.AreEqual(EventDate, myEvent.Date); } + + [TestMethod] + public void DataContextTests() + { + DataContext dataContext = new DataContext(); + + Assert.IsNotNull(dataContext.Users); + Assert.IsNotNull(dataContext.Products); + Assert.IsNotNull(dataContext.States); + Assert.IsNotNull(dataContext.Events); + } + + [TestMethod] + public void DataRepositoryTests() + { + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + + IUser user = new User("John", "Doe", "Doe", 100.0, 1234567890, null); + IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); + IState state = new State(product, 10, new DateTime(2022, 1, 1), 100.0, "1234567890"); + IEvent myEvent = new Event(user, state, new DateTime(2022, 1, 1), "0987654321"); + + dataRepository.AddUser(user); + dataRepository.AddProduct(product); + dataRepository.AddState(state); + dataRepository.AddEvent(myEvent); + + Assert.IsTrue(dataRepository.GetAllUsers().Contains(user)); + Assert.IsTrue(dataRepository.GetAllProducts().Contains(product)); + Assert.IsTrue(dataRepository.GetAllStates().Contains(state)); + Assert.IsTrue(dataRepository.GetAllEvents().Contains(myEvent)); + + Assert.AreEqual(user, dataRepository.GetUser(user.Guid)); + Assert.AreEqual(product, dataRepository.GetProduct(product.Guid)); + Assert.AreEqual(state, dataRepository.GetState(state.Guid)); + Assert.AreEqual(myEvent, dataRepository.GetEvent(myEvent.Guid)); + + Assert.ThrowsException(() => dataRepository.GetUser("For sure not even a valid guid")); + Assert.ThrowsException(() => dataRepository.GetProduct("For sure not even a valid guid")); + Assert.ThrowsException(() => dataRepository.GetState("For sure not even a valid guid")); + Assert.ThrowsException(() => dataRepository.GetEvent("For sure not even a valid guid")); + + dataRepository.RemoveUser(user.Guid); + dataRepository.RemoveProduct(product.Guid); + dataRepository.RemoveState(state.Guid); + dataRepository.RemoveEvent(myEvent.Guid); + + Assert.IsFalse(dataRepository.GetAllUsers().Contains(user)); + Assert.IsFalse(dataRepository.GetAllProducts().Contains(product)); + Assert.IsFalse(dataRepository.GetAllStates().Contains(state)); + Assert.IsFalse(dataRepository.GetAllEvents().Contains(myEvent)); + } } diff --git a/Library/Tests/LogicLayerTests.cs b/Library/Tests/LogicLayerTests.cs index e69de29..d745a22 100644 --- a/Library/Tests/LogicLayerTests.cs +++ b/Library/Tests/LogicLayerTests.cs @@ -0,0 +1,304 @@ +using DataLayer.Implementations; +using DataLayer.API; +using LogicLayer.Implementations; +using LogicLayer.API; + +namespace Tests; + +[TestClass] +public class LogicLayerTests +{ + [TestMethod] + public void AddUserTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddUser(user); + + IDataService dataService = new DataService(dataRepository); + + Assert.AreEqual(user, dataService.GetUser(user.Guid)); + Assert.IsTrue(dataService.GetAllUsers().Contains(user)); + } + + [TestMethod] + public void AddProductTest() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddProduct(product); + + IDataService dataService = new DataService(dataRepository); + + Assert.AreEqual(product, dataService.GetProduct(product.Guid)); + Assert.IsTrue(dataService.GetAllProducts().Contains(product)); + } + + [TestMethod] + public void AddStateTest() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddState(state); + + IDataService dataService = new DataService(dataRepository); + + Assert.AreEqual(state, dataService.GetState(state.Guid)); + Assert.IsTrue(dataService.GetAllStates().Contains(state)); + } + + [TestMethod] + public void AddEventTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddEvent(@event); + + IDataService dataService = new DataService(dataRepository); + + Assert.AreEqual(@event, dataService.GetEvent(@event.Guid)); + Assert.IsTrue(dataService.GetAllEvents().Contains(@event)); + } + + [TestMethod] + public void GetEventsByUserTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddEvent(@event); + + IDataService dataService = new DataService(dataRepository); + + Assert.IsTrue(dataService.GetEventsByUser(user.Guid).Contains(@event)); + } + + [TestMethod] + public void GetEventsByProductTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddEvent(@event); + + IDataService dataService = new DataService(dataRepository); + + Assert.IsTrue(dataService.GetEventsByProduct(product.Guid).Contains(@event)); + } + + [TestMethod] + public void GetProductByStateTest() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddState(state); + + IDataService dataService = new DataService(dataRepository); + + Assert.AreEqual(product, dataService.GetProductByState(state.Guid)); + } + + [TestMethod] + public void GetEventsByStateTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddEvent(@event); + + IDataService dataService = new DataService(dataRepository); + + Assert.IsTrue(dataService.GetEventsByState(state.Guid).Contains(@event)); + } + + [TestMethod] + public void RemoveUserTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddUser(user); + + IDataService dataService = new DataService(dataRepository); + + dataService.RemoveUser(user.Guid); + + Assert.IsFalse(dataService.GetAllUsers().Contains(user)); + } + + [TestMethod] + public void RemoveProductTest() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddProduct(product); + + IDataService dataService = new DataService(dataRepository); + + dataService.RemoveProduct(product.Guid); + + Assert.IsFalse(dataService.GetAllProducts().Contains(product)); + } + + [TestMethod] + public void RemoveStateTest() + { + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddState(state); + + IDataService dataService = new DataService(dataRepository); + + dataService.RemoveState(state.Guid); + + Assert.IsFalse(dataService.GetAllStates().Contains(state)); + } + + [TestMethod] + public void RemoveEventTest() + { + const string firstName = "John"; + const string lastName = "Doe"; + const string email = "Doe"; + const double balance = 100.0; + const int phoneNumber = 1234567890; + IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + const string name = "Sample Book"; + const string author = "John Doe"; + const string publisher = "Publisher X"; + const double price = 29.99; + const int pages = 299; + DateTime publicationDate = new DateTime(2022, 1, 1); + IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + + IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + dataRepository.AddEvent(@event); + + IDataService dataService = new DataService(dataRepository); + + dataService.RemoveEvent(@event.Guid); + + Assert.IsFalse(dataService.GetAllEvents().Contains(@event)); + } +} \ No newline at end of file diff --git a/Library/Tests/Tests.csproj b/Library/Tests/Tests.csproj index 033d57e..f14fa5a 100644 --- a/Library/Tests/Tests.csproj +++ b/Library/Tests/Tests.csproj @@ -22,6 +22,7 @@ + \ No newline at end of file From d23b49ca3f7920e092107e0566ba53c0274097be Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 18:33:31 +0200 Subject: [PATCH 15/24] Gitignore update --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8e67ca8..90d069c 100644 --- a/.gitignore +++ b/.gitignore @@ -404,8 +404,8 @@ FodyWeavers.xsd .vscode/* !.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json +.vscode/tasks.json +.vscode/launch.json !.vscode/extensions.json !.vscode/*.code-snippets From 6ec63874905d77a446a6620e3d5a87991745b8d1 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Sun, 14 Apr 2024 18:35:25 +0200 Subject: [PATCH 16/24] Removed .vscode files (they should have never been there) --- .vscode/launch.json | 26 -------------------------- .vscode/tasks.json | 41 ----------------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 74cd58d..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/Library/Tests/bin/Debug/net8.0/Tests.dll", - "args": [], - "cwd": "${workspaceFolder}/Library/Tests", - // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 5ee20c9..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/Library.sln", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/Library.sln", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/Library.sln" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file From c22ab143f137337789d5a6e9fa1636bb2d046f02 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 20:35:10 +0200 Subject: [PATCH 17/24] add new methods on User with proper tests --- Library/DataLayer/API/IDataRepository.cs | 2 ++ .../Implementations/DataRepository.cs | 27 +++++++++++++++---- Library/Tests/DataLayerTests.cs | 14 ++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs index d052d19..b6cfde0 100644 --- a/Library/DataLayer/API/IDataRepository.cs +++ b/Library/DataLayer/API/IDataRepository.cs @@ -14,6 +14,8 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) IUser GetUser(string guid); List GetAllUsers(); void RemoveUser(string guid); + bool DoesUserExist(string guid); + void UpdateUser(IUser updateUser); #endregion #region Product diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index cfa05b6..e9be72c 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -12,27 +12,44 @@ public DataRepository(IDataContext dataContext) _dataContext = dataContext; } - // -----> User methods + #region User public void AddUser(IUser user) { _dataContext.Users.Add(user); } - public List GetAllUsers() - { - return _dataContext.Users; - } public IUser GetUser(string guid) { IUser? user = _dataContext.Users.FirstOrDefault(u => u.Guid == guid) ?? throw new Exception("User does not exist"); return user; } + public List GetAllUsers() + { + return _dataContext.Users; + } public void RemoveUser(string guid) { IUser user = _dataContext.Users.FirstOrDefault(user => user.Guid == guid) ?? throw new Exception("User does not exist"); _dataContext.Users.Remove(user); } + public bool DoesUserExist(string guid) + { + return _dataContext.Users.Exists(e => e.Guid == guid); + } + public void UpdateUser(IUser updateUser) + { + IUser? userToBeUpdated = _dataContext.Users.FirstOrDefault(u => u.Guid == updateUser.Guid); + + if (userToBeUpdated == null) + { + throw new Exception("Cannot update user that does not exist"); + } + + userToBeUpdated = updateUser; + } + + #endregion // -----> Product methods public void AddProduct(IProduct product) diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index e0ced58..572ad2e 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -46,6 +46,20 @@ public void UserTests() Assert.ThrowsException(() => dataRepository.GetUser("For sure not even a valid guid")); + Assert.AreEqual(user, dataRepository.GetUser(user.Guid)); + + Assert.IsTrue(dataRepository.DoesUserExist(user.Guid)); + Assert.IsFalse(dataRepository.DoesUserExist("For sure not even a valid guid")); + + user.FirstName = "New Name is Jane"; + + dataRepository.UpdateUser(user); + user = dataRepository.GetUser(user.Guid); + + Assert.AreEqual("New Name is Jane", user.FirstName); + + dataRepository.RemoveUser(user.Guid); + Assert.AreEqual(0, dataRepository.GetAllUsers().Count); } From 41dfa004498445380b7a3a8846da33a2448c8fe8 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 20:53:43 +0200 Subject: [PATCH 18/24] add regions and reorder to match interface --- .../Implementations/DataRepository.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index e9be72c..4787dc0 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -51,21 +51,21 @@ public void UpdateUser(IUser updateUser) #endregion - // -----> Product methods + #region Product public void AddProduct(IProduct product) { _dataContext.Products.Add(product); } - public List GetAllProducts() - { - return _dataContext.Products; - } public IProduct GetProduct(string guid) { IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); return product; } + public List GetAllProducts() + { + return _dataContext.Products; + } public IProduct GetProductByState(string stateGuid) { IProduct? product = _dataContext.States.FirstOrDefault(state => state.Guid == stateGuid)?.Product; @@ -82,22 +82,23 @@ public void RemoveProduct(string guid) _dataContext.Products.Remove(product); } + #endregion - // -----> Event methods + #region Event public void AddEvent(IEvent @event) { _dataContext.Events.Add(@event); } - public List GetAllEvents() - { - return _dataContext.Events; - } public IEvent GetEvent(string guid) { IEvent @event = _dataContext.Events.FirstOrDefault(@event => @event.Guid == guid) ?? throw new Exception("Event does not exist"); return @event; } + public List GetAllEvents() + { + return _dataContext.Events; + } public List GetEventsByUser(string userGuid) { List events = _dataContext.Events.Where(@event => @event.User.Guid == userGuid).ToList(); @@ -122,8 +123,9 @@ public void RemoveEvent(string guid) _dataContext.Events.Remove(@event); } + #endregion - // -----> State methods + #region State public void AddState(IState state) { _dataContext.States.Add(state); @@ -144,5 +146,6 @@ public void RemoveState(string guid) _dataContext.States.Remove(state); } + #endregion } } \ No newline at end of file From b2a17aad78c867288235baed6424a2fc67e0c58a Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 21:35:32 +0200 Subject: [PATCH 19/24] hope it will prevent tracking of .vscode --- .gitignore | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 90d069c..d0595a7 100644 --- a/.gitignore +++ b/.gitignore @@ -401,13 +401,13 @@ FodyWeavers.xsd - +.vscode/ .vscode/* -!.vscode/settings.json +.vscode/settings.json .vscode/tasks.json .vscode/launch.json -!.vscode/extensions.json -!.vscode/*.code-snippets +.vscode/extensions.json +.vscode/*.code-snippets # Local History for Visual Studio Code .history/ From b6ce82f8430a2984ed6a176a5614e815ad60e2c8 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 21:36:31 +0200 Subject: [PATCH 20/24] refactor State and it's tests --- Library/DataLayer/API/IState.cs | 3 +-- Library/DataLayer/Implementations/State.cs | 21 ++++++++++++----- Library/Tests/DataLayerTests.cs | 26 ++++++++++------------ Library/Tests/LogicLayerTests.cs | 16 ++++++------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Library/DataLayer/API/IState.cs b/Library/DataLayer/API/IState.cs index 8b88652..eeecc5d 100644 --- a/Library/DataLayer/API/IState.cs +++ b/Library/DataLayer/API/IState.cs @@ -4,8 +4,7 @@ public interface IState { IProduct Product { get; set; } int Quantity { get; set; } - DateTime Date { get; set; } - double Price { get; set; } + DateTime LastUpdatedDate { get; } string Guid { get; } } } diff --git a/Library/DataLayer/Implementations/State.cs b/Library/DataLayer/Implementations/State.cs index af31769..8240a04 100644 --- a/Library/DataLayer/Implementations/State.cs +++ b/Library/DataLayer/Implementations/State.cs @@ -4,17 +4,26 @@ namespace DataLayer.Implementations { public class State : IState { - public State(IProduct product, int quantity, DateTime date, double price, string guid) + private int _quantity; + private DateTime _lastUpdatedDate; + public State(IProduct product, int quantity = 0, string? guid = null) { + Guid = string.IsNullOrEmpty(guid) ? System.Guid.NewGuid().ToString() : guid; Product = product; Quantity = quantity; - Date = date; - Price = price; - Guid = guid; + _lastUpdatedDate = DateTime.Now; } public IProduct Product { get; set; } - public int Quantity { get; set; } - public DateTime Date { get; set; } + public int Quantity + { + get => _quantity; + set + { + _quantity = value; + _lastUpdatedDate = DateTime.Now; + } + } + public DateTime LastUpdatedDate { get => _lastUpdatedDate; } public double Price { get; set; } public string Guid { get; } } diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index 572ad2e..4f7b195 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -97,18 +97,17 @@ public void BookTests() [TestMethod] public void StateTests() { - IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); - int quantity = 10; - DateTime date = new DateTime(2022, 1, 1); - double price = 100.0; - string guid = "1234567890"; + IProduct bookProduct = new Book("Buszujący w Zbożu", 10.0, " J.D. Salinger", "Albatros", 100, new DateTime(1951, 7, 16)); + + const int amountOfBooks = 10; + double wholeStockPrice = bookProduct.Price * amountOfBooks; - IState state = new State(product, quantity, date, price, guid); + IState bookProductState = new State(bookProduct, amountOfBooks); - Assert.AreEqual(product, state.Product); - Assert.AreEqual(quantity, state.Quantity); - Assert.AreEqual(date, state.Date); - Assert.AreEqual(price, state.Price); + Assert.AreEqual(bookProduct, bookProductState.Product); + Assert.AreEqual(amountOfBooks, bookProductState.Quantity); + Assert.IsTrue((DateTime.Now - bookProductState.LastUpdatedDate).TotalSeconds < 1); + Assert.AreEqual(wholeStockPrice, bookProductState.Product.Price * bookProductState.Quantity); } [TestMethod] @@ -119,12 +118,11 @@ public void EventTests() IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); int quantity = 10; - DateTime StateDate = new DateTime(2020, 1, 1); - double price = 100.0; + string guid1 = "1234567890"; string guid2 = "0987654321"; - IState state = new State(product, quantity, StateDate, price, guid1); + IState state = new State(product, quantity, guid1); IEvent myEvent = new Event(user, state, EventDate, guid2); @@ -151,7 +149,7 @@ public void DataRepositoryTests() IUser user = new User("John", "Doe", "Doe", 100.0, 1234567890, null); IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); - IState state = new State(product, 10, new DateTime(2022, 1, 1), 100.0, "1234567890"); + IState state = new State(product, 10, "1234567890"); IEvent myEvent = new Event(user, state, new DateTime(2022, 1, 1), "0987654321"); dataRepository.AddUser(user); diff --git a/Library/Tests/LogicLayerTests.cs b/Library/Tests/LogicLayerTests.cs index d745a22..e85a93f 100644 --- a/Library/Tests/LogicLayerTests.cs +++ b/Library/Tests/LogicLayerTests.cs @@ -60,7 +60,7 @@ public void AddStateTest() DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); dataRepository.AddState(state); @@ -88,7 +88,7 @@ public void AddEventTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); @@ -118,7 +118,7 @@ public void GetEventsByUserTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); @@ -147,7 +147,7 @@ public void GetEventsByProductTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); @@ -169,7 +169,7 @@ public void GetProductByStateTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); dataRepository.AddState(state); @@ -196,7 +196,7 @@ public void GetEventsByStateTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); @@ -259,7 +259,7 @@ public void RemoveStateTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); dataRepository.AddState(state); @@ -288,7 +288,7 @@ public void RemoveEventTest() const int pages = 299; DateTime publicationDate = new DateTime(2022, 1, 1); IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, DateTime.Now, 100.0, "1234567890"); + IState state = new State(product, 20, "1234567890"); IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); From c5aed69aa2f027206c78a1bd22ba1963bee40247 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 22:24:23 +0200 Subject: [PATCH 21/24] refactoring the code of the Event class and its consequences --- Library/DataLayer/API/IEvent.cs | 6 +- Library/DataLayer/Implementations/Borrow.cs | 22 -- Library/DataLayer/Implementations/Event.cs | 19 -- .../Implementations/Events/Borrow.cs | 36 +++ .../Implementations/Events/Delivery.cs | 27 ++ .../Implementations/Events/Return.cs | 28 ++ Library/DataLayer/Implementations/Return.cs | 22 -- Library/Tests/DataLayerTests.cs | 34 +-- Library/Tests/LogicLayerTests.cs | 259 +++++++++--------- 9 files changed, 237 insertions(+), 216 deletions(-) delete mode 100644 Library/DataLayer/Implementations/Borrow.cs delete mode 100644 Library/DataLayer/Implementations/Event.cs create mode 100644 Library/DataLayer/Implementations/Events/Borrow.cs create mode 100644 Library/DataLayer/Implementations/Events/Delivery.cs create mode 100644 Library/DataLayer/Implementations/Events/Return.cs delete mode 100644 Library/DataLayer/Implementations/Return.cs diff --git a/Library/DataLayer/API/IEvent.cs b/Library/DataLayer/API/IEvent.cs index 81b2a35..4bc8a34 100644 --- a/Library/DataLayer/API/IEvent.cs +++ b/Library/DataLayer/API/IEvent.cs @@ -2,9 +2,9 @@ namespace DataLayer.API { public interface IEvent { - IUser User { get; set; } - IState State { get; set; } - DateTime Date { get; set; } string Guid { get; } + IUser User { get; } + IState State { get; } + DateTime CreatedAt { get; } } } diff --git a/Library/DataLayer/Implementations/Borrow.cs b/Library/DataLayer/Implementations/Borrow.cs deleted file mode 100644 index 8dac4f8..0000000 --- a/Library/DataLayer/Implementations/Borrow.cs +++ /dev/null @@ -1,22 +0,0 @@ -using DataLayer.API; - -namespace DataLayer.Implementations -{ - public class Borrow : IEvent - { - public Borrow(IUser user, IState state, DateTime date, string guid) - { - User = user; - State = state; - Date = date; - Guid = guid; - - user.ProductsDic.Add(state.Product.Guid, state.Product); - state.Quantity--; - } - public IUser User { get; set; } - public IState State { get; set; } - public DateTime Date { get; set; } - public string Guid { get; } - } -} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Event.cs b/Library/DataLayer/Implementations/Event.cs deleted file mode 100644 index 8231f46..0000000 --- a/Library/DataLayer/Implementations/Event.cs +++ /dev/null @@ -1,19 +0,0 @@ -using DataLayer.API; - -namespace DataLayer.Implementations -{ - public class Event : IEvent - { - public Event(IUser user, IState state, DateTime date, string guid) - { - User = user; - State = state; - Date = date; - Guid = guid; - } - public IUser User { get; set; } - public IState State { get; set; } - public DateTime Date { get; set; } - public string Guid { get; } - } -} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Events/Borrow.cs b/Library/DataLayer/Implementations/Events/Borrow.cs new file mode 100644 index 0000000..942acaa --- /dev/null +++ b/Library/DataLayer/Implementations/Events/Borrow.cs @@ -0,0 +1,36 @@ +using DataLayer.API; + +namespace DataLayer.Implementations.Events +{ + public class Borrow : IEvent + { + public Borrow(IUser user, IState state, string? guid = null) + { + Guid = string.IsNullOrEmpty(guid) ? System.Guid.NewGuid().ToString() : guid; + User = user; + State = state; + CreatedAt = DateTime.Now; + + // Check if the product is available + if (state.Quantity <= 0) + { + throw new Exception("Product is not available"); + } + + // Should we allow borrowing the product if the user already has it? + + // Check if user has enough credits to borrow the product + if (user.Balance < state.Product.Price) + { + throw new Exception("User balance is not enough to borrow the product"); + } + + user.ProductsDic.Add(state.Product.Guid, state.Product); + state.Quantity--; + } + public string Guid { get; } + public IUser User { get; } + public IState State { get; } + public DateTime CreatedAt { get; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Events/Delivery.cs b/Library/DataLayer/Implementations/Events/Delivery.cs new file mode 100644 index 0000000..f555592 --- /dev/null +++ b/Library/DataLayer/Implementations/Events/Delivery.cs @@ -0,0 +1,27 @@ +using DataLayer.API; + +namespace DataLayer.Implementations.Events +{ + public class Delivery : IEvent + { + public Delivery(IUser user, IState state, int amount, string? guid = null) + { + Guid = string.IsNullOrEmpty(guid) ? System.Guid.NewGuid().ToString() : guid; + User = user; + State = state; + CreatedAt = DateTime.Now; + + if(amount <= 0) + { + throw new Exception("Amount must be greater than 0"); + } + + state.Quantity += amount; + } + + public string Guid { get; } + public IUser User { get; } + public IState State { get; } + public DateTime CreatedAt { get; } + } +} diff --git a/Library/DataLayer/Implementations/Events/Return.cs b/Library/DataLayer/Implementations/Events/Return.cs new file mode 100644 index 0000000..fde3417 --- /dev/null +++ b/Library/DataLayer/Implementations/Events/Return.cs @@ -0,0 +1,28 @@ +using DataLayer.API; + +namespace DataLayer.Implementations.Events +{ + public class Return : IEvent + { + public Return(IUser user, IState state, string? guid = null) + { + Guid = string.IsNullOrEmpty(guid) ? System.Guid.NewGuid().ToString() : guid; + User = user; + State = state; + CreatedAt = DateTime.Now; + + // Check if the product is in the user's inventory because we can't return a product that we don't have + if (!user.ProductsDic.ContainsKey(state.Product.Guid)) + { + throw new Exception("Product not found in user's inventory"); + } + + user.ProductsDic.Remove(state.Product.Guid); + state.Quantity++; + } + public string Guid { get; } + public IUser User { get; } + public IState State { get; } + public DateTime CreatedAt { get; } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/Return.cs b/Library/DataLayer/Implementations/Return.cs deleted file mode 100644 index b6b1e0c..0000000 --- a/Library/DataLayer/Implementations/Return.cs +++ /dev/null @@ -1,22 +0,0 @@ -using DataLayer.API; - -namespace DataLayer.Implementations -{ - public class Return : IEvent - { - public Return(IUser user, IState state, DateTime date, string guid) - { - User = user; - State = state; - Date = date; - Guid = guid; - - user.ProductsDic.Remove(state.Product.Guid); - state.Quantity++; - } - public IUser User { get; set; } - public IState State { get; set; } - public DateTime Date { get; set; } - public string Guid { get; } - } -} \ No newline at end of file diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index 4f7b195..c6ed700 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -1,4 +1,5 @@ using DataLayer.Implementations; +using DataLayer.Implementations.Events; using DataLayer.API; namespace Tests; @@ -111,24 +112,22 @@ public void StateTests() } [TestMethod] - public void EventTests() + public void BorrowEventTests() { - IUser user = new User("John", "Doe", "Doe", 100.0, 1234567890, null); - DateTime EventDate = new DateTime(2022, 1, 1); - - IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); - int quantity = 10; - - string guid1 = "1234567890"; - string guid2 = "0987654321"; + // TODO: Implement BorrowEventTests - IState state = new State(product, quantity, guid1); + } - IEvent myEvent = new Event(user, state, EventDate, guid2); + [TestMethod] + public void ReturnEventTests() + { + // TODO: Implement ReturnEventTests + } - Assert.AreEqual(user, myEvent.User); - Assert.AreEqual(state, myEvent.State); - Assert.AreEqual(EventDate, myEvent.Date); + [TestMethod] + public void DeliveryEventTests() + { + // TODO: Implement DeliveryEventTests } [TestMethod] @@ -150,36 +149,29 @@ public void DataRepositoryTests() IUser user = new User("John", "Doe", "Doe", 100.0, 1234567890, null); IProduct product = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1)); IState state = new State(product, 10, "1234567890"); - IEvent myEvent = new Event(user, state, new DateTime(2022, 1, 1), "0987654321"); dataRepository.AddUser(user); dataRepository.AddProduct(product); dataRepository.AddState(state); - dataRepository.AddEvent(myEvent); Assert.IsTrue(dataRepository.GetAllUsers().Contains(user)); Assert.IsTrue(dataRepository.GetAllProducts().Contains(product)); Assert.IsTrue(dataRepository.GetAllStates().Contains(state)); - Assert.IsTrue(dataRepository.GetAllEvents().Contains(myEvent)); Assert.AreEqual(user, dataRepository.GetUser(user.Guid)); Assert.AreEqual(product, dataRepository.GetProduct(product.Guid)); Assert.AreEqual(state, dataRepository.GetState(state.Guid)); - Assert.AreEqual(myEvent, dataRepository.GetEvent(myEvent.Guid)); Assert.ThrowsException(() => dataRepository.GetUser("For sure not even a valid guid")); Assert.ThrowsException(() => dataRepository.GetProduct("For sure not even a valid guid")); Assert.ThrowsException(() => dataRepository.GetState("For sure not even a valid guid")); - Assert.ThrowsException(() => dataRepository.GetEvent("For sure not even a valid guid")); dataRepository.RemoveUser(user.Guid); dataRepository.RemoveProduct(product.Guid); dataRepository.RemoveState(state.Guid); - dataRepository.RemoveEvent(myEvent.Guid); Assert.IsFalse(dataRepository.GetAllUsers().Contains(user)); Assert.IsFalse(dataRepository.GetAllProducts().Contains(product)); Assert.IsFalse(dataRepository.GetAllStates().Contains(state)); - Assert.IsFalse(dataRepository.GetAllEvents().Contains(myEvent)); } } diff --git a/Library/Tests/LogicLayerTests.cs b/Library/Tests/LogicLayerTests.cs index e85a93f..5e7b11d 100644 --- a/Library/Tests/LogicLayerTests.cs +++ b/Library/Tests/LogicLayerTests.cs @@ -74,90 +74,91 @@ public void AddStateTest() [TestMethod] public void AddEventTest() { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); - - IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddEvent(@event); - - IDataService dataService = new DataService(dataRepository); - - Assert.AreEqual(@event, dataService.GetEvent(@event.Guid)); - Assert.IsTrue(dataService.GetAllEvents().Contains(@event)); - } - - [TestMethod] - public void GetEventsByUserTest() - { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); - - IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddEvent(@event); - - IDataService dataService = new DataService(dataRepository); - - Assert.IsTrue(dataService.GetEventsByUser(user.Guid).Contains(@event)); + // TODO Implement + // const string firstName = "John"; + // const string lastName = "Doe"; + // const string email = "Doe"; + // const double balance = 100.0; + // const int phoneNumber = 1234567890; + // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + // const string name = "Sample Book"; + // const string author = "John Doe"; + // const string publisher = "Publisher X"; + // const double price = 29.99; + // const int pages = 299; + // DateTime publicationDate = new DateTime(2022, 1, 1); + // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + // IState state = new State(product, 20, "1234567890"); + + // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + // dataRepository.AddEvent(@event); + + // IDataService dataService = new DataService(dataRepository); + + // Assert.AreEqual(@event, dataService.GetEvent(@event.Guid)); + // Assert.IsTrue(dataService.GetAllEvents().Contains(@event)); } - [TestMethod] - public void GetEventsByProductTest() - { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); - - IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddEvent(@event); - - IDataService dataService = new DataService(dataRepository); - - Assert.IsTrue(dataService.GetEventsByProduct(product.Guid).Contains(@event)); - } + // [TestMethod] + // public void GetEventsByUserTest() + // { + // const string firstName = "John"; + // const string lastName = "Doe"; + // const string email = "Doe"; + // const double balance = 100.0; + // const int phoneNumber = 1234567890; + // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + // const string name = "Sample Book"; + // const string author = "John Doe"; + // const string publisher = "Publisher X"; + // const double price = 29.99; + // const int pages = 299; + // DateTime publicationDate = new DateTime(2022, 1, 1); + // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + // IState state = new State(product, 20, "1234567890"); + + // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + // dataRepository.AddEvent(@event); + + // IDataService dataService = new DataService(dataRepository); + + // Assert.IsTrue(dataService.GetEventsByUser(user.Guid).Contains(@event)); + // } + + // [TestMethod] + // public void GetEventsByProductTest() + // { + // const string firstName = "John"; + // const string lastName = "Doe"; + // const string email = "Doe"; + // const double balance = 100.0; + // const int phoneNumber = 1234567890; + // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + + // const string name = "Sample Book"; + // const string author = "John Doe"; + // const string publisher = "Publisher X"; + // const double price = 29.99; + // const int pages = 299; + // DateTime publicationDate = new DateTime(2022, 1, 1); + // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + // IState state = new State(product, 20, "1234567890"); + + // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + + // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + // dataRepository.AddEvent(@event); + + // IDataService dataService = new DataService(dataRepository); + + // Assert.IsTrue(dataService.GetEventsByProduct(product.Guid).Contains(@event)); + // } [TestMethod] public void GetProductByStateTest() @@ -179,34 +180,34 @@ public void GetProductByStateTest() Assert.AreEqual(product, dataService.GetProductByState(state.Guid)); } - [TestMethod] - public void GetEventsByStateTest() - { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + // [TestMethod] + // public void GetEventsByStateTest() + // { + // const string firstName = "John"; + // const string lastName = "Doe"; + // const string email = "Doe"; + // const double balance = 100.0; + // const int phoneNumber = 1234567890; + // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); + // const string name = "Sample Book"; + // const string author = "John Doe"; + // const string publisher = "Publisher X"; + // const double price = 29.99; + // const int pages = 299; + // DateTime publicationDate = new DateTime(2022, 1, 1); + // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + // IState state = new State(product, 20, "1234567890"); - IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddEvent(@event); + // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + // dataRepository.AddEvent(@event); - IDataService dataService = new DataService(dataRepository); + // IDataService dataService = new DataService(dataRepository); - Assert.IsTrue(dataService.GetEventsByState(state.Guid).Contains(@event)); - } + // Assert.IsTrue(dataService.GetEventsByState(state.Guid).Contains(@event)); + // } [TestMethod] public void RemoveUserTest() @@ -271,34 +272,34 @@ public void RemoveStateTest() Assert.IsFalse(dataService.GetAllStates().Contains(state)); } - [TestMethod] - public void RemoveEventTest() - { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + // [TestMethod] + // public void RemoveEventTest() + // { + // const string firstName = "John"; + // const string lastName = "Doe"; + // const string email = "Doe"; + // const double balance = 100.0; + // const int phoneNumber = 1234567890; + // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); + // const string name = "Sample Book"; + // const string author = "John Doe"; + // const string publisher = "Publisher X"; + // const double price = 29.99; + // const int pages = 299; + // DateTime publicationDate = new DateTime(2022, 1, 1); + // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + // IState state = new State(product, 20, "1234567890"); - IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddEvent(@event); + // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); + // dataRepository.AddEvent(@event); - IDataService dataService = new DataService(dataRepository); + // IDataService dataService = new DataService(dataRepository); - dataService.RemoveEvent(@event.Guid); + // dataService.RemoveEvent(@event.Guid); - Assert.IsFalse(dataService.GetAllEvents().Contains(@event)); - } + // Assert.IsFalse(dataService.GetAllEvents().Contains(@event)); + // } } \ No newline at end of file From 110fac4f6a4ad7d1e3d569017e6b9f5cf5700189 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 23:06:55 +0200 Subject: [PATCH 22/24] add method DoesProductExist --- Library/DataLayer/API/IDataRepository.cs | 2 ++ Library/DataLayer/Implementations/DataRepository.cs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/Library/DataLayer/API/IDataRepository.cs b/Library/DataLayer/API/IDataRepository.cs index b6cfde0..6c6cb65 100644 --- a/Library/DataLayer/API/IDataRepository.cs +++ b/Library/DataLayer/API/IDataRepository.cs @@ -23,6 +23,8 @@ static IDataRepository CreateDataRepository(IDataContext dataContext) IProduct GetProduct(string guid); List GetAllProducts(); IProduct GetProductByState(string stateGuid); + bool DoesProductExist(string guid); + void RemoveProduct(string guid); #endregion diff --git a/Library/DataLayer/Implementations/DataRepository.cs b/Library/DataLayer/Implementations/DataRepository.cs index 4787dc0..b6744fe 100644 --- a/Library/DataLayer/Implementations/DataRepository.cs +++ b/Library/DataLayer/Implementations/DataRepository.cs @@ -76,6 +76,10 @@ public IProduct GetProductByState(string stateGuid) return product; } + public bool DoesProductExist(string guid) + { + return _dataContext.Products.Exists(e => e.Guid == guid); + } public void RemoveProduct(string guid) { IProduct product = _dataContext.Products.FirstOrDefault(product => product.Guid == guid) ?? throw new Exception("Product does not exist"); From 145287165686d285a1a9def444a7e0faebf7f023 Mon Sep 17 00:00:00 2001 From: Oskarowski Date: Sun, 14 Apr 2024 23:07:35 +0200 Subject: [PATCH 23/24] refactor business logic, test also --- Library/LogicLayer/API/IDataService.cs | 51 +-- .../LogicLayer/Implementations/DataService.cs | 155 ++++----- Library/Tests/LogicLayerTests.cs | 300 +++--------------- 3 files changed, 114 insertions(+), 392 deletions(-) diff --git a/Library/LogicLayer/API/IDataService.cs b/Library/LogicLayer/API/IDataService.cs index 50a208b..9fee60a 100644 --- a/Library/LogicLayer/API/IDataService.cs +++ b/Library/LogicLayer/API/IDataService.cs @@ -1,50 +1,19 @@ using DataLayer.API; +using DataLayer.Implementations.Events; namespace LogicLayer.API { public interface IDataService { - #region Get - IProduct GetProduct(string guid); - IState GetState(string guid); - IUser GetUser(string guid); - IEvent GetEvent(string guid); - #endregion + void BorrowProduct(IUser user, IState state); + void ReturnProduct(IUser user, IState state); + void DeliverProduct(IUser user, IState state, int amount); - // #region Add - // void AddProduct(IProduct product); - // void AddState(IState state); - // void AddUser(IUser user); - // void AddEvent(IEvent @event); - // #endregion - - #region Remove - void RemoveProduct(string guid); - void RemoveState(string guid); - void RemoveUser(string guid); - void RemoveEvent(string guid); - #endregion - - // #region Create - // IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate); - // IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid); - // IEvent CreateReturn(IUser user, IState state, DateTime date, string guid); - // IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber); - // IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid); - // #endregion - - #region GetBy - List GetEventsByUser(string userGuid); - List GetEventsByProduct(string productGuid); - IProduct GetProductByState(string stateGuid); - List GetEventsByState(string stateGuid); - #endregion - - #region GetAll - List GetAllProducts(); - List GetAllStates(); - List GetAllUsers(); - List GetAllEvents(); - #endregion + // Maybe add in the future + // void RegisterUser(string firstName, string lastName, string email, double balance, int phoneNumber); + // void RegisterProduct + // void UpdateUserBalance + // void UpdateProductPrice + // bool CheckProductAvailability } } \ No newline at end of file diff --git a/Library/LogicLayer/Implementations/DataService.cs b/Library/LogicLayer/Implementations/DataService.cs index 7aecb73..d3bea51 100644 --- a/Library/LogicLayer/Implementations/DataService.cs +++ b/Library/LogicLayer/Implementations/DataService.cs @@ -1,4 +1,5 @@ using DataLayer.API; +using DataLayer.Implementations.Events; using LogicLayer.API; namespace LogicLayer.Implementations @@ -10,109 +11,73 @@ public DataService(IDataRepository dataRepository) { _dataRepository = dataRepository; } - - #region Get - public IUser GetUser(string guid) - { - return _dataRepository.GetUser(guid); - } - public IProduct GetProduct(string guid) - { - return _dataRepository.GetProduct(guid); - } - public IState GetState(string guid) + public void DeliverProduct(IUser user, IState state, int amount) { - return _dataRepository.GetState(guid); - } - public IEvent GetEvent(string guid) - { - return _dataRepository.GetEvent(guid); - } - #endregion - - #region GetAll - public List GetAllUsers() - { - return _dataRepository.GetAllUsers(); - } - public List GetAllProducts() - { - return _dataRepository.GetAllProducts(); - } - public List GetAllStates() - { - return _dataRepository.GetAllStates(); - } - public List GetAllEvents() - { - return _dataRepository.GetAllEvents(); - } - #endregion - #region GetBy - public List GetEventsByUser(string userGuid) - { - return _dataRepository.GetEventsByUser(userGuid); - } - public List GetEventsByProduct(string productGuid) - { - return _dataRepository.GetEventsByProduct(productGuid); - } - public IProduct GetProductByState(string stateGuid) - { - return _dataRepository.GetProductByState(stateGuid); - } - public List GetEventsByState(string stateGuid) - { - return _dataRepository.GetEventsByState(stateGuid); - } - #endregion + // Check if such product exists + if (!_dataRepository.DoesProductExist(state.Product.Guid)) + { + throw new Exception("Product not found"); + } - // #region Create - // public IUser CreateUser(string guid, string firstName, string lastName, string email, int phoneNumber) - // { - // IUser user = new User(guid, firstName, lastName, email, phoneNumber); - // return _dataRepository.AddUser(new User(guid, firstName, lastName, email, phoneNumber)); - // } + if (!_dataRepository.DoesUserExist(user.Guid)) + { + throw new Exception("User not found"); + } - // public IProduct CreateBook(string name, string guid, double price, string author, string publisher, int pages, DateTime publicationDate) - // { - // return _dataRepository.CreateBook(name, guid, price, author, publisher, pages, publicationDate); - // } + try + { + _dataRepository.AddEvent(new Delivery(user, state, amount)); + } + catch (Exception e) + { + throw new Exception("Unable to perform deliver: " + e.Message, e); ; + } - // public IState CreateState(IProduct product, int quantity, DateTime date, double price, string guid) - // { - // return _dataRepository.CreateState(product, quantity, date, price, guid); - // } + } - // public IEvent CreateBorrow(IUser user, IState state, DateTime date, string guid) - // { - // return _dataRepository.CreateBorrow(user, state, date, guid); - // } + public void BorrowProduct(IUser user, IState state) + { + // Check if such product exists + if (!_dataRepository.DoesProductExist(state.Product.Guid)) + { + throw new Exception("Product not found"); + } + if (!_dataRepository.DoesUserExist(user.Guid)) + { + throw new Exception("User not found"); + } - // public IEvent CreateReturn(IUser user, IState state, DateTime date, string guid) - // { - // return _dataRepository.CreateReturn(user, state, date, guid); - // } - // #endregion + try + { + _dataRepository.AddEvent(new Borrow(user, state)); + } + catch (Exception e) + { + throw new Exception("Unable to perform borrow: " + e.Message, e); ; + } + } + public void ReturnProduct(IUser user, IState state) + { + // Check if such product exists + if (!_dataRepository.DoesProductExist(state.Product.Guid)) + { + throw new Exception("Product not found"); + } + if (!_dataRepository.DoesUserExist(user.Guid)) + { + throw new Exception("User not found"); + } - #region Remove - public void RemoveUser(string guid) - { - _dataRepository.RemoveUser(guid); - } - public void RemoveProduct(string guid) - { - _dataRepository.RemoveProduct(guid); - } - public void RemoveState(string guid) - { - _dataRepository.RemoveState(guid); - } - public void RemoveEvent(string guid) - { - _dataRepository.RemoveEvent(guid); + try + { + _dataRepository.AddEvent(new Return(user, state)); + } + catch (Exception e) + { + throw new Exception("Unable to perform return: " + e.Message, e); ; + } } - #endregion + } } \ No newline at end of file diff --git a/Library/Tests/LogicLayerTests.cs b/Library/Tests/LogicLayerTests.cs index 5e7b11d..4bc313f 100644 --- a/Library/Tests/LogicLayerTests.cs +++ b/Library/Tests/LogicLayerTests.cs @@ -9,297 +9,85 @@ namespace Tests; public class LogicLayerTests { [TestMethod] - public void AddUserTest() + public void TestDeliverProduct_Success() { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - + // Arrange IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddUser(user); - IDataService dataService = new DataService(dataRepository); + var testUser1 = new User("User 1", "1", "1@gmail.com", 1000, 1234567890, null); - Assert.AreEqual(user, dataService.GetUser(user.Guid)); - Assert.IsTrue(dataService.GetAllUsers().Contains(user)); - } + const int amountOfBooksOnStart = 5; + IProduct bookProduct1 = new Book("Buszujący w Zbożu", 10.0, " J.D. Salinger", "Albatros", 258, new DateTime(1951, 7, 16)); - [TestMethod] - public void AddProductTest() - { - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); + var stateOfBookProduct1 = new State(bookProduct1, amountOfBooksOnStart); - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddProduct(product); + dataRepository.AddUser(testUser1); + dataRepository.AddProduct(bookProduct1); + dataRepository.AddState(stateOfBookProduct1); IDataService dataService = new DataService(dataRepository); - Assert.AreEqual(product, dataService.GetProduct(product.Guid)); - Assert.IsTrue(dataService.GetAllProducts().Contains(product)); - } - - [TestMethod] - public void AddStateTest() - { - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - - IState state = new State(product, 20, "1234567890"); - - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddState(state); - - IDataService dataService = new DataService(dataRepository); + // Act + const int amountOfBooksToDeliver = 5; + dataService.DeliverProduct(testUser1, stateOfBookProduct1, amountOfBooksToDeliver); - Assert.AreEqual(state, dataService.GetState(state.Guid)); - Assert.IsTrue(dataService.GetAllStates().Contains(state)); + // Assert + Assert.AreEqual(amountOfBooksOnStart + amountOfBooksToDeliver, stateOfBookProduct1.Quantity); } [TestMethod] - public void AddEventTest() + public void TestBorrowProduct_Success() { - // TODO Implement - // const string firstName = "John"; - // const string lastName = "Doe"; - // const string email = "Doe"; - // const double balance = 100.0; - // const int phoneNumber = 1234567890; - // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - // const string name = "Sample Book"; - // const string author = "John Doe"; - // const string publisher = "Publisher X"; - // const double price = 29.99; - // const int pages = 299; - // DateTime publicationDate = new DateTime(2022, 1, 1); - // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - // IState state = new State(product, 20, "1234567890"); - - // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - // dataRepository.AddEvent(@event); - - // IDataService dataService = new DataService(dataRepository); - - // Assert.AreEqual(@event, dataService.GetEvent(@event.Guid)); - // Assert.IsTrue(dataService.GetAllEvents().Contains(@event)); - } - - // [TestMethod] - // public void GetEventsByUserTest() - // { - // const string firstName = "John"; - // const string lastName = "Doe"; - // const string email = "Doe"; - // const double balance = 100.0; - // const int phoneNumber = 1234567890; - // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - // const string name = "Sample Book"; - // const string author = "John Doe"; - // const string publisher = "Publisher X"; - // const double price = 29.99; - // const int pages = 299; - // DateTime publicationDate = new DateTime(2022, 1, 1); - // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - // IState state = new State(product, 20, "1234567890"); - - // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - // dataRepository.AddEvent(@event); - - // IDataService dataService = new DataService(dataRepository); - - // Assert.IsTrue(dataService.GetEventsByUser(user.Guid).Contains(@event)); - // } - - // [TestMethod] - // public void GetEventsByProductTest() - // { - // const string firstName = "John"; - // const string lastName = "Doe"; - // const string email = "Doe"; - // const double balance = 100.0; - // const int phoneNumber = 1234567890; - // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - // const string name = "Sample Book"; - // const string author = "John Doe"; - // const string publisher = "Publisher X"; - // const double price = 29.99; - // const int pages = 299; - // DateTime publicationDate = new DateTime(2022, 1, 1); - // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - // IState state = new State(product, 20, "1234567890"); - - // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); - - // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - // dataRepository.AddEvent(@event); - - // IDataService dataService = new DataService(dataRepository); - - // Assert.IsTrue(dataService.GetEventsByProduct(product.Guid).Contains(@event)); - // } - - [TestMethod] - public void GetProductByStateTest() - { - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); - + // Arrange IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddState(state); - - IDataService dataService = new DataService(dataRepository); - Assert.AreEqual(product, dataService.GetProductByState(state.Guid)); - } - - // [TestMethod] - // public void GetEventsByStateTest() - // { - // const string firstName = "John"; - // const string lastName = "Doe"; - // const string email = "Doe"; - // const double balance = 100.0; - // const int phoneNumber = 1234567890; - // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - // const string name = "Sample Book"; - // const string author = "John Doe"; - // const string publisher = "Publisher X"; - // const double price = 29.99; - // const int pages = 299; - // DateTime publicationDate = new DateTime(2022, 1, 1); - // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - // IState state = new State(product, 20, "1234567890"); - - // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + var testUser1 = new User("User 1", "1", "1@gmail.com", 1000, 1234567890, null); - // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - // dataRepository.AddEvent(@event); + const int amountOfBooksOnStart = 5; + IProduct bookProduct1 = new Book("Buszujący w Zbożu", 10.0, " J.D. Salinger", "Albatros", 258, new DateTime(1951, 7, 16)); - // IDataService dataService = new DataService(dataRepository); - - // Assert.IsTrue(dataService.GetEventsByState(state.Guid).Contains(@event)); - // } - - [TestMethod] - public void RemoveUserTest() - { - const string firstName = "John"; - const string lastName = "Doe"; - const string email = "Doe"; - const double balance = 100.0; - const int phoneNumber = 1234567890; - IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); + var stateOfBookProduct1 = new State(bookProduct1, amountOfBooksOnStart); - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddUser(user); + dataRepository.AddUser(testUser1); + dataRepository.AddProduct(bookProduct1); + dataRepository.AddState(stateOfBookProduct1); IDataService dataService = new DataService(dataRepository); - dataService.RemoveUser(user.Guid); + // Act + dataService.BorrowProduct(testUser1, stateOfBookProduct1); - Assert.IsFalse(dataService.GetAllUsers().Contains(user)); + // Assert + Assert.IsTrue(testUser1.ProductsDic.ContainsKey(bookProduct1.Guid)); + Assert.AreEqual(amountOfBooksOnStart - 1, stateOfBookProduct1.Quantity); } [TestMethod] - public void RemoveProductTest() + public void TestReturnProduct_Success() { - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - + // Arrange IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddProduct(product); - - IDataService dataService = new DataService(dataRepository); - dataService.RemoveProduct(product.Guid); + var testUser1 = new User("User 1", "1", "1@gmail.com", 1000, 1234567890, null); - Assert.IsFalse(dataService.GetAllProducts().Contains(product)); - } + const int amountOfBooksOnStart = 5; + IProduct bookProduct1 = new Book("Buszujący w Zbożu", 10.0, " J.D. Salinger", "Albatros", 258, new DateTime(1951, 7, 16)); - [TestMethod] - public void RemoveStateTest() - { - const string name = "Sample Book"; - const string author = "John Doe"; - const string publisher = "Publisher X"; - const double price = 29.99; - const int pages = 299; - DateTime publicationDate = new DateTime(2022, 1, 1); - IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - IState state = new State(product, 20, "1234567890"); + var stateOfBookProduct1 = new State(bookProduct1, amountOfBooksOnStart); - IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - dataRepository.AddState(state); + dataRepository.AddUser(testUser1); + dataRepository.AddProduct(bookProduct1); + dataRepository.AddState(stateOfBookProduct1); IDataService dataService = new DataService(dataRepository); - dataService.RemoveState(state.Guid); + dataService.BorrowProduct(testUser1, stateOfBookProduct1); - Assert.IsFalse(dataService.GetAllStates().Contains(state)); - } - - // [TestMethod] - // public void RemoveEventTest() - // { - // const string firstName = "John"; - // const string lastName = "Doe"; - // const string email = "Doe"; - // const double balance = 100.0; - // const int phoneNumber = 1234567890; - // IUser user = new User(firstName, lastName, email, balance, phoneNumber, null); - - // const string name = "Sample Book"; - // const string author = "John Doe"; - // const string publisher = "Publisher X"; - // const double price = 29.99; - // const int pages = 299; - // DateTime publicationDate = new DateTime(2022, 1, 1); - // IProduct product = new Book(name, price, author, publisher, pages, publicationDate); - // IState state = new State(product, 20, "1234567890"); - - // IEvent @event = new Event(user, state, DateTime.Now, "1111111111"); + // Act + dataService.ReturnProduct(testUser1, stateOfBookProduct1); - // IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext()); - // dataRepository.AddEvent(@event); - - // IDataService dataService = new DataService(dataRepository); - - // dataService.RemoveEvent(@event.Guid); - - // Assert.IsFalse(dataService.GetAllEvents().Contains(@event)); - // } + // Assert + Assert.IsFalse(testUser1.ProductsDic.ContainsKey(bookProduct1.Guid)); + Assert.AreEqual(amountOfBooksOnStart, stateOfBookProduct1.Quantity); + } } \ No newline at end of file From c00ac8069329887d6b22970bed8e06f2ff919d09 Mon Sep 17 00:00:00 2001 From: 247027 Wojciech Kapica <247027@edu.p.lodz.pl> Date: Mon, 15 Apr 2024 11:54:37 +0200 Subject: [PATCH 24/24] Fixed RandomFiller multiple unwanted events being created --- Library/DataLayer/API/IDataFiller.cs | 7 + .../DataFillers/PresetFiller.cs | 37 ++++ .../DataFillers/RandomFiller.cs | 158 ++++++++++++++++++ Library/Tests/DataLayerTests.cs | 24 +++ 4 files changed, 226 insertions(+) create mode 100644 Library/DataLayer/API/IDataFiller.cs create mode 100644 Library/DataLayer/Implementations/DataFillers/PresetFiller.cs create mode 100644 Library/DataLayer/Implementations/DataFillers/RandomFiller.cs diff --git a/Library/DataLayer/API/IDataFiller.cs b/Library/DataLayer/API/IDataFiller.cs new file mode 100644 index 0000000..1f07318 --- /dev/null +++ b/Library/DataLayer/API/IDataFiller.cs @@ -0,0 +1,7 @@ +namespace DataLayer.API +{ + public interface IDataFiller + { + void Fill(IDataContext context); + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataFillers/PresetFiller.cs b/Library/DataLayer/Implementations/DataFillers/PresetFiller.cs new file mode 100644 index 0000000..be5a032 --- /dev/null +++ b/Library/DataLayer/Implementations/DataFillers/PresetFiller.cs @@ -0,0 +1,37 @@ +using DataLayer.API; +using DataLayer.Implementations.Events; + +namespace DataLayer.Implementations +{ + public class PresetFiller : IDataFiller + { + public void Fill(IDataContext context) + { + context.Users.Add(new User("John", "Doe", "johndoe@email", 100, 123456789, null)); + context.Users.Add(new User("Jane", "Doe", "janedoe@email", 200, 111111111, null)); + context.Users.Add(new User("Alice", "Smith", "alicesmith@email", 300, 121212121, null)); + context.Users.Add(new User("Bob", "Smith", "bobsmith@email", 400, 987654321, null)); + context.Users.Add(new User("Charlie", "Brown", "charliebrown@email", 500, 999999999, null)); + + context.Products.Add(new Book("Old Man and the Sea", 10, "Ernest Hemingway", "Charles Sons", 300, new DateTime(1952, 1, 1))); + context.Products.Add(new Book("The Great Gatsby", 20, "F. Scott Fitzgerald", "Charles Sons", 400, new DateTime(1925, 1, 1))); + context.Products.Add(new Book("To Kill a Mockingbird", 30, "Harper Lee", "Charles Sons", 500, new DateTime(1960, 1, 1))); + context.Products.Add(new Book("1984", 40, "George Orwell", "Charles Sons", 600, new DateTime(1949, 1, 1))); + context.Products.Add(new Book("Brave New World", 50, "Aldous Huxley", "Charles Sons", 700, new DateTime(1932, 1, 1))); + + context.States.Add(new State(context.Products[0], 5)); + context.States.Add(new State(context.Products[1], 5)); + context.States.Add(new State(context.Products[2], 5)); + context.States.Add(new State(context.Products[3], 5)); + context.States.Add(new State(context.Products[4], 5)); + + context.Events.Add(new Borrow(context.Users[0], context.States[0])); + context.Events.Add(new Borrow(context.Users[1], context.States[1])); + context.Events.Add(new Borrow(context.Users[3], context.States[3])); + context.Events.Add(new Return(context.Users[0], context.States[0])); + context.Events.Add(new Return(context.Users[1], context.States[1])); + context.Events.Add(new Return(context.Users[3], context.States[3])); + context.Events.Add(new Delivery(context.Users[4], context.States[4], 10)); + } + } +} \ No newline at end of file diff --git a/Library/DataLayer/Implementations/DataFillers/RandomFiller.cs b/Library/DataLayer/Implementations/DataFillers/RandomFiller.cs new file mode 100644 index 0000000..922f2c7 --- /dev/null +++ b/Library/DataLayer/Implementations/DataFillers/RandomFiller.cs @@ -0,0 +1,158 @@ +using System; +using DataLayer.API; +using DataLayer.Implementations.Events; + +namespace DataLayer.Implementations +{ + public class RandomFiller : IDataFiller + { + private Random random = new Random(); + public void Fill(IDataContext context) + { + int usersCount = random.Next(5, 11); + for (int i = 0; i < usersCount; i++) + { + context.Users.Add(new User( + GetRandomFirstName(), + GetRandomLastName(), + $"{GetRandomFirstName().ToLower()}{GetRandomLastName().ToLower()}@email.com", + random.Next(100, 1000), + GetRandomPhoneNumber(), + null + )); + } + + int productsCount = random.Next(5, 11); + for (int i = 0; i < productsCount; i++) + { + context.Products.Add(new Book( + GetRandomTitle(), + random.Next(10, 100), + GetRandomAuthor(), + GetRandomPublisher(), + random.Next(50, 1000), + GetRandomDate() + )); + } + + int statesCount = context.Products.Count; + for (int i = 0; i < statesCount; i++) + { + context.States.Add(new State( + context.Products[i], + random.Next(1, 10) + )); + } + + int eventsCount = random.Next(5, 11); + int borrowsCount = 0; + List usersBorrowing = new List(); + List statesBorrowed = new List(); + for (int i = 0; i < eventsCount; i++) + { + int userIndex = random.Next(context.Users.Count); + int stateIndex = random.Next(context.States.Count); + int randomEvent = random.Next(3); + if (randomEvent == 0) + { + context.Events.Add(new Borrow(context.Users[userIndex], context.States[stateIndex])); + borrowsCount++; + usersBorrowing.Add(context.Users[userIndex]); + statesBorrowed.Add(context.States[stateIndex]); + } + else if (randomEvent == 1 && borrowsCount > 0) + { + context.Events.Add(new Return(usersBorrowing[borrowsCount-1], statesBorrowed[borrowsCount-1])); + borrowsCount--; + usersBorrowing.RemoveAt(borrowsCount); + statesBorrowed.RemoveAt(borrowsCount); + } + else if (randomEvent == 1 && borrowsCount == 0) + { + i--; + } + else if (randomEvent == 2) + { + context.Events.Add(new Delivery(context.Users[userIndex], context.States[stateIndex], random.Next(1, 10))); + } + } + } + private string GetRandomFirstName() + { + string[] firstNames = { + "John", "Jane", "Alice", "Bob", "Charlie", + "Michael", "Emily", "David", "Sarah", "Daniel", + "Matthew", "Jessica", "Andrew", "Jennifer", "James", + "Linda", "William", "Karen", "Joseph", "Maria", + "Robert", "Susan", "Christopher", "Nancy", "Daniel", + "Karen", "Thomas", "Lisa", "Brian", "Margaret", + "Steven", "Betty", "Timothy", "Dorothy", "Kevin", + "Sandra", "Richard", "Ashley", "Mark", "Kimberly", + "Jeffrey", "Donna", "Scott", "Emily", "Charles", + "Carol", "Paul", "Michelle", "Anthony", "Laura" + }; + return firstNames[random.Next(firstNames.Length)]; + } + private string GetRandomLastName() + { + string[] lastNames = { + "Smith", "Johnson", "Williams", "Brown", "Jones", + "Garcia", "Miller", "Davis", "Rodriguez", "Martinez", + "Hernandez", "Lopez", "Gonzalez", "Wilson", "Anderson", + "Thomas", "Taylor", "Moore", "Jackson", "Martin", + "Lee", "Perez", "Thompson", "White", "Harris", + "Sanchez", "Clark", "Ramirez", "Lewis", "Robinson", + "Walker", "Young", "Allen", "King", "Wright", + "Scott", "Torres", "Nguyen", "Hill", "Flores", + "Green", "Adams", "Nelson", "Baker", "Hall", + "Rivera", "Campbell", "Mitchell", "Carter", "Roberts" + }; + return lastNames[random.Next(lastNames.Length)]; + } + private int GetRandomPhoneNumber() + { + return random.Next(100000000, 1000000000); // 9-digit random number + } + private string GetRandomTitle() + { + string[] titles = { + "The Catcher in the Rye", "To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice", + "Harry Potter and the Philosopher's Stone", "The Hobbit", "The Lord of the Rings", "The Hunger Games", "The Da Vinci Code", + "The Chronicles of Narnia", "Twilight", "The Alchemist", "The Fault in Our Stars", "The Kite Runner", + "A Song of Ice and Fire", "The Hitchhiker's Guide to the Galaxy", "The Shining", "The Girl with the Dragon Tattoo", "Gone Girl", + "Brave New World", "Animal Farm", "The Adventures of Huckleberry Finn", "Moby-Dick", "Frankenstein", + "Dracula", "Alice's Adventures in Wonderland", "The War of the Worlds", "The Picture of Dorian Gray", "The Bell Jar" + }; + return titles[random.Next(titles.Length)]; + } + private string GetRandomAuthor() + { + string[] authors = { + "J.D. Salinger", "Harper Lee", "George Orwell", "F. Scott Fitzgerald", "Jane Austen", + "J.K. Rowling", "J.R.R. Tolkien", "George R.R. Martin", "Suzanne Collins", "Dan Brown", + "C.S. Lewis", "Stephenie Meyer", "Paulo Coelho", "John Green", "Khaled Hosseini", + "George R.R. Martin", "Douglas Adams", "Stephen King", "Stieg Larsson", "Gillian Flynn", + "Aldous Huxley", "George Orwell", "Mark Twain", "Herman Melville", "Mary Shelley", + "Bram Stoker", "Lewis Carroll", "H.G. Wells", "Oscar Wilde", "Sylvia Plath" + }; + return authors[random.Next(authors.Length)]; + } + private string GetRandomPublisher() + { + string[] publishers = { + "Random House", "Penguin Books", "HarperCollins", "Simon & Schuster", "Hachette Livre", + "Macmillan Publishers", "Scholastic Corporation", "Pearson Education", "Bloomsbury Publishing", "Oxford University Press", + "Cambridge University Press", "Wiley", "Springer Nature", "McGraw-Hill Education", "Cengage", + "Houghton Mifflin Harcourt", "Elsevier", "Taylor & Francis", "Harvard University Press", "MIT Press" + }; + return publishers[random.Next(publishers.Length)]; + } + private DateTime GetRandomDate() + { + // Random date between 1900 and now + DateTime start = new DateTime(1900, 1, 1); + int range = (DateTime.Today - start).Days; + return start.AddDays(random.Next(range)); + } + } +} \ No newline at end of file diff --git a/Library/Tests/DataLayerTests.cs b/Library/Tests/DataLayerTests.cs index c6ed700..4932c78 100644 --- a/Library/Tests/DataLayerTests.cs +++ b/Library/Tests/DataLayerTests.cs @@ -174,4 +174,28 @@ public void DataRepositoryTests() Assert.IsFalse(dataRepository.GetAllProducts().Contains(product)); Assert.IsFalse(dataRepository.GetAllStates().Contains(state)); } + + [TestMethod] + public void DataFillerTests() + { + IDataContext dataContext = new DataContext(); + IDataFiller dataFiller = new PresetFiller(); + + dataFiller.Fill(dataContext); + + Assert.AreEqual(5, dataContext.Users.Count); + Assert.AreEqual(5, dataContext.Products.Count); + Assert.AreEqual(5, dataContext.States.Count); + Assert.AreEqual(7, dataContext.Events.Count); + + dataContext = new DataContext(); + dataFiller = new RandomFiller(); + + dataFiller.Fill(dataContext); + + Assert.IsTrue(dataContext.Users.Count >= 5 && dataContext.Users.Count <= 11); + Assert.IsTrue(dataContext.Products.Count >= 5 && dataContext.Products.Count <= 11); + Assert.AreEqual(dataContext.Products.Count, dataContext.States.Count); + Assert.IsTrue(dataContext.Events.Count >= 5 && dataContext.Events.Count <= 11); + } }