<commit_before>package store

import (
	"sync"

	"github.com/gitpods/gitpods"
	"github.com/go-errors/errors"
	"golang.org/x/crypto/bcrypt"
)

var UserNotFound = errors.New("user not found")

type UserInMemory struct {
	mu    sync.RWMutex
	users []gitpods.User
}

func NewUserInMemory() *UserInMemory {
	pass1, _ := bcrypt.GenerateFromPassword([]byte("kubernetes"), bcrypt.DefaultCost)
	pass2, _ := bcrypt.GenerateFromPassword([]byte("golang"), bcrypt.DefaultCost)

	return &UserInMemory{
		users: []gitpods.User{{
			ID:       "25558000-2565-48dc-84eb-18754da2b0a2",
			Username: "metalmatze",
			Name:     "<NAME>",
			Email:    "<EMAIL>",
			Password: string(<PASSWORD>),
		}, {
			ID:       "911d24ae-ad9b-4e50-bf23-9dcbdc8134c6",
			Username: "tboerger",
			Name:     "<NAME>",
			Email:    "<EMAIL>",
			Password: string(<PASSWORD>),
		}},
	}
}

func (s *UserInMemory) List() ([]gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.users, nil
}

func (s *UserInMemory) GetUser(username string) (gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, user := range s.users {
		if user.Username == username {
			return user, nil
		}
	}

	return gitpods.User{}, UserNotFound
}

func (s *UserInMemory) GetUserByEmail(email string) (gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, user := range s.users {
		if user.Email == email {
			return user, nil
		}
	}
	return gitpods.User{}, UserNotFound
}

func (s *UserInMemory) CreateUser(user gitpods.User) (gitpods.User, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.users = append(s.users, user)

	return user, nil
}

func (s *UserInMemory) UpdateUser(username string, updatedUser gitpods.User) (gitpods.User, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	for i, user := range s.users {
		if user.Username == username {
			s.users[i] = updatedUser
			return updatedUser, nil
		}
	}
	return updatedUser, UserNotFound
}

func (s *UserInMemory) DeleteUser(username string) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	for i, user := range s.users {
		if user.Username == username {
			s.users = append(s.users[:i], s.users[i+1:]...)
			return nil
		}
	}
	return UserNotFound
}<commit_msg>Update only the updatable parts of a user<commit_after>package store

import (
	"sync"

	"github.com/gitpods/gitpods"
	"github.com/go-errors/errors"
	"golang.org/x/crypto/bcrypt"
)

var UserNotFound = errors.New("user not found")

type UserInMemory struct {
	mu    sync.RWMutex
	users []gitpods.User
}

func NewUserInMemory() *UserInMemory {
	pass1, _ := bcrypt.GenerateFromPassword([]byte("kubernetes"), bcrypt.DefaultCost)
	pass2, _ := bcrypt.GenerateFromPassword([]byte("golang"), bcrypt.DefaultCost)

	return &UserInMemory{
		users: []gitpods.User{{
			ID:       "25558000-2565-48dc-8<PASSWORD>",
			Username: "metalmatze",
			Name:     "<NAME>",
			Email:    "<EMAIL>",
			Password: string(<PASSWORD>),
		}, {
			ID:       "<PASSWORD>",
			Username: "tboerger",
			Name:     "<NAME>",
			Email:    "<EMAIL>",
			Password: string(<PASSWORD>),
		}},
	}
}

func (s *UserInMemory) List() ([]gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.users, nil
}

func (s *UserInMemory) GetUser(username string) (gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, user := range s.users {
		if user.Username == username {
			return user, nil
		}
	}

	return gitpods.User{}, UserNotFound
}

func (s *UserInMemory) GetUserByEmail(email string) (gitpods.User, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	for _, user := range s.users {
		if user.Email == email {
			return user, nil
		}
	}
	return gitpods.User{}, UserNotFound
}

func (s *UserInMemory) CreateUser(user gitpods.User) (gitpods.User, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.users = append(s.users, user)

	return user, nil
}

func (s *UserInMemory) UpdateUser(username string, updatedUser gitpods.User) (gitpods.User, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	for i, user := range s.users {
		if user.Username == username {
			s.users[i].Username = updatedUser.Username
			s.users[i].Name = updatedUser.Name
			s.users[i].Email = updatedUser.Email
			return updatedUser, nil
		}
	}
	return updatedUser, UserNotFound
}

func (s *UserInMemory) DeleteUser(username string) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	for i, user := range s.users {
		if user.Username == username {
			s.users = append(s.users[:i], s.users[i+1:]...)
			return nil
		}
	}
	return UserNotFound
}
{'fraction_non_alphanumeric': 0.15826204412426834, 'fraction_numerical': 0.013957676722197209, 'mean_word_length': 3.7015873015873018, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2094922', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1688, 'n_words': 367}