| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Data.SqlClient;
- using Quadarax.Application.QLiberace.Base;
- using Quadarax.Application.QLiberace.Base.Entities;
- using Quadarax.Application.QLiberace.Base.Repositories;
- using Quadarax.Application.QLiberace.Common.Domain;
- using Quadarax.Application.QLiberace.Console.Commands.Base;
- using Quadarax.Foundation.Core.Data.Domain;
- using Quadarax.Foundation.Core.QConsole;
- using Quadarax.Foundation.Core.QConsole.Attributes;
- using Quadarax.Foundation.Core.Value;
- namespace Quadarax.Application.QLiberace.Console.Commands
- {
- [CommandDefinition]
- internal class DbInitCmd : DbContextCommand
- {
- #region *** Properties ***
- public override string Name => Constants.Commands.DbInit.Name;
- public override string Description => Constants.Commands.DbInit.Description;
- #endregion
- #region *** Fields ***
- #endregion
- #region *** Constructor ***
- public DbInitCmd(Engine engine) : base(engine)
- {
- }
- #endregion
- #region *** EXECUTE ***
- protected override Result OnExecute(QlbrcDbContext context)
- {
- WriteInfo("Initializing database data...");
- var resultCode = Constants.ReturnCodes.Ok;
- try
- {
- var dbcResolver = new DomainContextResolver<QlbrcDbContext>(context);
- var uow = new UnitOfWork<QlbrcDbContext>(dbcResolver);
- var rtenant = new RepoTenant(uow.As<IDataDomain>(), new QlbrcContext(null, null, 1));
- var tenant = rtenant.GetByCode("default");
- if (tenant == null)
- {
- tenant = rtenant.New();
- tenant.Code = "default";
- tenant.Name = "Default tenant";
- tenant.IsLocked = false;
- rtenant.Set(tenant);
- uow.Commit();
- WriteInfo($"Tenant: '{tenant.Code}' created");
- }
- else
- {
- WriteCaption($"Tenant: '{tenant.Code}' already exists");
- }
- tenant = rtenant.GetByCode("test");
- if (tenant == null)
- {
- tenant = rtenant.New();
- tenant.Code = "test";
- tenant.Name = "Test tenant";
- tenant.IsLocked = false;
- uow.Commit();
- WriteInfo($"Tenant: '{tenant.Code}' created");
- }
- else
- {
- WriteCaption($"Tenant: '{tenant.Code}' already exists");
- }
- var ruser = new RepoUser(uow.As<IDataDomain>());
- var user = ruser.GetByLoginName("testuser");
- if (user == null)
- {
- user = ruser.New();
- user.LoginName = "testuser";
- tenant.IsLocked = false;
- uow.Commit();
- WriteInfo($"User: '{user.LoginName}' created");
- }
- else
- {
- WriteCaption($"User: '{user.LoginName}' already exists");
- }
- WriteInfo($"Assigning user '{user.LoginName}' to tenants..");
- var tenant1Id = rtenant.GetByCode("default")?.Id;
- var tenant2Id = rtenant.GetByCode("test")?.Id;
- var assign = new TenantUser();
- assign.UserId = user.Id;
- assign.TenantId = tenant1Id.GetValueOrDefault();
- assign = new TenantUser();
- assign.UserId = user.Id;
- assign.TenantId = tenant2Id.GetValueOrDefault();
- uow.Commit();
- WriteInfo("Assigned.");
- }
- catch (SqlException e)
- {
- return HandleConnectionException(e);
- }
- return new Result(resultCode);
- }
- #endregion
- #region *** Private operations ***
- #endregion
- }
- }
|