PIAPS_CW/DatabaseImplement/Implements/RoleStorage.cs

88 lines
1.8 KiB
C#
Raw Normal View History

using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class RoleStorage : IRoleStorage
{
public RoleBindingModel? Delete(RoleSearchModel model)
{
2024-06-04 23:43:52 +04:00
if (model.Id is null)
{
return null;
}
var context = new Database();
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
if (role is null)
{
return null;
}
context.Remove(role);
context.SaveChanges();
return role.GetBindingModel();
}
public RoleBindingModel? GetElement(RoleSearchModel model)
{
2024-06-04 23:43:52 +04:00
if (model.Id is null)
{
return null;
}
var context = new Database();
return context.Roles
.FirstOrDefault(r => r.Id == model.Id)
?.GetBindingModel();
}
public IEnumerable<RoleBindingModel> GetList(RoleSearchModel? model)
{
var context = new Database();
if (model is null)
{
return context.Roles.Select(r => r.GetBindingModel());
}
if (model.Id is null)
{
return [];
}
return context.Roles
.Where(r => r.Id == model.Id)
.Select(r => r.GetBindingModel());
}
public RoleBindingModel? Insert(RoleBindingModel model)
{
var context = new Database();
var newRole = Models.Role.ToRoleFromBinding(model);
context.Roles.Add(newRole);
context.SaveChanges();
return newRole.GetBindingModel();
}
public RoleBindingModel? Update(RoleBindingModel model)
{
var context = new Database();
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
if (role is null)
{
return null;
}
role.Update(model);
context.SaveChanges();
return role.GetBindingModel();
}
}
}