Guarantor: Shifted from user model to guarantor model + all implementations.
This commit is contained in:
parent
e0a3c1e1fd
commit
42790d8e3b
122
ComputerStoreBusinessLogic/BusinessLogic/EmployeeLogic.cs
Normal file
122
ComputerStoreBusinessLogic/BusinessLogic/EmployeeLogic.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using ComputerStoreContracts.BindingModels;
|
||||
using ComputerStoreContracts.BusinessLogicContracts;
|
||||
using ComputerStoreContracts.SearchModels;
|
||||
using ComputerStoreContracts.StorageContracts;
|
||||
using ComputerStoreContracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreBusinessLogic.BusinessLogic
|
||||
{
|
||||
public class EmployeeLogic : IEmployeeLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IEmployeeStorage _employeeStorage;
|
||||
public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage employeeStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_employeeStorage = employeeStorage;
|
||||
}
|
||||
public bool Create(EmployeeBindingModel model)
|
||||
{
|
||||
CheckUser(model);
|
||||
if (_employeeStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Update(EmployeeBindingModel model)
|
||||
{
|
||||
CheckUser(model);
|
||||
if (_employeeStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(EmployeeBindingModel model)
|
||||
{
|
||||
CheckUser(model, false);
|
||||
_logger.LogInformation("Delete. ID:{ID}", model.ID);
|
||||
if (_employeeStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public EmployeeViewModel? ReadElement(EmployeeSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. Username:{Username}.ID:{ ID}", model.Username, model.ID);
|
||||
var element = _employeeStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
_logger.LogWarning("ReadElement element not found");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
|
||||
return element;
|
||||
}
|
||||
|
||||
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. Username:{Username}. ID:{ ID}", model?.Username, model?.ID);
|
||||
|
||||
var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model);
|
||||
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
|
||||
public void CheckUser(EmployeeBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Username))
|
||||
{
|
||||
throw new ArgumentNullException("Invalid username of user", nameof(model.Username));
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Password))
|
||||
{
|
||||
throw new ArgumentNullException("Invalid password of user", nameof(model.Password));
|
||||
}
|
||||
|
||||
_logger.LogInformation("Client. Email:{ Email}. ID: { ID} ", model.Username, model.ID);
|
||||
|
||||
var element = _employeeStorage.GetElement(new EmployeeSearchModel
|
||||
{
|
||||
Username = model.Username
|
||||
});
|
||||
|
||||
if (element != null && element.ID != model.ID)
|
||||
{
|
||||
throw new InvalidOperationException("Employee with such username already exists.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using ComputerStoreDataModels.Enums;
|
||||
using ComputerStoreDataModels.Models;
|
||||
using ComputerStoreDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -8,7 +7,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.BindingModels
|
||||
{
|
||||
public class UserBindingModel : IUserModel
|
||||
public class EmployeeBindingModel : IEmployeeModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string Username { get; set; } = string.Empty;
|
||||
@ -16,6 +15,5 @@ namespace ComputerStoreContracts.BindingModels
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public string MiddleName { get; set; } = string.Empty;
|
||||
public Role Role { get; set; } = Role.Unknown;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using ComputerStoreContracts.BindingModels;
|
||||
using ComputerStoreContracts.SearchModels;
|
||||
using ComputerStoreContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.BusinessLogicContracts
|
||||
{
|
||||
public interface IEmployeeLogic
|
||||
{
|
||||
List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model);
|
||||
EmployeeViewModel? ReadElement(EmployeeSearchModel model);
|
||||
bool Create(EmployeeBindingModel model);
|
||||
bool Update(EmployeeBindingModel model);
|
||||
bool Delete(EmployeeBindingModel model);
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using ComputerStoreDataModels.Enums;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -7,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.SearchModels
|
||||
{
|
||||
public class UserSearchModel
|
||||
public class EmployeeSearchModel
|
||||
{
|
||||
public int? ID { get; set; }
|
||||
public string? Username { get; set; }
|
21
ComputerStoreContracts/StorageContracts/IEmployeeStorage.cs
Normal file
21
ComputerStoreContracts/StorageContracts/IEmployeeStorage.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using ComputerStoreContracts.BindingModels;
|
||||
using ComputerStoreContracts.SearchModels;
|
||||
using ComputerStoreContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.StorageContracts
|
||||
{
|
||||
public interface IEmployeeStorage
|
||||
{
|
||||
List<EmployeeViewModel> GetFullList();
|
||||
List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model);
|
||||
EmployeeViewModel? GetElement(EmployeeSearchModel model);
|
||||
EmployeeViewModel? Insert(EmployeeBindingModel model);
|
||||
EmployeeViewModel? Update(EmployeeBindingModel model);
|
||||
EmployeeViewModel? Delete(EmployeeBindingModel model);
|
||||
}
|
||||
}
|
31
ComputerStoreContracts/ViewModels/EmployeeViewModel.cs
Normal file
31
ComputerStoreContracts/ViewModels/EmployeeViewModel.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using ComputerStoreDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.ViewModels
|
||||
{
|
||||
public class EmployeeViewModel : IEmployeeModel
|
||||
{
|
||||
public int ID { get; set; }
|
||||
|
||||
[DisplayName("Employee's login")]
|
||||
public string Username { get; set; } = string.Empty;
|
||||
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Employee's first name")]
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Employee's last name")]
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Employee's middle name")]
|
||||
public string MiddleName { get; set; } = string.Empty;
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreContracts.ViewModels
|
||||
{
|
||||
internal class UserViewModel
|
||||
{
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreDataModels.Enums
|
||||
{
|
||||
public enum Role
|
||||
{
|
||||
Unknown = -1,
|
||||
Guarantor = 0,
|
||||
Cotnractor = 1
|
||||
}
|
||||
}
|
17
ComputerStoreDataModels/Models/IEmployeeModel.cs
Normal file
17
ComputerStoreDataModels/Models/IEmployeeModel.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreDataModels.Models
|
||||
{
|
||||
public interface IEmployeeModel : IID
|
||||
{
|
||||
string Username { get; }
|
||||
string Password { get; }
|
||||
string FirstName { get; }
|
||||
string LastName { get; }
|
||||
string MiddleName { get; }
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
using ComputerStoreDataModels.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerStoreDataModels.Models
|
||||
{
|
||||
public interface IUserModel : IID
|
||||
{
|
||||
string Username { get; }
|
||||
string Password { get; }
|
||||
string FirstName { get; }
|
||||
string LastName { get; }
|
||||
string MiddleName { get; }
|
||||
Role Role { get; }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user