Добавил роль
This commit is contained in:
parent
13ed86ece8
commit
7d9eedb01b
100
UniversityBusinessLogic/BusinessLogics/RoleLogic.cs
Normal file
100
UniversityBusinessLogic/BusinessLogics/RoleLogic.cs
Normal file
@ -0,0 +1,100 @@
|
||||
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 RoleLogic : IRoleLogic
|
||||
{
|
||||
private readonly IRoleStorage _roleStorage;
|
||||
public RoleLogic(IRoleStorage roleStorage)
|
||||
{
|
||||
_roleStorage = roleStorage;
|
||||
}
|
||||
public bool Create(RoleBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_roleStorage.Insert(model) == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(RoleBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
if (_roleStorage.Delete(model) == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public RoleViewModel? ReadElement(RoleSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
var element = _roleStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
|
||||
{
|
||||
var list = model == null ? _roleStorage.GetFullList() : _roleStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool Update(RoleBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_roleStorage.Update(model) == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CheckModel(RoleBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия роли", nameof(model.Name));
|
||||
}
|
||||
|
||||
var element = _roleStorage.GetElement(new RoleSearchModel
|
||||
{
|
||||
Name = model.Name
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Роль с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
UniversityContracts/BindingModels/RoleBindingModel.cs
Normal file
15
UniversityContracts/BindingModels/RoleBindingModel.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityModels.Models;
|
||||
|
||||
namespace UniversityContracts.BindingModels
|
||||
{
|
||||
public class RoleBindingModel : IRoleModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
20
UniversityContracts/BusinessLogicContracts/IRoleLogic.cs
Normal file
20
UniversityContracts/BusinessLogicContracts/IRoleLogic.cs
Normal 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 IRoleLogic
|
||||
{
|
||||
List<RoleViewModel>? ReadList(RoleSearchModel? model);
|
||||
RoleViewModel? ReadElement(RoleSearchModel model);
|
||||
bool Create(RoleBindingModel model);
|
||||
bool Update(RoleBindingModel model);
|
||||
bool Delete(RoleBindingModel model);
|
||||
}
|
||||
}
|
14
UniversityContracts/SearchModels/RoleSearchModel.cs
Normal file
14
UniversityContracts/SearchModels/RoleSearchModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityContracts.SearchModels
|
||||
{
|
||||
public class RoleSearchModel
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public int? Id { get; set; }
|
||||
}
|
||||
}
|
@ -13,5 +13,6 @@ namespace UniversityContracts.SearchModels
|
||||
public int? UserId { get; set; }
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public bool? Disciplines { get; set; }
|
||||
}
|
||||
}
|
||||
|
21
UniversityContracts/StoragesContracts/IRoleStorage.cs
Normal file
21
UniversityContracts/StoragesContracts/IRoleStorage.cs
Normal 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 IRoleStorage
|
||||
{
|
||||
List<RoleViewModel> GetFullList();
|
||||
List<RoleViewModel> GetFilteredList(RoleSearchModel model);
|
||||
RoleViewModel? GetElement(RoleSearchModel model);
|
||||
RoleViewModel? Insert(RoleBindingModel model);
|
||||
RoleViewModel? Update(RoleBindingModel model);
|
||||
RoleViewModel? Delete(RoleBindingModel model);
|
||||
}
|
||||
}
|
18
UniversityContracts/ViewModels/RoleViewModel.cs
Normal file
18
UniversityContracts/ViewModels/RoleViewModel.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityModels.Models;
|
||||
|
||||
namespace UniversityContracts.ViewModels
|
||||
{
|
||||
public class RoleViewModel : IRoleModel
|
||||
{
|
||||
[DisplayName("Название")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
@ -22,5 +22,6 @@ namespace UniversityDataBaseImplemet
|
||||
public virtual DbSet<EducationStatus> EducationStatuses { get; set; }
|
||||
public virtual DbSet<Student> Students { get; set; }
|
||||
public virtual DbSet<StudentDocument> StudentDocuments { get; set; }
|
||||
public virtual DbSet<Role> Roles { get; set; }
|
||||
}
|
||||
}
|
||||
|
89
UniversityDataBaseImplemet/Implements/RoleStorage.cs
Normal file
89
UniversityDataBaseImplemet/Implements/RoleStorage.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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 RoleStorage : IRoleStorage
|
||||
{
|
||||
public RoleViewModel? Delete(RoleBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var element = context.Roles
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Roles.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RoleViewModel? GetElement(RoleSearchModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
if (model.Id.HasValue)
|
||||
return context.Roles
|
||||
.FirstOrDefault(record => record.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
if (!string.IsNullOrEmpty(model.Name))
|
||||
return context.Roles
|
||||
.FirstOrDefault(record => record.Name.Equals(model.Name))
|
||||
?.GetViewModel;
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<RoleViewModel> GetFilteredList(RoleSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new Database();
|
||||
return context.Roles
|
||||
.Where(record => record.Name.Contains(model.Name))
|
||||
.Select(record => record.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<RoleViewModel> GetFullList()
|
||||
{
|
||||
using var context = new Database();
|
||||
return context.Roles.Select(record => record.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public RoleViewModel? Insert(RoleBindingModel model)
|
||||
{
|
||||
var newRole = Role.Create(model);
|
||||
if (newRole == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new Database();
|
||||
context.Roles.Add(newRole);
|
||||
context.SaveChanges();
|
||||
return newRole.GetViewModel;
|
||||
}
|
||||
|
||||
public RoleViewModel? Update(RoleBindingModel model)
|
||||
{
|
||||
using var context = new Database();
|
||||
var client = context.Roles.FirstOrDefault(record => record.Id == model.Id);
|
||||
if (client == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
client.Update(model);
|
||||
context.SaveChanges();
|
||||
return client.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -51,28 +51,38 @@ namespace UniversityDataBaseImplemet.Implements
|
||||
.Select(record => record.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
/*else if (model.Disciplines == true) // для отчета#1 Поставщик
|
||||
{
|
||||
return context.Students
|
||||
.Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream })
|
||||
.Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Stream = stream })
|
||||
.Join(context.Disciplines, record => record.stream.Id, discipline => discipline.StreamId, (record, discipline) => new { record.student, discipline })
|
||||
.GroupBy(sd => sd.student)
|
||||
.Select(g => new {
|
||||
Student = g.Key.Name,
|
||||
Disciplines = g.Select(sd => sd.discipline.Name).ToList()
|
||||
})
|
||||
.ToList();
|
||||
}*/
|
||||
/*else if (model.DateFrom != null && model.DateTo != null) // для отчета#2 Поставщик
|
||||
{
|
||||
return context.Students
|
||||
.Include(record => record.User)
|
||||
.Include(record => record.EducationStatus)
|
||||
.Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream.StreamId })
|
||||
.Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Student = studentStream.Student, Stream = stream })
|
||||
.GroupBy(record => record.Stream)
|
||||
.Join(context.StudentStreams,
|
||||
student => student.Id,
|
||||
studentStream => studentStream.StudentId,
|
||||
(student, studentStream) => new { Student = student, StreamId = studentStream })
|
||||
.Join(context.Streams,
|
||||
studentStream => studentStream.StreamId,
|
||||
stream => stream.Id,
|
||||
(studentStream, stream) => new { Stream = stream })
|
||||
.Select(result => new
|
||||
{
|
||||
Stream = result.Key,
|
||||
Stream = result.Stream,
|
||||
Students = result.Select(record => (record.Student, record.EducationStatus)).ToList()
|
||||
})
|
||||
.ToList();
|
||||
var result = context.Students
|
||||
.Include(student => student.User)
|
||||
.Include(student => student.StudentStreams)
|
||||
.ThenInclude(studentStream => studentStream.Stream)
|
||||
.ThenInclude(stream => stream.Disciplines)
|
||||
.SelectMany(student => student.StudentStreams, (student, studentStream) => new { Student = student, Stream = studentStream.Stream })
|
||||
.Select(record => new { StudentName = record.Student.User.Name + " " + record.Student.User.Surname, Discipline = record.Stream.Disciplines })
|
||||
.ToList();
|
||||
.ToList();
|
||||
}*/
|
||||
else
|
||||
{
|
||||
|
58
UniversityDataBaseImplemet/Models/Role.cs
Normal file
58
UniversityDataBaseImplemet/Models/Role.cs
Normal file
@ -0,0 +1,58 @@
|
||||
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;
|
||||
using UniversityModels.Models;
|
||||
|
||||
namespace UniversityDataBaseImplemet.Models
|
||||
{
|
||||
public class Role : IRoleModel
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public int Id { get; set; }
|
||||
|
||||
[ForeignKey("RoleId")]
|
||||
public virtual List<User> Users { get; set; } = new();
|
||||
|
||||
public static Role? Create(RoleBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Role()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name
|
||||
};
|
||||
}
|
||||
public static Role Create(RoleViewModel model)
|
||||
{
|
||||
return new Role
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name
|
||||
};
|
||||
}
|
||||
public void Update(RoleBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
|
||||
}
|
||||
public RoleViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name
|
||||
};
|
||||
}
|
||||
}
|
13
UniversityModels/Models/IRoleModel.cs
Normal file
13
UniversityModels/Models/IRoleModel.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityModels.Models
|
||||
{
|
||||
public interface IRoleModel : IId
|
||||
{
|
||||
string Name { get; }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user