using CoreDatabase; using CoreDatabase.Models.Department; using DepartmentContract.BindingModels; using DepartmentContract.Services.IGenericEntityService; using DepartmentContract.ViewModels; using System; using System.Linq; using ToolsModule.ManagmentEntity; using ToolsModule.ManagmentMapping; namespace DepartmentDatabaseImplementation.Implementations.AbstractGenerticEntityService { /// /// Реализация IStudentMarkSyncHistoryService /// public class StudentMarkSyncHistoryService : IStudentMarkSyncHistoryService { public OperationResultModel Create(StudentMarkSyncHistorySetBindingModel model) { using var context = DatabaseManager.GetContext; var entity = Mapper.MapToClass(model, true); context.StudentMarkSyncHistories.Add(entity); context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } public OperationResultModel Delete(StudentMarkSyncHistoryGetBindingModel model) { using var context = DatabaseManager.GetContext; using var transaction = context.Database.BeginTransaction(); try { var entity = context.StudentMarkSyncHistories.FirstOrDefault(x => x.Id == model.Id); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } context.StudentMarkSyncHistories.Remove(entity); context.SaveChanges(); var records = context.StudentMarkSyncHistoryRecords.Where(x => x.StudentMarkSyncHistoryId == model.Id); context.StudentMarkSyncHistoryRecords.RemoveRange(records); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } return OperationResultModel.Success(true); } public OperationResultModel Read(StudentMarkSyncHistoryGetBindingModel model) { int countPages = 0; using var context = DatabaseManager.GetContext; // для одной записи if (model.Id.HasValue) { var entity = context.StudentMarkSyncHistories.FirstOrDefault(x => x.Id == model.Id.Value); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } return OperationResultModel.Success(Mapper.MapToClass(entity, model.HaveRight)); } var query = context.StudentMarkSyncHistories.AsQueryable(); query = query.OrderByDescending(x => x.SyncDate); 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 StudentMarkSyncHistoryListViewModel { MaxCount = countPages, List = query.Select(x => Mapper.MapToClass(x, model.HaveRight)).ToList() }; return OperationResultModel.Success(result); } public OperationResultModel Restore(StudentMarkSyncHistoryGetBindingModel model) { throw new NotImplementedException(); } public OperationResultModel Update(StudentMarkSyncHistorySetBindingModel model) { using var context = DatabaseManager.GetContext; var entity = context.StudentMarkSyncHistories.FirstOrDefault(x => x.Id == model.Id); if (entity == null) { return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound); } entity = Mapper.MapToClass(model, entity, true); context.SaveChanges(); return OperationResultModel.Success(Mapper.MapToClass(entity, true)); } } }