Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Task 1 - 2 after classes fixes #8

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Library/DataLayer/API/IDataFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DataLayer.API
{
public interface IDataFiller
{
public List<IUser> GetGeneratedUsers();
public List<IProduct> GetGeneratedProducts();
public List<IEvent> GetGeneratedEvents();
public List<IState> GetGeneratedStates();
}
}
5 changes: 1 addition & 4 deletions Library/DataLayer/API/IDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ namespace DataLayer.API
{
public interface IDataRepository
{
static IDataRepository CreateDataRepository(IDataContext dataContext)
{
return new DataRepository(dataContext);
}
public void Seed(IDataFiller dataSeeder);

#region User
void AddUser(IUser user);
Expand Down
7 changes: 6 additions & 1 deletion Library/DataLayer/Implementations/DataContex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ namespace DataLayer.Implementations
{
public class DataContext : IDataContext
{
public static IDataContext NewInstance()
{
return new DataContext();
}

public List<IUser> Users { get; set; }
public List<IProduct> Products { get; set; }
public List<IEvent> Events { get; set; }
public List<IState> States { get; set; }

public DataContext()
private DataContext()
{
Users = new List<IUser>();
Products = new List<IProduct>();
Expand Down
29 changes: 26 additions & 3 deletions Library/DataLayer/Implementations/DataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@ public class DataRepository : IDataRepository
{
private IDataContext _dataContext;

// Init the data context
public DataRepository(IDataContext dataContext)
public static IDataRepository NewInstance(IDataContext dataContext)
{
return new DataRepository(dataContext);
}
private DataRepository(IDataContext dataContext)
{
_dataContext = dataContext;
}

public void Seed(IDataFiller dataSeeder)
{
foreach (IUser user in dataSeeder.GetGeneratedUsers())
{
AddUser(user);
}
foreach (IState state in dataSeeder.GetGeneratedStates())
{
AddState(state);
}
foreach (IProduct product in dataSeeder.GetGeneratedProducts())
{
AddProduct(product);
}
foreach (IEvent @event in dataSeeder.GetGeneratedEvents())
{
AddEvent(@event);
}
}

#region User
public void AddUser(IUser user)
{
Expand Down Expand Up @@ -48,7 +71,7 @@ public void UpdateUser(IUser updateUser)

userToBeUpdated = updateUser;
}

#endregion

#region Product
Expand Down
7 changes: 6 additions & 1 deletion Library/LogicLayer/Implementations/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace LogicLayer.Implementations
public class DataService : IDataService
{
private IDataRepository _dataRepository;
public DataService(IDataRepository dataRepository)
public static IDataService NewInstance(IDataRepository dataRepository)
{
return new DataService(dataRepository);
}
private DataService(IDataRepository dataRepository)
{
_dataRepository = dataRepository;
}

public void DeliverProduct(IUser user, IState state, int amount)
{

Expand Down
8 changes: 4 additions & 4 deletions Library/Tests/DataLayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void UserTests()
Assert.AreEqual(book1, user.ProductsDic[book1.Guid]);
Assert.AreEqual(book2, user.ProductsDic[book2.Guid]);

IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

dataRepository.AddUser(user);

Expand Down Expand Up @@ -85,7 +85,7 @@ public void BookTests()

Book book1 = new Book("Book1", 10.0, "Author1", "Publisher1", 100, new DateTime(2022, 1, 1));

IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

dataRepository.AddProduct(book);

Expand Down Expand Up @@ -113,7 +113,7 @@ public void StateTests()
[TestMethod]
public void DataContextTests()
{
DataContext dataContext = new DataContext();
IDataContext dataContext = DataContext.NewInstance();

Assert.IsNotNull(dataContext.Users);
Assert.IsNotNull(dataContext.Products);
Expand All @@ -124,7 +124,7 @@ public void DataContextTests()
[TestMethod]
public void DataRepositoryTests()
{
IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

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));
Expand Down
22 changes: 10 additions & 12 deletions Library/Tests/FillerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ public class FillerTests
[TestMethod]
public void PredefinedFillerTests()
{
IDataContext context = new DataContext();
IDataFiller filler = new PresetFiller();
IDataRepository repository = DataRepository.NewInstance(DataContext.NewInstance());

filler.Fill(context);
repository.Seed(new PresetFiller());

Assert.AreEqual(5, context.Users.Count);
Assert.AreEqual(5, context.Products.Count);
Assert.AreEqual(5, context.States.Count);
Assert.AreEqual(11, context.Events.Count);
Assert.AreEqual(5, repository.GetAllUsers().Count);
Assert.AreEqual(5, repository.GetAllProducts().Count);
Assert.AreEqual(5, repository.GetAllStates().Count);
Assert.AreEqual(11, repository.GetAllEvents().Count);
}

[TestMethod]
public void RandomFillerTests(){
IDataContext context = new DataContext();
IDataFiller filler = new RandomFiller();

filler.Fill(context);
public void RandomFillerTests()
{
IDataRepository repository = DataRepository.NewInstance(DataContext.NewInstance());
repository.Seed(new RandomFiller());

Assert.ThrowsException<ArgumentException>(() => {RandomFiller.GetRandomNumber<int>(0);});
Assert.AreEqual(15, RandomFiller.GetRandomString(15).Length);
Expand Down
12 changes: 6 additions & 6 deletions Library/Tests/LogicLayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LogicLayerTests
public void TestDeliverProduct_Success()
{
// Arrange
IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

var testUser1 = new User("User 1", "1", "[email protected]", 1000, 1234567890, null);

Expand All @@ -25,7 +25,7 @@ public void TestDeliverProduct_Success()
dataRepository.AddProduct(bookProduct1);
dataRepository.AddState(stateOfBookProduct1);

IDataService dataService = new DataService(dataRepository);
IDataService dataService = DataService.NewInstance(dataRepository);

// Act
const int amountOfBooksToDeliver = 5;
Expand All @@ -39,7 +39,7 @@ public void TestDeliverProduct_Success()
public void TestBorrowProduct_Success()
{
// Arrange
IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

var testUser1 = new User("User 1", "1", "[email protected]", 1000, 1234567890, null);

Expand All @@ -52,7 +52,7 @@ public void TestBorrowProduct_Success()
dataRepository.AddProduct(bookProduct1);
dataRepository.AddState(stateOfBookProduct1);

IDataService dataService = new DataService(dataRepository);
IDataService dataService = DataService.NewInstance(dataRepository);

// Act
dataService.BorrowProduct(testUser1, stateOfBookProduct1);
Expand All @@ -66,7 +66,7 @@ public void TestBorrowProduct_Success()
public void TestReturnProduct_Success()
{
// Arrange
IDataRepository dataRepository = IDataRepository.CreateDataRepository(new DataContext());
IDataRepository dataRepository = DataRepository.NewInstance(DataContext.NewInstance());

var testUser1 = new User("User 1", "1", "[email protected]", 1000, 1234567890, null);

Expand All @@ -79,7 +79,7 @@ public void TestReturnProduct_Success()
dataRepository.AddProduct(bookProduct1);
dataRepository.AddState(stateOfBookProduct1);

IDataService dataService = new DataService(dataRepository);
IDataService dataService = DataService.NewInstance(dataRepository);

dataService.BorrowProduct(testUser1, stateOfBookProduct1);

Expand Down
9 changes: 0 additions & 9 deletions Library/Tests/Seeders/IDataFiller.cs

This file was deleted.

79 changes: 51 additions & 28 deletions Library/Tests/Seeders/PresetFiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,55 @@

namespace Tests
{
public class PresetFiller : IDataFiller
internal class PresetFiller : IDataFiller
{
public void Fill(IDataContext context)
private List<IUser> GeneratedUsers;
private List<IProduct> GeneratedProducts;
private List<IEvent> GeneratedEvents;
private List<IState> GeneratedStates;

public PresetFiller()
{
GeneratedUsers = new List<IUser>();
GeneratedProducts = new List<IProduct>();
GeneratedEvents = new List<IEvent>();
GeneratedStates = new List<IState>();

var user1 = new User("John", "Doe", "johndoe@email", 5000, 123456789, null);
var user2 = new User("Jane", "Doe", "janedoe@email", 700, 111111111, null);
var user3 = new User("Alice", "Smith", "alicesmith@email", 800, 121212121, null);
var user4 = new User("Bob", "Smith", "bobsmith@email", 900, 987654321, null);
var user5 = new User("Charlie", "Brown", "charliebrown@email", 1000, 999999999, null);

context.Users.Add(user1);
context.Users.Add(user2);
context.Users.Add(user3);
context.Users.Add(user4);
context.Users.Add(user5);
GeneratedUsers.Add(user1);
GeneratedUsers.Add(user2);
GeneratedUsers.Add(user3);
GeneratedUsers.Add(user4);
GeneratedUsers.Add(user5);

var product1 = new Book("Old Man and the Sea", 10, "Ernest Hemingway", "Charles Sons", 300, new DateTime(1952, 1, 1));
var product2 = new Book("The Great Gatsby", 20, "F. Scott Fitzgerald", "Charles Sons", 400, new DateTime(1925, 1, 1));
var product3 = new Book("To Kill a Mockingbird", 30, "Harper Lee", "Charles Sons", 500, new DateTime(1960, 1, 1));
var product4 = new Book("1984", 40, "George Orwell", "Charles Sons", 600, new DateTime(1949, 1, 1));
var product5 = new Book("Brave New World", 50, "Aldous Huxley", "Charles Sons", 700, new DateTime(1932, 1, 1));

context.Products.Add(product1);
context.Products.Add(product2);
context.Products.Add(product3);
context.Products.Add(product4);
context.Products.Add(product5);
GeneratedProducts.Add(product1);
GeneratedProducts.Add(product2);
GeneratedProducts.Add(product3);
GeneratedProducts.Add(product4);
GeneratedProducts.Add(product5);

var state1 = new State(product1, 50);
var state2 = new State(product2, 40);
var state3 = new State(product3, 30);
var state4 = new State(product4, 20);
var state5 = new State(product5, 15);

context.States.Add(state1);
context.States.Add(state2);
context.States.Add(state3);
context.States.Add(state4);
context.States.Add(state5);
GeneratedStates.Add(state1);
GeneratedStates.Add(state2);
GeneratedStates.Add(state3);
GeneratedStates.Add(state4);
GeneratedStates.Add(state5);

var event1 = new Borrow(user1, state1);
var event2 = new Borrow(user1, state2);
Expand All @@ -52,26 +62,39 @@ public void Fill(IDataContext context)

var event6 = new Borrow(user2, state2);

context.Events.Add(event1);
context.Events.Add(event2);
context.Events.Add(event3);
context.Events.Add(event4);
context.Events.Add(event5);
context.Events.Add(event6);
GeneratedEvents.Add(event1);
GeneratedEvents.Add(event2);
GeneratedEvents.Add(event3);
GeneratedEvents.Add(event4);
GeneratedEvents.Add(event5);
GeneratedEvents.Add(event6);

var event7 = new Delivery(user3, state1, 25);
var event8 = new Delivery(user4, state4, 40);
var event9 = new Delivery(user5, state5, 40);

context.Events.Add(event7);
context.Events.Add(event8);
context.Events.Add(event9);
GeneratedEvents.Add(event7);
GeneratedEvents.Add(event8);
GeneratedEvents.Add(event9);

var event10 = new Return(user1, state1);
var event11 = new Return(user2, state2);

context.Events.Add(event10);
context.Events.Add(event11);
GeneratedEvents.Add(event10);
GeneratedEvents.Add(event11);
}

public List<IUser> GetGeneratedUsers(){
return GeneratedUsers;
}
public List<IProduct> GetGeneratedProducts(){
return GeneratedProducts;
}
public List<IEvent> GetGeneratedEvents(){
return GeneratedEvents;
}
public List<IState> GetGeneratedStates(){
return GeneratedStates;
}
}
}
Loading