2023-10-28 09:06:35 +04:00
|
|
|
|
using UniversityContracts.BindingModels;
|
|
|
|
|
using UniversityContracts.StoragesContracts;
|
|
|
|
|
using UniversityContracts.ViewModels;
|
2023-11-09 04:07:06 +04:00
|
|
|
|
using UniversityDatabaseImplement;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
using UniversityDatabaseImplement.Models;
|
|
|
|
|
|
2023-11-09 04:07:06 +04:00
|
|
|
|
namespace UniversityUniversityDatabaseImplement.Implements
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
public class DirectionStorage : IDirectionStorage
|
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
public void Delete(DirectionBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var direction = context.Directions.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (direction != null)
|
|
|
|
|
{
|
|
|
|
|
context.Directions.Remove(direction);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Направление не найдено");
|
|
|
|
|
}
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public DirectionViewModel GetElement(DirectionBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
if (model == null)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
return null;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
using var context = new UniversityDatabase();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
var direction = context.Directions
|
|
|
|
|
.ToList()
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id || rec.Name == model.Name);
|
|
|
|
|
return direction != null ? CreateModel(direction) : null;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<DirectionViewModel> GetFilteredList(DirectionBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
if (model == null)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
return context.Directions
|
2023-11-09 04:07:06 +04:00
|
|
|
|
.Where(rec => rec.Name.Contains(model.Name))
|
|
|
|
|
.Select(CreateModel)
|
|
|
|
|
.ToList();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public List<DirectionViewModel> GetFullList()
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
return context.Directions
|
|
|
|
|
.Select(CreateModel)
|
|
|
|
|
.ToList();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public void Insert(DirectionBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
context.Directions.Add(CreateModel(model, new Direction()));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public void Update(DirectionBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var direction = context.Directions.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (direction == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Направление не найдено");
|
|
|
|
|
}
|
|
|
|
|
CreateModel(model, direction);
|
2023-10-28 09:06:35 +04:00
|
|
|
|
context.SaveChanges();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Direction CreateModel(DirectionBindingModel model, Direction direction)
|
|
|
|
|
{
|
|
|
|
|
direction.Name = model.Name;
|
|
|
|
|
return direction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DirectionViewModel CreateModel(Direction direction)
|
|
|
|
|
{
|
|
|
|
|
return new DirectionViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = direction.Id,
|
|
|
|
|
Name = direction.Name
|
|
|
|
|
};
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|