useradd #3

Merged
ker73rus merged 2 commits from myprov into provider 2023-04-07 19:32:19 +04:00
10 changed files with 366 additions and 0 deletions

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class UserLogic : IUserLogic
{
private readonly IUserStorage _userStorage;
public UserLogic(IUserStorage userStorage)
{
_userStorage = userStorage;
}
public bool Create(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(UserBindingModel model)
{
CheckModel(model, false);
if (_userStorage.Delete(model) == null)
{
return false;
}
return true;
}
public UserViewModel? ReadElement(UserSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var user = _userStorage.GetElement(model);
if (user == null)
{
return null;
}
return user;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(UserBindingModel model)
{
CheckModel(model, false);
if (_userStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(UserBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина", nameof(model.Login));
}
var user = _userStorage.GetElement(new UserSearchModel{Login = model.Login});
if (user != null)
{
throw new InvalidOperationException("Пользователь с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityModels.Models;
namespace UniversityContracts.BindingModels
{
public class UserBindingModel : IUserModel
{
public int Id { get; set; }
public string Login { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public int RoleId { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicContracts
{
public interface IUserLogic
{
bool Create(UserBindingModel model);
bool Update(UserBindingModel model);
bool Delete(UserBindingModel model);
List<UserViewModel>? ReadList(UserSearchModel? model);
UserViewModel? ReadElement(UserSearchModel model);
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.SearchModels
{
public class UserSearchModel
{
public int? Id { get; set; }
public string? Login { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StoragesContracts
{
public interface IUserStorage
{
List<UserViewModel> GetFullList();
List<UserViewModel> GetFilteredList(UserSearchModel model);
UserViewModel? GetElement(UserSearchModel model);
UserViewModel? Insert(UserBindingModel model);
UserViewModel? Update(UserBindingModel model);
UserViewModel? Delete(UserBindingModel model);
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.ViewModels
{
public class UserViewModel
{
public int Id { get; set; }
public int RoleId { get; set; }
[DisplayName("Логин")]
public string Login { get; set; } = string.Empty;
[DisplayName("Пароль")]
public string Password { get; set; } = string.Empty;
}
}

View File

@ -18,6 +18,7 @@ namespace UniversityDataBaseImplemet
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<User> User { get; set; }
public virtual DbSet<Document> Documents { get; set; }
public virtual DbSet<EducationStatus> EducationStatuses { get; set; }
public virtual DbSet<Student> Students { get; set; }

View File

@ -0,0 +1,113 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet.Implements
{
public class UserStorage : IUserStorage
{
public UserViewModel? Delete(UserBindingModel model)
{
using var context = new Database();
var user = context.User
.Include(record => record.Login)
.Include(record => record.RoleId)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (user == null)
{
return null;
}
context.User.Remove(user);
context.SaveChanges();
return user.GetViewModel;
}
public UserViewModel? GetElement(UserSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.User
.Include(record => record.Login)
.Include(record => record.Id)
.FirstOrDefault(record => record.Id.Equals(model.Id))
?.GetViewModel;
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue)
{
return context.User
.Include(record => record.Login)
.Include(record => record.RoleId)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else return new();
}
public List<UserViewModel> GetFullList()
{
using var context = new Database();
return context.User
.Include(record => record.Login)
.Include(record => record.RoleId)
.Select(record => record.GetViewModel)
.ToList();
}
public UserViewModel? Insert(UserBindingModel model)
{
var newUser = User.Create(model);
if (newUser == null)
{
return null;
}
using var context = new Database();
context.User.Add(newUser);
context.SaveChanges();
return newUser.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var user = context.User
.Include(record => record.Login)
.Include(record => record.RoleId)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
transaction.Commit();
return user.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
namespace UniversityDataBaseImplemet.Models
{
public class User
{
public int Id { get; private set; }
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
public int RoleId { get; set; }
public static User Create(UserBindingModel model)
{
return new User()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
RoleId = model.RoleId
};
}
public void Update(UserBindingModel model)
{
Password = model.Password;
RoleId = model.RoleId;
}
public UserViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Password = Password,
RoleId = RoleId
};
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityModels.Models
{
public interface IUserModel
{
string Login { get; }
string Password { get; }
int RoleId { get; }
}
}