74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using EkzamenContracts.BindingModels;
|
|
using EkzamenContracts.SearchModels;
|
|
using EkzamenContracts.StoragesContract;
|
|
using EkzamenContracts.ViewModels;
|
|
|
|
namespace EkzamenDatabaseImplement
|
|
{
|
|
public class GroupStorage : IGroupStorage
|
|
{
|
|
public GroupViewModel? Delete(GroupBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var element = context.Groups.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Groups.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public GroupViewModel? GetElement(GroupSearchModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
return context.Groups.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<GroupViewModel> GetFilteredList(GroupSearchModel model)
|
|
{
|
|
var result = GetElement(model);
|
|
return result != null ? new() { result } : new();
|
|
}
|
|
|
|
public List<GroupViewModel> GetFullList()
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
return context.Groups
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public GroupViewModel? Insert(GroupBindingModel model)
|
|
{
|
|
var newOrder = Group.Create(model);
|
|
if (newOrder == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConfectioneryDatabase();
|
|
context.Groups.Add(newOrder);
|
|
context.SaveChanges();
|
|
return newOrder.GetViewModel;
|
|
}
|
|
|
|
public GroupViewModel? Update(GroupBindingModel model)
|
|
{
|
|
using var context = new ConfectioneryDatabase();
|
|
var order = context.Groups.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
return order.GetViewModel;
|
|
}
|
|
}
|
|
}
|