<commit_before>package com.wcecil.data.repositiories;

import static org.springframework.data.mongodb.core.query.Criteria.*
import static org.springframework.data.mongodb.core.query.Query.*

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.stereotype.Service

import com.wcecil.data.objects.User
import com.wcecil.data.objects.UserToken

@Service
public class UsersRepository {
	private final static String USERS_REPO = "deck.users.repo";
	private final static String TOKENS_REPO = "deck.users.tokens";
	
	@Autowired
	MongoTemplate mongoTemplate;

	boolean mask = true;

	public List<User> listAll() {
		def result = mongoTemplate.findAll(User.class, USERS_REPO);

		mask(result)

		result
	}

	private def mask(List<User> users){
		users.each{mask it}
	}

	private def mask(User user){
		if(mask){
			user.login=null
			user.password=<PASSWORD>
		}
		
		user
	}

	public User createNew(String userName, String password, String name) {
		def sameUserName = mongoTemplate.find(query(where("login").is(userName)), User.class, USERS_REPO);
		if(sameUserName){
			throw new IllegalStateException("User with username '$userName' already exists");
		}
		
		if (name == null) {
			name = userName;
		}
		User p = new User();
		p.setLogin(userName);
		p.setPassword(password);
		p.setName(name);

		mongoTemplate.save(p, USERS_REPO);

		mask p
	}

	public User auth(String userName, String password) {
		return mask(mongoTemplate.findOne(query(where("login").is(userName)
				.and("password").is(password)), User.class, USERS_REPO));
	}

	public void saveUser(User u){
		mongoTemplate.save(u, USERS_REPO);
	}

	public User addGameToUser(String userId, String gameId){
		User user = getUserById(userId);
		if(user){
			if(!user.games){
				user.games = []
			}

			user.games << gameId

			saveUser(user);
		}

		user
	}

	private User getUserById(String userId) {
		User user = mongoTemplate.findById(userId, User.class, USERS_REPO)
		return user
	}
	
	public User getUser(String userId) {
		return mask(getUserById(userId))
	}
	
	public UserToken createTokenForUserId(String userId){
		UserToken token = new UserToken(userId:userId)
		
		mongoTemplate.save(token, TOKENS_REPO)
		
		return token
	}
	
	public UserToken getUserTokenForTokenId(String token){
		UserToken myToken = mongoTemplate.findById(token, UserToken.class, TOKENS_REPO)
		return myToken
	}
}<commit_msg>Only one token per user for now<commit_after>package com.wcecil.data.repositiories;

import static org.springframework.data.mongodb.core.query.Criteria.*
import static org.springframework.data.mongodb.core.query.Query.*

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.stereotype.Service

import com.wcecil.data.objects.User
import com.wcecil.data.objects.UserToken

@Service
public class UsersRepository {
	private final static String USERS_REPO = "deck.users.repo";
	private final static String TOKENS_REPO = "deck.users.tokens";

	@Autowired
	MongoTemplate mongoTemplate;

	boolean mask = true;

	public List<User> listAll() {
		def result = mongoTemplate.findAll(User.class, USERS_REPO);

		mask(result)

		result
	}

	private def mask(List<User> users){
		users.each{mask it}
	}

	private def mask(User user){
		if(mask){
			user.login=null
			user.password=null
		}

		user
	}

	public User createNew(String userName, String password, String name) {
		def sameUserName = mongoTemplate.find(query(where("login").is(userName)), User.class, USERS_REPO);
		if(sameUserName){
			throw new IllegalStateException("User with username '$userName' already exists");
		}

		if (name == null) {
			name = userName;
		}
		User p = new User();
		p.setLogin(userName);
		p.setPassword(password);
		p.setName(name);

		mongoTemplate.save(p, USERS_REPO);

		mask p
	}

	public User auth(String userName, String password) {
		return mask(mongoTemplate.findOne(query(where("login").is(userName)
				.and("password").is(password)), User.class, USERS_REPO));
	}

	public void saveUser(User u){
		mongoTemplate.save(u, USERS_REPO);
	}

	public User addGameToUser(String userId, String gameId){
		User user = getUserById(userId);
		if(user){
			if(!user.games){
				user.games = []
			}

			user.games << gameId

			saveUser(user);
		}

		user
	}

	private User getUserById(String userId) {
		User user = mongoTemplate.findById(userId, User.class, USERS_REPO)
		return user
	}

	public User getUser(String userId) {
		return mask(getUserById(userId))
	}

	public UserToken createTokenForUserId(String userId){
		UserToken token = mongoTemplate.findOne(query(where('userId').is(userId)), UserToken.class, TOKENS_REPO)

		if(!token){
			token = new UserToken(userId:userId)

			mongoTemplate.save(token, TOKENS_REPO)
		}

		return token
	}

	public UserToken getUserTokenForTokenId(String token){
		UserToken myToken = mongoTemplate.findById(token, UserToken.class, TOKENS_REPO)
		return myToken
	}
}
{'fraction_non_alphanumeric': 0.11320380650277558, 'fraction_numerical': 0.0, 'mean_word_length': 4.605555555555555, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 8, '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': '1219566', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1680, 'n_words': 359}