This commit is contained in:
MaxKarme 2023-04-08 17:38:34 +04:00
parent 7a7a1f2ed3
commit 92cfe7ad61
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,78 @@
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.Implements
{
internal class UserStorage : IUserStorage
{
public List<UserViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Users.Select(x => x.GetViewModel).ToList();
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
using var context = new UniversityDatabase();
return context.Users
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(string.IsNullOrEmpty(model.Login) || x.Login.Contains(model.Login)) &&
(string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))
)
)
.Select(x => x.GetViewModel)
.ToList();
}
public UserViewModel? GetElement(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Users
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(!string.IsNullOrEmpty(model.Password) && x.Password == model.Password) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public UserViewModel? Insert(UserBindingModel model)
{
var newUser = User.Create(model);
if (newUser == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Users.Add(newUser);
context.SaveChanges();
return newUser.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
using var context = new UniversityDatabase();
var user = context.Users.FirstOrDefault(x => x.Id == model.Id);
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetViewModel;
}
public UserViewModel? Delete(UserBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Users.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,62 @@
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace UniversityDatabaseImplement.Models
{
public class User : IUserModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Surname { get; set; } = string.Empty;
[Required]
public string PhoneNumber { get; set; } = string.Empty;
[Required]
public string Position { get; set; } = string.Empty;
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
public static User Create(UserBindingModel model)
{
return new User
{
Id = model.Id,
Name = model.Name,
Surname = model.Surname,
PhoneNumber = model.PhoneNumber,
Position = model.Position,
Login = model.Login,
Password = model.Password
};
}
public void Update(UserBindingModel model)
{
if (model == null) return;
Id = model.Id;
Name = model.Name;
Surname = model.Surname;
PhoneNumber = model.PhoneNumber;
Position = model.Position;
Login = model.Login;
Password = model.Password;
}
public UserViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Surname = Surname,
PhoneNumber = PhoneNumber,
Position = Position,
Login = Login,
Password = Password
};
}
}

View File

@ -23,5 +23,6 @@ namespace UniversityDatabaseImplement
public virtual DbSet<StatementStudent> StatementStudents { set; get; }
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<StudentExaminationResult> StudentExaminationResults { set; get; }
public virtual DbSet<User> Users { set; get; }
}
}