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

128 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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