138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
using DatabaseCore;
|
||
using DatabaseCore.Models.Department;
|
||
using DepartmentBusinessLogic.BindingModels;
|
||
using DepartmentBusinessLogic.Enums;
|
||
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>
|
||
/// Реализация интерфейса IClassroomService
|
||
/// </summary>
|
||
public class ClassroomService : IClassroomService
|
||
{
|
||
public OperationResultModel Create(ClassroomSetBindingModel model)
|
||
{
|
||
using var context = DatabaseManager.GetContext;
|
||
|
||
var exsistEntity = context.Classrooms.FirstOrDefault(x => x.Number == model.Number);
|
||
if (exsistEntity == null)
|
||
{
|
||
var entity = Mapper.MapToClass<ClassroomSetBindingModel, Classroom>(model, true);
|
||
context.Classrooms.Add(entity);
|
||
context.SaveChanges();
|
||
return OperationResultModel.Success(Mapper.MapToClass<Classroom, ClassroomViewModel>(entity, true));
|
||
}
|
||
else
|
||
{
|
||
if (exsistEntity.IsDeleted)
|
||
{
|
||
exsistEntity = Mapper.MapToClass(model, exsistEntity, true);
|
||
exsistEntity.IsDeleted = false;
|
||
context.SaveChanges();
|
||
return OperationResultModel.Success(Mapper.MapToClass<Classroom, ClassroomViewModel>(exsistEntity, true));
|
||
}
|
||
else
|
||
{
|
||
return OperationResultModel.Error("Error:", "Элемент уже существует", ResultServiceStatusCode.ExsistItem);
|
||
}
|
||
}
|
||
}
|
||
|
||
public OperationResultModel Delete(ClassroomGetBindingModel model)
|
||
{
|
||
using var context = DatabaseManager.GetContext;
|
||
|
||
var entity = context.Classrooms.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(ClassroomGetBindingModel model)
|
||
{
|
||
int countPages = 0;
|
||
using var context = DatabaseManager.GetContext;
|
||
|
||
// для одной записи
|
||
if (model.Id.HasValue)
|
||
{
|
||
var entity = context.Classrooms.FirstOrDefault(x => x.Id == model.Id.Value);
|
||
if (entity == null)
|
||
{
|
||
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
||
}
|
||
return OperationResultModel.Success(Mapper.MapToClass<Classroom, ClassroomViewModel>(entity, model.HaveRight));
|
||
}
|
||
|
||
var query = context.Classrooms.Where(x => !x.IsDeleted).AsQueryable();
|
||
if (model.UseInSchedule.HasValue)
|
||
{
|
||
query = query.Where(x => x.ClassroomType == (int)ClassroomType.Дисплейный || x.ClassroomType == (int)ClassroomType.Лекционный ||
|
||
x.ClassroomType == (int)ClassroomType.Обычный);
|
||
}
|
||
if (model.EmployeeId.HasValue)
|
||
{
|
||
query = query.Where(x => x.EmployeeId == model.EmployeeId.Value);
|
||
}
|
||
|
||
query = query.OrderBy(x => x.Number);
|
||
|
||
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.Employee);
|
||
|
||
var result = new ClassroomListViewModel
|
||
{
|
||
MaxCount = countPages,
|
||
List = query.Select(x => Mapper.MapToClass<Classroom, ClassroomViewModel>(x, model.HaveRight)).ToList()
|
||
};
|
||
|
||
return OperationResultModel.Success(result);
|
||
}
|
||
|
||
public OperationResultModel Update(ClassroomSetBindingModel model)
|
||
{
|
||
using var context = DatabaseManager.GetContext;
|
||
|
||
var entity = context.Classrooms.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<Classroom, ClassroomViewModel>(entity, true));
|
||
}
|
||
}
|
||
} |