137 lines
4.4 KiB
C#
137 lines
4.4 KiB
C#
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.Extensions;
|
|
using ModuleTools.Models;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace DepartmentDatabaseImplementation.Implementations
|
|
{
|
|
/// <summary>
|
|
/// Реализация IDisciplineService
|
|
/// </summary>
|
|
public class DisciplineService : IDisciplineService
|
|
{
|
|
public OperationResultModel Create(DisciplineSetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var exsistEntity = context.Disciplines.FirstOrDefault(x => x.DisciplineName == model.DisciplineName);
|
|
if (exsistEntity == null)
|
|
{
|
|
var entity = Mapper.MapToClass<DisciplineSetBindingModel, Discipline>(model, true);
|
|
context.Disciplines.Add(entity);
|
|
context.SaveChanges();
|
|
return OperationResultModel.Success(Mapper.MapToClass<Discipline, DisciplineViewModel>(entity, true));
|
|
}
|
|
else
|
|
{
|
|
if (exsistEntity.IsDeleted)
|
|
{
|
|
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
|
exsistEntity.IsDeleted = false;
|
|
context.SaveChanges();
|
|
return OperationResultModel.Success(Mapper.MapToClass<Discipline, DisciplineViewModel>(exsistEntity, true));
|
|
}
|
|
else
|
|
{
|
|
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
|
}
|
|
}
|
|
}
|
|
|
|
public OperationResultModel Delete(DisciplineGetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var entity = context.Disciplines.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(DisciplineGetBindingModel model)
|
|
{
|
|
int countPages = 0;
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
// для одной записи
|
|
if (model.Id.HasValue)
|
|
{
|
|
var entity = context.Disciplines.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
if (entity == null)
|
|
{
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
}
|
|
return OperationResultModel.Success(Mapper.MapToClass<Discipline, DisciplineViewModel>(entity, model.HaveRight));
|
|
}
|
|
|
|
var query = context.Disciplines.Where(x => !x.IsDeleted).AsQueryable();
|
|
if (model.DisciplineName.IsNotEmpty())
|
|
{
|
|
query = query.Where(x => x.DisciplineName.Contains(model.DisciplineName));
|
|
}
|
|
if (model.DisciplineBlockId.HasValue)
|
|
{
|
|
query = query.Where(x => x.DisciplineBlockId == model.DisciplineBlockId.Value);
|
|
}
|
|
|
|
query = query.OrderBy(x => x.DisciplineBlock.DisciplineBlockOrder).ThenBy(x => x.DisciplineName);
|
|
|
|
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.DisciplineBlock);
|
|
|
|
var result = new DisciplineListViewModel
|
|
{
|
|
MaxCount = countPages,
|
|
List = query.Select(x => Mapper.MapToClass<Discipline, DisciplineViewModel>(x, model.HaveRight)).ToList()
|
|
};
|
|
|
|
return OperationResultModel.Success(result);
|
|
}
|
|
|
|
public OperationResultModel Update(DisciplineSetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var entity = context.Disciplines.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<Discipline, DisciplineViewModel>(entity, true));
|
|
}
|
|
}
|
|
} |