Skip to content

Commit

Permalink
refactor: implement lazy relation mapping
Browse files Browse the repository at this point in the history
Signed-off-by: russkyc <[email protected]>
  • Loading branch information
russkyc committed Jun 11, 2023
1 parent 9ffee79 commit 2850b6b
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 84 deletions.
18 changes: 18 additions & 0 deletions Russkyc.GroomWise/Models/Entities/AppointmentCustomer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

namespace GroomWise.Models.Entities;

public class AppointmentCustomer
{
public int CustomerId { get; set; }
public int AppointmentId { get; set; }

[Navigate(nameof(CustomerId))]
public virtual Customer Customer { get; set; }

[Navigate(nameof(AppointmentId))]
public virtual Appointment Appointment { get; set; }
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/AppointmentEmployee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record AppointmentEmployee
{
public int AppointmentId { get; set; }
public int EmployeeId { get; set; }

[Navigate(nameof(AppointmentId))]
public virtual Appointment Appointment { get; set; }

[Navigate(nameof(EmployeeId))]
public virtual Employee Employee { get; set; }
}
10 changes: 8 additions & 2 deletions Russkyc.GroomWise/Models/Entities/AppointmentPet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

Expand All @@ -9,4 +9,10 @@ public record AppointmentPet
{
public int AppointmentId { get; set; }
public int PetId { get; set; }
}

[Navigate(nameof(AppointmentId))]
public virtual Appointment Appointment { get; set; }

[Navigate(nameof(PetId))]
public virtual Pet Pet { get; set; }
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/AppointmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record AppointmentService
{
public int ServiceId { get; set; }
public int AppointmentId { get; set; }

[Navigate(nameof(ServiceId))]
public virtual GroomingService GroomingService { get; set; }

[Navigate(nameof(AppointmentId))]
public virtual Appointment Appointment { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record AppointmentServiceProduct
{
public int ProductId { get; set; }
public int ServiceId { get; set; }

[Navigate(nameof(ProductId))]
public virtual Product Product { get; set; }

[Navigate(nameof(ServiceId))]
public virtual GroomingService GroomingService { get; set; }
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/CustomerAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record CustomerAddress
{
public int CustomerId { get; set; }
public int AddressId { get; set; }

[Navigate(nameof(CustomerId))]
public virtual Customer Customer { get; set; }

[Navigate(nameof(AddressId))]
public virtual Address Address { get; set; }
}
10 changes: 8 additions & 2 deletions Russkyc.GroomWise/Models/Entities/CustomerPet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

Expand All @@ -9,4 +9,10 @@ public record CustomerPet
{
public int PetId { get; set; }
public int OwnerId { get; set; }
}

[Navigate(nameof(OwnerId))]
public virtual Customer Owner { get; set; }

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/CustomerPet.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Owner' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/CustomerPet.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Owner' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Navigate(nameof(PetId))]
public virtual Pet Pet { get; set; }
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/EmployeeAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record EmployeeAccount
{
public int AccountId { get; set; }
public int EmployeeId { get; set; }

[Navigate(nameof(AccountId))]
public virtual Account Account { get; set; }

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeAccount.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Account' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeAccount.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Account' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Navigate(nameof(EmployeeId))]
public virtual Employee Employee { get; set; }

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeAccount.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeAccount.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/EmployeeAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record EmployeeAddress
{
public int AddressId { get; set; }
public int EmployeeId { get; set; }

[Navigate(nameof(AddressId))]
public virtual Address Address { get; set; }

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeAddress.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeAddress.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Navigate(nameof(EmployeeId))]
public virtual Employee Employee { get; set; }

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeAddress.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeAddress.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
6 changes: 6 additions & 0 deletions Russkyc.GroomWise/Models/Entities/EmployeeRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public record EmployeeRole
{
public int RoleId { get; set; }
public int EmployeeId { get; set; }

[Navigate(nameof(RoleId))]
public virtual Role Role { get; set; }

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeRole.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Role' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in Russkyc.GroomWise/Models/Entities/EmployeeRole.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Role' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Navigate(nameof(EmployeeId))]
public virtual Employee Employee { get; set; }

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeRole.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 17 in Russkyc.GroomWise/Models/Entities/EmployeeRole.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Employee' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
6 changes: 3 additions & 3 deletions Russkyc.GroomWise/Russkyc.GroomWise.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<PackageReference Include="CaseExtensions" Version="1.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="CredentialManagement" Version="1.0.2" />
<PackageReference Include="FreeSql" Version="3.2.697-preview20230530" />
<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.697-preview20230530" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.697-preview20230530" />
<PackageReference Include="FreeSql" Version="3.2.698-preview20230608" />
<PackageReference Include="FreeSql.Extensions.LazyLoading" Version="3.2.698-preview20230608" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.698-preview20230608" />
<PackageReference Include="ini.net" Version="1.1.0" />
<PackageReference Include="Material.Icons.WPF" Version="2.0.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
Expand Down
20 changes: 10 additions & 10 deletions Russkyc.GroomWise/Services/App/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void BuildNavItems()
return;

// Build Nav Items
Application.Current.Dispatcher.BeginInvoke(() => NavItems.Clear());
Application.Current.Dispatcher.BeginInvoke(
() =>
NavItems.AddRange(
NavItemsCollection
.Get()
.Where(navItem => navItem.Roles!.Any(navItemRole => navItemRole == role.Id))
.ToList()
)
);
Application.Current.Dispatcher.BeginInvoke(() =>
{
NavItems.Clear();
NavItems.AddRange(
NavItemsCollection
.Get()
.Where(navItem => navItem.Roles!.Any(navItemRole => role.Id == navItemRole))
.ToList()
);
});
}
}
63 changes: 17 additions & 46 deletions Russkyc.GroomWise/ViewModels/App/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ private void Login()
if (HasErrors)
return;

var account = _dbContext.AccountsRepository.Find(
a => a.Username == _encryptionService.Encrypt(Username!)
var employeeAccount = _dbContext.EmployeeAccountRepository.Find(
a => a.Account.Username == _encryptionService.Encrypt(Username!)
);

// Check if account is null
if (account is null)
if (employeeAccount is null)
{
BuilderServices.Resolve<ILoginView>().ClearFields();
ShowNotification("Account does not exist", NotificationType.Danger);
Expand All @@ -77,71 +77,39 @@ private void Login()
}

// Check if account password is equal to input password
if (_encryptionService.Hash(Password!) != account.Password)
if (employeeAccount.Account.Password != _encryptionService.Hash(Password!))
{
BuilderServices.Resolve<ILoginView>().ClearFields("Password");
ShowNotification("Password is incorrect", NotificationType.Danger);
_logger.Log(this, "Unsuccessful login attempt, wrong password");
return;
}

var employeeAccount = _dbContext.EmployeeAccountRepository.Find(
e => e.AccountId == account.Id
);

// Check if employee account is null
if (employeeAccount == null)
{
ShowNotification("Account does not match any employee record", NotificationType.Danger);
_logger.Log(
this,
"Unsuccessful login attempt, account does not match any employee record"
);
return;
}

var employee = _dbContext.EmployeeRepository.Find(
employee => employee.Id == employeeAccount.EmployeeId
);

// Check if employee is null
if (employee == null)
{
ShowNotification("Employee does not exist", NotificationType.Danger);
_logger.Log(this, "Unsuccessful login attempt, employee does not exist");
return;
}

var employeeRole = _dbContext.EmployeeRoleRepository.Find(
employeeRole => employeeRole.EmployeeId == employee.Id
employeeRole => employeeRole.Employee == employeeAccount.Employee
);

// Check if Employee role is null
if (employeeRole == null)
{
_logger.Log(this, "Employee is not assigned any role");
return;
}

var role = _dbContext.RoleRepository.Find(role => role.Id == employeeRole.RoleId);

// Check if role is null
if (role == null)
{
_logger.Log(this, "Role is non-existent");
return;
}


// Start Session
var session = _sessionFactory.Create(session =>
{
session.SessionUser = employee;
session.SessionRole = role;
session.SessionUser = employeeAccount.Employee;
session.SessionRole = employeeRole.Role;
});

_sessionManagerService.StartSession(session);
_applicationService.BuildNavItems();

RemoveNotification();
BuilderServices.Resolve<IMainView>().Show();
BuilderServices.Resolve<ILoginView>().Hide();
RemoveNotification();
}

[RelayCommand]
Expand All @@ -153,9 +121,12 @@ void RemoveNotification()
void ShowNotification(string description, NotificationType type)
{
var notification = new Notification { Description = description, Type = type };

if (Notifications.Count > 0)
{
Notifications[0] = notification;
else
Notifications.Add(notification);
return;
}
Notifications.Add(notification);
}
}
25 changes: 4 additions & 21 deletions Russkyc.GroomWise/ViewModels/Employees/EmployeesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,17 @@ void GetEmployees()
if (employeeAddress == null)
return;
var address = _dbContext.AddressRepository.Find(
address => address.Id == employeeAddress.AddressId
);
if (address == null)
return;
employeevm.Address =
$"{address.Barangay}, {address.City}, {address.Province}";
$"{employeeAddress.Address.Barangay}, {employeeAddress.Address.City}, {employeeAddress.Address.Province}";
var employeeRoles = _dbContext.EmployeeRoleRepository.FindAll(
var employeeRole = _dbContext.EmployeeRoleRepository.Find(
role => role.EmployeeId == employee.Id
);
if (employeeRoles == null)
return;
var roles = employeeRoles.Select(
employeeRole =>
_dbContext.RoleRepository.Find(
role => role.Id == employeeRole.RoleId
)
);
if (roles == null)
if (employeeRole == null)
return;
employeevm.Roles.AddRange(roles);
employeevm.Roles.Add(employeeRole.Role);
})
);
});
Expand Down

0 comments on commit 2850b6b

Please sign in to comment.