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

Commit

Permalink
Merge pull request #12 from Oskarowski/dev-task-2
Browse files Browse the repository at this point in the history
Task-2 Changes after first class attempt
  • Loading branch information
Oskarowski committed Jun 10, 2024
2 parents 0d933b7 + df56de7 commit e457c8e
Show file tree
Hide file tree
Showing 44 changed files with 316 additions and 246 deletions.
235 changes: 234 additions & 1 deletion Library/DataLayer/Implementations/DataContex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,24 @@ public async Task AddUserAsync(IUser user)
}
}

public async Task UpdateUserAsync(IUser user)
public async Task<IUser?> GetUserQueryAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.User? user = await Task.Run(() =>
{
var query = from u in context.User
where u.guid == guid
select u;
return query.FirstOrDefault();
});

return user is not null ? new User(user.guid, user.firstName, user.lastName, user.email, user.balance, user.phoneNumber) : null;
}
}

public async Task UpdateUserAsync(IUser user)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Expand Down Expand Up @@ -93,6 +110,18 @@ public async Task DeleteUserAsync(string guid)
}
}

public async Task DeleteUserMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.User toDelete = context.User.FirstOrDefault(u => u.guid == guid)!;

context.User.DeleteOnSubmit(toDelete);

await Task.Run(() => context.SubmitChanges());
}
}

public async Task<Dictionary<string, IUser>> GetAllUsersAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -105,6 +134,17 @@ public async Task DeleteUserAsync(string guid)
}
}

public async Task<Dictionary<string, IUser>> GetAllUsersMethodAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
IQueryable<IUser> usersQuery = context.User
.Select(u => new User(u.guid, u.firstName, u.lastName, u.email, u.balance, u.phoneNumber) as IUser);

return await Task.Run(() => usersQuery.ToDictionary(k => k.Guid));
}
}

public async Task<int> GetUsersCountAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -113,6 +153,17 @@ public async Task<int> GetUsersCountAsync()
}
}

public async Task<int> GetUsersCountQueryAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
var userCountQuery = from u in context.User
select u;

return await Task.Run(() => userCountQuery.Count());
}
}

#endregion

#region Product CRUD
Expand Down Expand Up @@ -157,6 +208,19 @@ public async Task AddProductAsync(IBook product)
}
}

public async Task<IBook?> GetProductMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Book? product = await Task.Run(() =>
{
return context.Book.FirstOrDefault(p => p.guid == guid);
});

return product is not null ? new Book(product.guid, product.name, (double)product.price, product.author, product.publisher, product.pages, product.publicationDate) : null;
}
}

public async Task UpdateProductAsync(IBook product)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -176,6 +240,26 @@ public async Task UpdateProductAsync(IBook product)
}
}

public async Task UpdateProductMethodAsync(IBook product)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Book toUpdate = context.Book.FirstOrDefault(p => p.guid == product.Guid)!;

if (toUpdate != null)
{
toUpdate.name = product.Name;
toUpdate.price = product.Price;
toUpdate.author = product.Author;
toUpdate.publisher = product.Publisher;
toUpdate.pages = product.Pages;
toUpdate.publicationDate = product.PublicationDate;

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task DeleteProductAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -188,6 +272,21 @@ public async Task DeleteProductAsync(string guid)
}
}

public async Task DeleteProductMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Book toDelete = context.Book.FirstOrDefault(p => p.guid == guid)!;

if (toDelete != null)
{
context.Book.DeleteOnSubmit(toDelete);

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task<Dictionary<string, IBook>> GetAllProductsAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -200,6 +299,17 @@ public async Task DeleteProductAsync(string guid)
}
}

public async Task<Dictionary<string, IBook>> GetAllProductsMethodAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
IQueryable<IBook> productQuery = context.Book
.Select(p => new Book(p.guid, p.name, (double)p.price, p.author, p.publisher, p.pages, p.publicationDate) as IBook);

return await Task.Run(() => productQuery.ToDictionary(k => k.Guid));
}
}

public async Task<int> GetProductsCountAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand Down Expand Up @@ -248,6 +358,19 @@ public async Task AddStateAsync(IState state)
}
}

public async Task<IState?> GetStateMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.State? state = await Task.Run(() =>
{
return context.State.FirstOrDefault(s => s.guid == guid);
});

return state is not null ? new State(state.guid, state.productGuid, state.quantity) : null;
}
}

public async Task UpdateStateAsync(IState state)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -261,6 +384,22 @@ public async Task UpdateStateAsync(IState state)
}
}

