Пример #1
0
        public async Task <ResponseModel <UpdateWorkingStatusModel> > Handle(UpdateWorkingStatusCommand request, CancellationToken cancellationToken)
        {
            var redisKey  = $"WorkingStatusByTenantId{request.TenantId}";
            var updatedAt = DateTimeOffset.Now;

            using (var transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var workingStatuses = await _context.WorkingStatuses
                                          .Where(x => x.TenantId == request.TenantId)
                                          .ToListAsync(cancellationToken);

                    var currentWorkingStatus = workingStatuses.FirstOrDefault(x => x.Id == request.Id);

                    if (currentWorkingStatus is null)
                    {
                        throw new NotFoundException(nameof(WorkingStatus), request.Id);
                    }

                    currentWorkingStatus.Name      = request.Name;
                    currentWorkingStatus.UpdatedBy = request.UpdatedBy;
                    currentWorkingStatus.UpdatedAt = updatedAt;

                    _context.Update(currentWorkingStatus);
                    await _context.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    await _cacheService.RedisCacheUpdateAsync(redisKey,
                                                              _ => workingStatuses
                                                              , cancellationToken);
                }
                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(WorkingStatus), request.Name);
                }
                catch (NotFoundException)
                {
                    transaction.Rollback();
                    throw;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw new TransactionException();
                }
            }

            var workingStatusModel = new UpdateWorkingStatusModel(request.Id,
                                                                  request.Name,
                                                                  request.UpdatedBy,
                                                                  updatedAt);

            return(new ResponseModel <UpdateWorkingStatusModel>(workingStatusModel));
        }
Пример #2
0
        public async Task <ResponseModel <UpdateTraineeUserModel> > Handle(UpdateTraineeUserCommand request, CancellationToken cancellationToken)
        {
            var redisKey = $"TraineeUsersWithDepartmentsByTenantId{request.TenantId}";
            var salt     = ByteArrayExtensions.GetRandomSalt();
            var updateAt = DateTimeOffset.Now;

            using (IDbContextTransaction transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    var traineeUsers = await _context.TraineeUsers
                                       .Include(x => x.Department)
                                       .Where(x => x.Department.TenantId == request.TenantId)
                                       .ToListAsync(cancellationToken);

                    var traineeUser = traineeUsers.FirstOrDefault(x => x.Id == request.Id);

                    if (traineeUser is null)
                    {
                        throw new NotFoundException(nameof(TraineeUser), request.Id);
                    }

                    traineeUser.Name                       = request.Name;
                    traineeUser.DepartmentId               = request.DepartmentId;
                    traineeUser.AvatarId                   = request.AvatarId;
                    traineeUser.Gender                     = request.Gender;
                    traineeUser.NationalIdentityNumber     = request.NationalIdentityNumber;
                    traineeUser.PhoneNumber                = request.PhoneNumber;
                    traineeUser.Surname                    = request.Surname;
                    traineeUser.WorkingStatusId            = request.WorkingStatusId;
                    traineeUser.UpdatedBy                  = request.UpdatedBy;
                    traineeUser.UpdatedAt                  = updateAt;
                    traineeUser.PasswordSalt               = salt;
                    traineeUser.Password                   = request.Password.GetSHA512(salt);
                    traineeUser.Email                      = request.Email;
                    traineeUser.LastPasswordChangeDateTime = updateAt;

                    _context.Update(traineeUser);
                    await _context.SaveChangesAsync(cancellationToken);

                    transaction.Commit();

                    await _cacheService.RedisCacheUpdateAsync(redisKey,
                                                              _ => traineeUsers
                                                              , cancellationToken);
                }
                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(TraineeUser), request.Name);
                }
                catch (NotFoundException)
                {
                    transaction.Rollback();
                    throw;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw new TransactionException();
                }
            }

            var traineeUserModel = new UpdateTraineeUserModel(request.Id,
                                                              request.Name,
                                                              request.Email,
                                                              request.Surname,
                                                              request.NationalIdentityNumber,
                                                              request.PhoneNumber,
                                                              request.Gender,
                                                              request.AvatarId,
                                                              updateAt);

            return(new ResponseModel <UpdateTraineeUserModel>(traineeUserModel));
        }