преподаватели реализация

This commit is contained in:
kotcheshir73 2021-04-03 23:46:48 +04:00
parent 054ad31744
commit d9321668a2
12 changed files with 759 additions and 65 deletions

View File

@ -11,6 +11,12 @@ namespace DepartmentBusinessLogic.BindingModels
public class LecturerGetBindingModel : GetBindingModel
{
public Guid? UserId { get; set; }
public Guid? LecturerPostId { get; set; }
public Guid? LecturerAcademicRankId { get; set; }
public Guid? LecturerAcademicDegreeId { get; set; }
}
/// <summary>

View File

@ -17,6 +17,12 @@ namespace DepartmentDatabaseImplementation
DependencyManager.Instance.RegisterType<IDisciplineBlockService, DisciplineBlockService>();
DependencyManager.Instance.RegisterType<IDisciplineService, DisciplineService>();
DependencyManager.Instance.RegisterType<ILecturerAcademicDegreeService, LecturerAcademicDegreeService>();
DependencyManager.Instance.RegisterType<ILecturerAcademicRankService, LecturerAcademicRankService>();
DependencyManager.Instance.RegisterType<ILecturerEmployeePostService, LecturerEmployeePostService>();
DependencyManager.Instance.RegisterType<ILecturerPostService, LecturerPostService>();
DependencyManager.Instance.RegisterType<ILecturerService, LecturerService>();
}
}
}

View File