public async Task UpdateStateMethodAsync(IState state)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.State toUpdate = context.State.FirstOrDefault(s => s.guid == state.Guid)!;

if (toUpdate != null)
{
toUpdate.productGuid = state.ProductGuid;
toUpdate.quantity = state.Quantity;

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task DeleteStateAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -273,6 +412,21 @@ public async Task DeleteStateAsync(string guid)
}
}

public async Task DeleteStateMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.State toDelete = context.State.FirstOrDefault(s => s.guid == guid)!;

if (toDelete != null)
{
context.State.DeleteOnSubmit(toDelete);

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task<Dictionary<string, IState>> GetAllStatesAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -285,6 +439,17 @@ public async Task DeleteStateAsync(string guid)
}
}

public async Task<Dictionary<string, IState>> GetAllStatesMethodAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
IQueryable<IState> stateQuery = context.State
.Select(s => new State(s.guid, s.productGuid, s.quantity) as IState);

return await Task.Run(() => stateQuery.ToDictionary(k => k.Guid));
}
}

public async Task<int> GetStatesCountAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand Down Expand Up @@ -336,6 +501,19 @@ public async Task AddEventAsync(IEvent even)

}

public async Task<IEvent?> GetEventMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Event? even = await Task.Run(() =>
{
return context.Event.FirstOrDefault(e => e.guid == guid);
});

return even is not null ? new Event(even.guid, even.stateGuid, even.userGuid, even.createdAt, even.type) : null;
}
}

public async Task UpdateEventAsync(IEvent even)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -351,6 +529,24 @@ public async Task UpdateEventAsync(IEvent even)
}
}

public async Task UpdateEventMethodAsync(IEvent even)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Event toUpdate = context.Event.FirstOrDefault(e => e.guid == even.Guid)!;

if (toUpdate != null)
{
toUpdate.stateGuid = even.StateGuid;
toUpdate.userGuid = even.UserGuid;
toUpdate.createdAt = even.CreatedAt;
toUpdate.type = even.Type;

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task DeleteEventAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -363,6 +559,21 @@ public async Task DeleteEventAsync(string guid)
}
}

public async Task DeleteEventMethodAsync(string guid)
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
Database.Event toDelete = context.Event.FirstOrDefault(e => e.guid == guid)!;

if (toDelete != null)
{
context.Event.DeleteOnSubmit(toDelete);

await Task.Run(() => context.SubmitChanges());
}
}
}

public async Task<Dictionary<string, IEvent>> GetAllEventsAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -375,6 +586,17 @@ public async Task DeleteEventAsync(string guid)
}
}

public async Task<Dictionary<string, IEvent>> GetAllEventsMethodAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
IQueryable<IEvent> eventQuery = context.Event
.Select(e => new Event(e.guid, e.stateGuid, e.userGuid, e.createdAt, e.type) as IEvent);

return await Task.Run(() => eventQuery.ToDictionary(k => k.Guid));
}
}

public async Task<int> GetEventsCountAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
Expand All @@ -383,6 +605,17 @@ public async Task<int> GetEventsCountAsync()
}
}

public async Task<int> GetEventsCountQueryAsync()
{
using (LibraryDataClassesDataContext context = new LibraryDataClassesDataContext(_connectionString))
{
var eventCountQuery = from e in context.Event
select e;

return await Task.Run(() => eventCountQuery.Count());
}
}

#endregion


Expand Down
11 changes: 0 additions & 11 deletions Library/Presentation/IErrorInformer.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Library/Presentation/Model/API/IEventModelOperation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Presentation.Model.Implementation;
using Service.API;
using Service.API;

namespace Presentation.Model.API
{
Expand Down
3 changes: 1 addition & 2 deletions Library/Presentation/Model/API/IProductModelOperation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Presentation.Model.Implementation;
using Service.API;
using Service.API;

namespace Presentation.Model.API
{
Expand Down
3 changes: 1 addition & 2 deletions Library/Presentation/Model/API/IStateModelOperation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Presentation.Model.Implementation;
using Service.API;
using Service.API;

namespace Presentation.Model.API
{
Expand Down
4 changes: 1 addition & 3 deletions Library/Presentation/Model/API/IUserModelOperation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Presentation.Model.Implementation;
using Service.API;
using Service.Implementation;
using Service.API;

namespace Presentation.Model.API
{
Expand Down
Loading

0 comments on commit e457c8e

Please sign in to comment.