DepartmentProject/DepartmentPortal/Department/DepartmentDatabaseImplementation.csproj/Implementations/LecturerAcademicRankService.cs

128 lines
4.5 KiB
C#
Raw Normal View History

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));
}
}
}