2021-04-03 19:03:56 +04:00
|
|
|
|
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>
|
|
|
|
|
/// Реализация IDisciplineBlockService
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DisciplineBlockService : IDisciplineBlockService
|
|
|
|
|
{
|
|
|
|
|
public OperationResultModel Create(DisciplineBlockSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var exsistEntity = context.DisciplineBlocks.FirstOrDefault(x => x.Title == model.Title);
|
|
|
|
|
if (exsistEntity == null)
|
|
|
|
|
{
|
|
|
|
|
var entity = Mapper.MapToClass<DisciplineBlockSetBindingModel, DisciplineBlock>(model, true);
|
|
|
|
|
context.DisciplineBlocks.Add(entity);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<DisciplineBlock, DisciplineBlockViewModel>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (exsistEntity.IsDeleted)
|
|
|
|
|
{
|
|
|
|
|
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
|
|
|
|
exsistEntity.IsDeleted = false;
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<DisciplineBlock, DisciplineBlockViewModel>(exsistEntity, true));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Delete(DisciplineBlockGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
2021-04-03 23:46:48 +04:00
|
|
|
|
var entity = context.DisciplineBlocks.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (entity == null)
|
2021-04-03 19:03:56 +04:00
|
|
|
|
{
|
2021-04-03 23:46:48 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
2021-04-03 19:03:56 +04:00
|
|
|
|
}
|
2021-04-03 23:46:48 +04:00
|
|
|
|
else if (entity.IsDeleted)
|
2021-04-03 19:03:56 +04:00
|
|
|
|
{
|
2021-04-03 23:46:48 +04:00
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент был удален", ResultServiceStatusCode.WasDelete);
|
2021-04-03 19:03:56 +04:00
|
|
|
|
}
|
2021-04-03 23:46:48 +04:00
|
|
|
|
if (context.Disciplines.Any(x => x.DisciplineBlockId == model.Id && !x.IsDeleted))
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Есть дисциплины, относящиеся к этому блоку", ResultServiceStatusCode.ExsistItem);
|
|
|
|
|
}
|
|
|
|
|
entity.IsDeleted = true;
|
|
|
|
|
entity.DateDelete = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
2021-04-03 19:03:56 +04:00
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Read(DisciplineBlockGetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
int countPages = 0;
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
// для одной записи
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var entity = context.DisciplineBlocks.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
|
|
|
if (entity == null)
|
|
|
|
|
{
|
|
|
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<DisciplineBlock, DisciplineBlockViewModel>(entity, model.HaveRight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = context.DisciplineBlocks.Where(x => !x.IsDeleted).AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(x => x.DisciplineBlockOrder);
|
|
|
|
|
|
|
|
|
|
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 DisciplineBlockListViewModel
|
|
|
|
|
{
|
|
|
|
|
MaxCount = countPages,
|
|
|
|
|
List = query.Select(x => Mapper.MapToClass<DisciplineBlock, DisciplineBlockViewModel>(x, model.HaveRight)).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return OperationResultModel.Success(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResultModel Update(DisciplineBlockSetBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
|
|
|
|
|
var entity = context.DisciplineBlocks.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<DisciplineBlock, DisciplineBlockViewModel>(entity, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|