@ -47,9 +47,6 @@ namespace DepartmentDatabaseImplementation.Implementations
public OperationResultModel Delete(DisciplineBlockGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.DisciplineBlocks.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
@ -59,8 +56,7 @@ namespace DepartmentDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
var disciplines = context.Disciplines.Where(x => x.DisciplineBlockId == model.Id);
if (disciplines.Any())
if (context.Disciplines.Any(x => x.DisciplineBlockId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть дисциплины, относящиеся к этому блоку", ResultServiceStatusCode.ExsistItem);
}
@ -69,14 +65,6 @@ namespace DepartmentDatabaseImplementation.Implementations
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
return OperationResultModel.Success(true);
}

View File

@ -47,9 +47,6 @@ namespace DepartmentDatabaseImplementation.Implementations
public OperationResultModel Delete(EmployeePostGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.EmployeePosts.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
@ -59,27 +56,19 @@ namespace DepartmentDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if (context.EmployeeEmployeePosts.Any(x => x.EmployeePostId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют сотрудники с этой должностью", ResultServiceStatusCode.ExsistItem);
}
if (context.LecturerEmployeePosts.Any(x => x.EmployeePostId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют преподаватели с этой должностью", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.EmployeeEmployeePosts.Where(x => x.EmployeePostId == model.Id);
foreach (var link in links)
{
link.IsDeleted = true;
link.DateDelete = DateTime.Now;
}
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
return OperationResultModel.Success(true);
}
@ -133,7 +122,7 @@ namespace DepartmentDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass<EmployeePostSetBindingModel, EmployeePost>(model, entity, true);
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();

View File

@ -3,6 +3,7 @@ using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
@ -51,7 +52,7 @@ namespace DepartmentDatabaseImplementation.Implementations
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.Employees.FirstOrDefault(x => x.Id == model.Id);
var entity = context.Employees.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
@ -60,13 +61,18 @@ namespace DepartmentDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
var classrooms = context.Classrooms.Where(x => x.EmployeeId == model.Id);
if (classrooms.Any())
else if (entity.User == null)
{
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
}
if (context.Classrooms.Any(x => x.EmployeeId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Есть аудитории, у которых этот сотрудник указан ответственным", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
@ -78,6 +84,7 @@ namespace DepartmentDatabaseImplementation.Implementations
}
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
@ -106,6 +113,10 @@ namespace DepartmentDatabaseImplementation.Implementations
}
var query = context.Employees.Where(x => !x.IsDeleted).AsQueryable();
if (model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
query = query.OrderBy(x => x.LastName);

View File

@ -0,0 +1,128 @@
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация ILecturerAcademicDegreeService
/// </summary>
public class LecturerAcademicDegreeService : ILecturerAcademicDegreeService
{
public OperationResultModel Create(LecturerAcademicDegreeSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.LecturerAcademicDegreeName == model.LecturerAcademicDegreeName);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<LecturerAcademicDegreeSetBindingModel, LecturerAcademicDegree>(model, true);
context.LecturerAcademicDegrees.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicDegree, LecturerAcademicDegreeViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicDegree, LecturerAcademicDegreeViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerAcademicDegreeGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if (context.Lecturers.Any(x => x.LecturerAcademicDegreeId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеются сотрудники, использующую эту запись", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerAcademicDegreeGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicDegree, LecturerAcademicDegreeViewModel>(entity, model.HaveRight));
}
var query = context.LecturerAcademicDegrees.Where(x => !x.IsDeleted).AsQueryable();
query = query.OrderBy(x => x.Order);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
var result = new LecturerAcademicDegreeListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<LecturerAcademicDegree, LecturerAcademicDegreeViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerAcademicDegreeSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerAcademicDegrees.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicDegree, LecturerAcademicDegreeViewModel>(entity, true));
}
}
}

View File

@ -0,0 +1,128 @@
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация ILecturerAcademicRankService
/// </summary>
public class LecturerAcademicRankService : ILecturerAcademicRankService
{
public OperationResultModel Create(LecturerAcademicRankSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.LecturerAcademicRanks.FirstOrDefault(x => x.LecturerAcademicRankName == model.LecturerAcademicRankName);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<LecturerAcademicRankSetBindingModel, LecturerAcademicRank>(model, true);
context.LecturerAcademicRanks.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicRank, LecturerAcademicRankViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicRank, LecturerAcademicRankViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerAcademicRankGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if (context.Lecturers.Any(x => x.LecturerAcademicRankId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеются сотрудники, использующую эту запись", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerAcademicRankGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicRank, LecturerAcademicRankViewModel>(entity, model.HaveRight));
}
var query = context.LecturerAcademicRanks.Where(x => !x.IsDeleted).AsQueryable();
query = query.OrderBy(x => x.Order);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
var result = new LecturerAcademicRankListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<LecturerAcademicRank, LecturerAcademicRankViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerAcademicRankSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerAcademicRanks.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerAcademicRank, LecturerAcademicRankViewModel>(entity, true));
}
}
}

View File

@ -0,0 +1,135 @@
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация ILecturerEmployeePostService
/// </summary>
public class LecturerEmployeePostService : ILecturerEmployeePostService
{
public OperationResultModel Create(LecturerEmployeePostSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.LecturerEmployeePosts.FirstOrDefault(x => x.LecturerId == model.LecturerId && x.EmployeePostId == model.EmployeePostId);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<LecturerEmployeePostSetBindingModel, LecturerEmployeePost>(model, true);
context.LecturerEmployeePosts.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerEmployeePost, LecturerEmployeePostViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerEmployeePost, LecturerEmployeePostViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerEmployeePostGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerEmployeePostGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<LecturerEmployeePost, LecturerEmployeePostViewModel>(entity, model.HaveRight));
}
var query = context.LecturerEmployeePosts.Where(x => !x.IsDeleted).AsQueryable();
if (model.EmployeePostId.HasValue)
{
query = query.Where(x => x.EmployeePostId == model.EmployeePostId);
}
if (model.LecturerId.HasValue)
{
query = query.Where(x => x.LecturerId == model.LecturerId);
}
query = query.OrderBy(x => x.Lecturer.LastName);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
query = query.Include(x => x.EmployeePost).Include(x => x.Lecturer);
var result = new LecturerEmployeePostListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<LecturerEmployeePost, LecturerEmployeePostViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerEmployeePostSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerEmployeePosts.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerEmployeePost, LecturerEmployeePostViewModel>(entity, true));
}
}
}

View File

@ -0,0 +1,128 @@
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация ILecturerPostService
/// </summary>
public class LecturerPostService : ILecturerPostService
{
public OperationResultModel Create(LecturerPostSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.LecturerPosts.FirstOrDefault(x => x.LecturerPostName == model.LecturerPostName);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<LecturerPostSetBindingModel, LecturerPost>(model, true);
context.LecturerPosts.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerPost, LecturerPostViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerPost, LecturerPostViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerPostGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if (context.Lecturers.Any(x => x.LecturerPostId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют преподаватели с этой должностью", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
context.SaveChanges();
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerPostGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<LecturerPost, LecturerPostViewModel>(entity, model.HaveRight));
}
var query = context.LecturerPosts.Where(x => !x.IsDeleted).AsQueryable();
query = query.OrderBy(x => x.Order);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
var result = new LecturerPostListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<LecturerPost, LecturerPostViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerPostSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.LecturerPosts.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<LecturerPost, LecturerPostViewModel>(entity, true));
}
}
}

