87 lines
3.5 KiB
C#
87 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using LinqToDB;
|
|
using UserService.DatabaseLayer.DataModels;
|
|
using UserService.Infrastructure.DataModels;
|
|
|
|
namespace UserService.DatabaseLayer.Repositories
|
|
{
|
|
public class OrganizationUnitsRepository : BaseRepository, IOrganizationUnitsRepository
|
|
{
|
|
public async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(Expression<Func<NodeModel, bool>>? predicate = null, CancellationToken token = default)
|
|
{
|
|
await using var db = new UserService2DB();
|
|
var organizationUnits = await db.NodeModels
|
|
.Where(x => x.Discriminator == nameof(OrganizationUnit))
|
|
.WhereOrDefault(predicate)
|
|
.Select(x => new OrganizationUnit
|
|
{
|
|
Id = x.Id,
|
|
CommonName = x.CommonName,
|
|
Description = x.Description,
|
|
ParentId = x.ParentId,
|
|
})
|
|
.ToListAsync(token).ConfigureAwait(false);
|
|
return organizationUnits;
|
|
}
|
|
|
|
public async Task<OrganizationUnit?> GetAsync(Expression<Func<NodeModel, bool>> predicate, CancellationToken token = default)
|
|
{
|
|
await using var db = new UserService2DB();
|
|
var result = await db.NodeModels
|
|
.Where(predicate)
|
|
.Select(x => new OrganizationUnit
|
|
{
|
|
Id = x.Id,
|
|
CommonName = x.CommonName,
|
|
Description = x.Description,
|
|
ParentId = x.ParentId,
|
|
ManagerId = x.ManagerId,
|
|
}).FirstOrDefaultAsync(token).ConfigureAwait(false);
|
|
return result;
|
|
}
|
|
|
|
public async Task<int> AddAsync(OrganizationUnit entity, CancellationToken token = default)
|
|
{
|
|
if (entity == null) throw new ArgumentNullException(nameof(entity));
|
|
await using var db = new UserService2DB();
|
|
|
|
var managerId = entity.Manager?.Id;
|
|
return await db.NodeModels.InsertWithInt32IdentityAsync(() => new NodeModel
|
|
{
|
|
CommonName = entity.CommonName,
|
|
Description = entity.Description,
|
|
Discriminator = nameof(OrganizationUnit),
|
|
ParentId = entity.ParentId,
|
|
ManagerId = managerId,
|
|
}, token).ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(OrganizationUnit entity, CancellationToken token = default)
|
|
{
|
|
if (entity == null) throw new ArgumentNullException(nameof(entity));
|
|
await using var db = new UserService2DB();
|
|
var managerId = entity.Manager?.Id;
|
|
var changedRows = await db.NodeModels.UpdateAsync(x => new NodeModel
|
|
{
|
|
Id = entity.Id,
|
|
CommonName = entity.CommonName,
|
|
Description = entity.Description,
|
|
Discriminator = nameof(OrganizationUnit),
|
|
ParentId = entity.ParentId,
|
|
ManagerId = managerId,
|
|
}, token).ConfigureAwait(false);
|
|
return changedRows > 0;
|
|
}
|
|
|
|
public async Task DeleteAsync(OrganizationUnit entity, CancellationToken token = default)
|
|
{
|
|
await using var db = new UserService2DB();
|
|
await db.NodeModels.DeleteAsync(x => x.Id == entity.Id, token).ConfigureAwait(false);
|
|
}
|
|
}
|
|
} |