Skip to content

Commit

Permalink
User repos return nil
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Nov 13, 2023
1 parent 179f234 commit e7b15de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions UserRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ const ClientUserPrefix = "cu"

const UserScanBatchSize = 500

func (repository *UserRepository) SaveUser(clientUserId string, student *models.Student) (err error, hasChanges bool) {
func (repository *UserRepository) SaveUser(clientUserId string, student *models.Student) (error, bool) {
previousStudent := repository.GetStudent(clientUserId)

studentSerialized, err := proto.Marshal(student)
ctx := context.Background()

hasChanges := previousStudent != student
if previousStudent != nil && student != nil {
hasChanges = previousStudent.Id != student.Id
}

if err == nil {
clientUserKey := repository.getClientUserKey(clientUserId)
pipe := repository.redis.TxPipeline()

if previousStudent.Id != student.Id && previousStudent.Id != 0 {
if hasChanges && previousStudent != nil {
pipe.SRem(ctx, repository.getStudentKey(previousStudent.Id), clientUserId)
}

if student.Id == 0 {
if student == nil || student.Id == 0 {
pipe.Del(ctx, clientUserKey)
} else {
pipe.Set(ctx, clientUserKey, studentSerialized, UserExpiration)
Expand All @@ -49,7 +54,7 @@ func (repository *UserRepository) SaveUser(clientUserId string, student *models.
_, err = pipe.Exec(ctx)
}

return err, previousStudent.Id != student.Id
return err, hasChanges
}

func (repository *UserRepository) Commit() error {
Expand All @@ -68,16 +73,16 @@ func (repository *UserRepository) GetStudent(clientUserId string) *models.Studen
UserExpiration,
).Bytes()

student := &models.Student{}
if studentSerialized != nil && len(studentSerialized) > 0 {
_ = proto.Unmarshal(studentSerialized, student)
}

if student.Id != 0 {
repository.redis.Expire(ctx, repository.getStudentKey(student.Id), UserExpiration)
student := models.Student{}
_ = proto.Unmarshal(studentSerialized, &student)
if student.Id != 0 {
repository.redis.Expire(ctx, repository.getStudentKey(student.Id), UserExpiration)
return &student
}
}

return student
return nil
}

func (repository *UserRepository) GetClientUserIds(studentId uint) []string {
Expand Down
4 changes: 2 additions & 2 deletions UserRepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestUserRepository_SaveUser(t *testing.T) {
assert.NoError(t, err)

actualStudent = userRepository.GetStudent(expectedClientUserId)
assert.Equal(t, emptyStudent.String(), actualStudent.String())
assert.Nil(t, actualStudent)

actualClientIds = userRepository.GetClientUserIds(uint(expectedStudent1.Id))
assert.Len(t, actualClientIds, 0)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestUserRepository_GetStudent(t *testing.T) {
redisMock.ExpectGetEx(clientUserId, UserExpiration).RedisNil()

actualStudent := userRepository.GetStudent(clientUserId)
assertStudent(t, &models.Student{}, actualStudent)
assert.Nil(t, actualStudent)
})
}

Expand Down

0 comments on commit e7b15de

Please sign in to comment.