View File

@ -0,0 +1,168 @@
using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
/// <summary>
/// Реализация ILecturerService
/// </summary>
public class LecturerService : ILecturerService
{
public OperationResultModel Create(LecturerSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var exsistEntity = context.Lecturers.FirstOrDefault(x => x.LastName == model.LastName && x.FirstName == model.FirstName &&
x.Patronymic == model.Patronymic);
if (exsistEntity == null)
{
var entity = Mapper.MapToClass<LecturerSetBindingModel, Lecturer>(model, true);
context.Lecturers.Add(entity);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Lecturer, LecturerViewModel>(entity, true));
}
else
{
if (exsistEntity.IsDeleted)
{
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
exsistEntity.IsDeleted = false;
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Lecturer, LecturerViewModel>(exsistEntity, true));
}
else
{
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
}
}
}
public OperationResultModel Delete(LecturerGetBindingModel model)
{
using var context = DatabaseManager.GetContext;
using var transaction = context.Database.BeginTransaction();
try
{
var entity = context.Lecturers.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
else if (entity.User == null)
{
return OperationResultModel.Error("Error:", "Не найден пользователь от сотрудника", ResultServiceStatusCode.NotFound);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
entity.User.IsDeleted = true;
entity.User.DateDelete = DateTime.Now;
context.SaveChanges();
var links = context.LecturerEmployeePosts.Where(x => x.LecturerId == model.Id);
foreach (var link in links)
{
link.IsDeleted = true;
link.DateDelete = DateTime.Now;
}
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
return OperationResultModel.Success(true);
}
public OperationResultModel Read(LecturerGetBindingModel model)
{
int countPages = 0;
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
{
var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id.Value);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
return OperationResultModel.Success(Mapper.MapToClass<Lecturer, LecturerViewModel>(entity, model.HaveRight));
}
var query = context.Lecturers.Where(x => !x.IsDeleted).AsQueryable();
if(model.UserId.HasValue)
{
query = query.Where(x => x.UserId == model.UserId);
}
if (model.LecturerAcademicDegreeId.HasValue)
{
query = query.Where(x => x.LecturerAcademicDegreeId == model.LecturerAcademicDegreeId);
}
if (model.LecturerAcademicRankId.HasValue)
{
query = query.Where(x => x.LecturerAcademicRankId == model.LecturerAcademicRankId);
}
if (model.LecturerPostId.HasValue)
{
query = query.Where(x => x.LecturerPostId == model.LecturerPostId);
}
query = query.OrderBy(x => x.LastName);
if (model.PageNumber.HasValue && model.PageSize.HasValue)
{
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
query = query
.Skip(model.PageSize.Value * model.PageNumber.Value)
.Take(model.PageSize.Value);
}
var result = new LecturerListViewModel
{
MaxCount = countPages,
List = query.Select(x => Mapper.MapToClass<Lecturer, LecturerViewModel>(x, model.HaveRight)).ToList()
};
return OperationResultModel.Success(result);
}
public OperationResultModel Update(LecturerSetBindingModel model)
{
using var context = DatabaseManager.GetContext;
var entity = context.Lecturers.FirstOrDefault(x => x.Id == model.Id);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
}
else if (entity.IsDeleted)
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
entity = Mapper.MapToClass(model, entity, true);
context.SaveChanges();
return OperationResultModel.Success(Mapper.MapToClass<Lecturer, LecturerViewModel>(entity, true));
}
}
}

View File

@ -59,6 +59,11 @@ namespace SecurityDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if(context.UserRoles.Any(x => x.RoleId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Существуют пользователи, у которых есть эта роль", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;
@ -72,14 +77,6 @@ namespace SecurityDatabaseImplementation.Implementations
}
context.SaveChanges();
var users = context.UserRoles.Where(x => x.RoleId == model.Id);
foreach (var u in users)
{
u.IsDeleted = true;
u.DateDelete = DateTime.Now;
}
context.SaveChanges();
transaction.Commit();
}
catch(Exception)

View File

@ -66,6 +66,16 @@ namespace SecurityDatabaseImplementation.Implementations
{
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
}
if (context.Employees.Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется сотрудник, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
if (context.Lecturers.Any(x => x.UserId == model.Id && !x.IsDeleted))
{
return OperationResultModel.Error("Error:", "Имеется преподаватель, приязанный к этой учетке", ResultServiceStatusCode.ExsistItem);
}
entity.IsDeleted = true;
entity.DateDelete = DateTime.Now;