Working on organization unit table
This commit is contained in:
parent
2d517170fd
commit
0aea91a59c
@ -78,17 +78,11 @@ namespace UserService.DatabaseLayer.DataModels
|
||||
[Column, Nullable] public int? ParentId { get; set; } // int(11)
|
||||
[Column, Nullable] public int? ManagerId { get; set; } // int(11)
|
||||
[Column, Nullable] public string? EMail { get; set; } // text
|
||||
[Column, NotNull ] public bool IsActive { get; set; } // bit(1)
|
||||
[Column, NotNull ] public string Discriminator { get; set; } = null!; // text
|
||||
[Column, Nullable] public bool? IsActive { get; set; } // bit(1)
|
||||
[Column, NotNull ] public string Discriminator { get; set; } = null!; // varchar(16)
|
||||
|
||||
#region Associations
|
||||
|
||||
/// <summary>
|
||||
/// NodeModels_ibfk_2_BackReference
|
||||
/// </summary>
|
||||
[Association(ThisKey="Id", OtherKey="ManagerId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||
public IEnumerable<NodeModel> Ibfks { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// IsMemberOf_ibfk_2_BackReference
|
||||
/// </summary>
|
||||
@ -101,24 +95,6 @@ namespace UserService.DatabaseLayer.DataModels
|
||||
[Association(ThisKey="Id", OtherKey="NodeId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||
public IEnumerable<IsMemberOf> IsMemberOfibfks { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// NodeModels_ibfk_2
|
||||
/// </summary>
|
||||
[Association(ThisKey="ManagerId", OtherKey="Id", CanBeNull=true, Relationship=Relationship.ManyToOne, KeyName="NodeModels_ibfk_2", BackReferenceName="Ibfks")]
|
||||
public NodeModel? Manager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// NodeModels_ibfk_3_BackReference
|
||||
/// </summary>
|
||||
[Association(ThisKey="Id", OtherKey="ParentId", CanBeNull=true, Relationship=Relationship.OneToMany, IsBackReference=true)]
|
||||
public IEnumerable<NodeModel> NodeModelsIbfk3BackReferences { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// NodeModels_ibfk_3
|
||||
/// </summary>
|
||||
[Association(ThisKey="ParentId", OtherKey="Id", CanBeNull=true, Relationship=Relationship.ManyToOne, KeyName="NodeModels_ibfk_3", BackReferenceName="NodeModelsIbfk3BackReferences")]
|
||||
public NodeModel? Parent { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
{
|
||||
Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<NodeModel, bool>>? predicate = null, CancellationToken token = default);
|
||||
Task<T?> GetAsync(Expression<Func<NodeModel, bool>> predicate, CancellationToken token = default);
|
||||
Task AddAsync(T entity, CancellationToken token = default);
|
||||
Task<int> AddAsync(T entity, CancellationToken token = default);
|
||||
Task<bool> UpdateAsync(T entity, CancellationToken token = default);
|
||||
Task DeleteAsync(T entity, CancellationToken token = default);
|
||||
}
|
||||
|
@ -1,38 +1,87 @@
|
||||
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 : IOrganizationUnitsRepository
|
||||
public class OrganizationUnitsRepository : BaseRepository, IOrganizationUnitsRepository
|
||||
{
|
||||
public Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(Expression<Func<NodeModel, bool>>? predicate = null, CancellationToken token = default)
|
||||
public async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(Expression<Func<NodeModel, bool>>? predicate = null, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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 Task<OrganizationUnit?> GetAsync(Expression<Func<NodeModel, bool>> predicate, CancellationToken token = default)
|
||||
public async Task<OrganizationUnit?> GetAsync(Expression<Func<NodeModel, bool>> predicate, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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 Task AddAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
public async Task<int> AddAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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 Task<bool> UpdateAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
public async Task<bool> UpdateAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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 Task DeleteAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
public async Task DeleteAsync(OrganizationUnit entity, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
await using var db = new UserService2DB();
|
||||
await db.NodeModels.DeleteAsync(x => x.Id == entity.Id, token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
@ -24,7 +24,7 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task AddAsync(SecurityGroup entity, CancellationToken token = default)
|
||||
public Task<int> AddAsync(SecurityGroup entity, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
{
|
||||
await using var db = new UserService2DB();
|
||||
var users = await db.NodeModels
|
||||
.LoadWith(x=> x.Parent)
|
||||
.LoadWith(x => x.Parent)
|
||||
.Where(x => x.Discriminator == nameof(User))
|
||||
.WhereOrDefault(predicate)
|
||||
.Select(x => new User
|
||||
@ -28,7 +28,7 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
Description = x.Description,
|
||||
FirstName = x.FirstName,
|
||||
LastName = x.LastName,
|
||||
IsActive = x.IsActive,
|
||||
IsActive = x.IsActive ?? false,
|
||||
EMail = x.EMail,
|
||||
ParentId = x.ParentId,
|
||||
})
|
||||
@ -37,7 +37,8 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<User?> GetAsync(Expression<Func<NodeModel, bool>> predicate, CancellationToken token = default)
|
||||
public async Task<User?> GetAsync(Expression<Func<NodeModel, bool>> predicate,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserService2DB();
|
||||
var result = await db.NodeModels
|
||||
@ -49,18 +50,18 @@ namespace UserService.DatabaseLayer.Repositories
|
||||
Description = x.Description,
|
||||
FirstName = x.FirstName,
|
||||
LastName = x.LastName,
|
||||
IsActive = x.IsActive,
|
||||
IsActive = x.IsActive ?? false,
|
||||
EMail = x.EMail
|
||||
}).FirstOrDefaultAsync(token).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task AddAsync(User entity, CancellationToken token = default)
|
||||
public async Task<int> AddAsync(User entity, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserService2DB();
|
||||
|
||||
await db.NodeModels.InsertAsync(() => new NodeModel
|
||||
return await db.NodeModels.InsertWithInt32IdentityAsync(() => new NodeModel
|
||||
{
|
||||
CommonName = entity.CommonName,
|
||||
FirstName = entity.FirstName,
|
||||
|
@ -3,5 +3,6 @@
|
||||
public class OrganizationUnit : Node
|
||||
{
|
||||
public Member? Manager { get; set; }
|
||||
public int? ManagerId { get; set; }
|
||||
}
|
||||
}
|
61
UserService.Test/OrgUnitRepositoryTests.cs
Normal file
61
UserService.Test/OrgUnitRepositoryTests.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using UserService.DatabaseLayer.Repositories;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Test
|
||||
{
|
||||
public class OrgUnitRepositoryTests
|
||||
{
|
||||
private OrganizationUnitsRepository _repository;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_repository = new OrganizationUnitsRepository();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(2)]
|
||||
public async Task Test1()
|
||||
{
|
||||
var entities = await _repository.GetAllAsync().ConfigureAwait(false);
|
||||
Assert.AreEqual(entities.Count, 4);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(1)]
|
||||
public async Task Test2()
|
||||
{
|
||||
var ou = new OrganizationUnit {CommonName = "Users", Description = "Users"};
|
||||
var id = await _repository.AddAsync(ou).ConfigureAwait(false);
|
||||
ou = new OrganizationUnit {CommonName = "USA", Description = "Users", ParentId = id};
|
||||
id = await _repository.AddAsync(ou).ConfigureAwait(false);
|
||||
ou = new OrganizationUnit { CommonName = "Arizona", Description = "Users", ParentId = id };
|
||||
id = await _repository.AddAsync(ou).ConfigureAwait(false);
|
||||
ou = new OrganizationUnit { CommonName = "Germany", Description = "Users", ParentId = id };
|
||||
await _repository.AddAsync(ou).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(3)]
|
||||
public async Task Test3()
|
||||
{
|
||||
var entity = await _repository.GetAsync(x => x.CommonName == "Users").ConfigureAwait(false);
|
||||
entity.Description = DateTime.Now.ToString("O");
|
||||
await _repository.UpdateAsync(entity).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(4)]
|
||||
public async Task Test4()
|
||||
{
|
||||
var entity = await _repository.GetAllAsync().ConfigureAwait(false);
|
||||
foreach (var organizationUnit in entity)
|
||||
{
|
||||
await _repository.DeleteAsync(organizationUnit).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -8,12 +8,19 @@ namespace UserService.Test
|
||||
{
|
||||
public class UserRepositoryTests
|
||||
{
|
||||
private UsersRepository _repository;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_repository = new UsersRepository();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(2)]
|
||||
public async Task Test1()
|
||||
{
|
||||
var usersRepository = new UsersRepository();
|
||||
var users = await usersRepository.GetAllAsync().ConfigureAwait(false);
|
||||
var users = await _repository.GetAllAsync().ConfigureAwait(false);
|
||||
Assert.AreEqual(users.Count, 1);
|
||||
}
|
||||
|
||||
@ -21,7 +28,6 @@ namespace UserService.Test
|
||||
[Order(1)]
|
||||
public async Task Test2()
|
||||
{
|
||||
var usersRepository = new UsersRepository();
|
||||
var user = new User
|
||||
{
|
||||
CommonName = "holger",
|
||||
@ -29,28 +35,24 @@ namespace UserService.Test
|
||||
LastName = "Börchers",
|
||||
IsActive = true
|
||||
};
|
||||
await usersRepository.AddAsync(user).ConfigureAwait(false);
|
||||
await _repository.AddAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(3)]
|
||||
public async Task Test3()
|
||||
{
|
||||
var usersRepository = new UsersRepository();
|
||||
|
||||
var user = await usersRepository.GetAsync(x => x.CommonName == "holger").ConfigureAwait(false);
|
||||
var user = await _repository.GetAsync(x => x.CommonName == "holger").ConfigureAwait(false);
|
||||
user.Description = DateTime.Now.ToString("O");
|
||||
await usersRepository.UpdateAsync(user).ConfigureAwait(false);
|
||||
await _repository.UpdateAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(4)]
|
||||
public async Task Test4()
|
||||
{
|
||||
var usersRepository = new UsersRepository();
|
||||
|
||||
var user = await usersRepository.GetAsync(x => x.CommonName == "holger").ConfigureAwait(false);
|
||||
await usersRepository.DeleteAsync(user).ConfigureAwait(false);
|
||||
var user = await _repository.GetAsync(x => x.CommonName == "holger").ConfigureAwait(false);
|
||||
await _repository.DeleteAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user