private HonoplayDbContext InitAndGetDbContext(out Guid tenantId, out int adminUserId, out int traineeGroupId)
        {
            var context = GetDbContext();

            var salt      = ByteArrayExtensions.GetRandomSalt();
            var adminUser = new AdminUser
            {
                Id           = 1,
                Email        = "*****@*****.**",
                Password     = "******".GetSHA512(salt),
                PasswordSalt = salt,
                LastPasswordChangeDateTime = DateTime.Today.AddDays(-5),
            };

            context.AdminUsers.Add(adminUser);

            var tenant = new Tenant
            {
                Name     = "TestTenant#01",
                HostName = "localhost"
            };

            context.Tenants.Add(tenant);

            context.TenantAdminUsers.Add(new TenantAdminUser
            {
                TenantId    = tenant.Id,
                AdminUserId = adminUser.Id,
                UpdatedBy   = adminUser.Id
            });

            var traineeGroup = new TraineeGroup
            {
                TenantId  = tenant.Id,
                Name      = "Test1asd",
                UpdatedBy = adminUser.Id
            };

            context.Add(traineeGroup);

            context.SaveChanges();

            adminUserId    = adminUser.Id;
            tenantId       = tenant.Id;
            traineeGroupId = traineeGroup.Id;

            return(context);
        }
Пример #2
0
        public async Task <ResponseModel <List <CreateTraineeGroupModel> > > Handle(CreateTraineeGroupCommand request, CancellationToken cancellationToken)
        {
            var redisKey             = $"TraineeGroupsByTenantId{request.TenantId}";
            var newTraineeGroups     = new List <TraineeGroup>();
            var createdTraineeGroups = new List <CreateTraineeGroupModel>();

            using (var transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    foreach (var createTraineeGroupModel in request.CreateTraineeGroupCommandModels)
                    {
                        var newTraineeGroup = new TraineeGroup
                        {
                            TenantId  = request.TenantId,
                            CreatedBy = request.CreatedBy,
                            Name      = createTraineeGroupModel.Name
                        };
                        newTraineeGroups.Add(newTraineeGroup);
                    }

                    if (newTraineeGroups.Count > 20)
                    {
                        _context.BulkInsert(newTraineeGroups);
                    }
                    else
                    {
                        await _context.TraineeGroups.AddRangeAsync(newTraineeGroups, cancellationToken);

                        await _context.SaveChangesAsync(cancellationToken);
                    }

                    transaction.Commit();
                    await _cacheService.RedisCacheUpdateAsync(redisKey,
                                                              _ => _context.TraineeGroups
                                                              .AsNoTracking()
                                                              .ToListAsync(cancellationToken),
                                                              cancellationToken);

                    newTraineeGroups.ForEach(x => createdTraineeGroups.Add(new CreateTraineeGroupModel(x.Id,
                                                                                                       x.Name,
                                                                                                       x.CreatedBy,
                                                                                                       x.CreatedAt
                                                                                                       )));
                }
                catch (DbUpdateException ex) when((ex.InnerException is SqlException sqlException && (sqlException.Number == 2627 || sqlException.Number == 2601)) ||
                                                  (ex.InnerException is SqliteException sqliteException && sqliteException.SqliteErrorCode == 19))
                {
                    transaction.Rollback();

                    throw new ObjectAlreadyExistsException(nameof(TraineeGroup), ExceptionExtensions.GetExceptionMessage(ex));
                }
                catch (NotFoundException)
                {
                    transaction.Rollback();
                    throw;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw new TransactionException();
                }
            }

            return(new ResponseModel <List <CreateTraineeGroupModel> >(createdTraineeGroups));
        }
Пример #3
0
 public static TraineeGroupsListModel Create(TraineeGroup traineeGroup)
 {
     return(Projection.Compile().Invoke(traineeGroup));
 }
Пример #4
0
 public static TraineeGroupDetailModel Create(TraineeGroup traineeGroup) => Projection.Compile().Invoke(traineeGroup);