de.sainth.recipe.backend.db.repositories.UserRepository.java Source code

Java tutorial

Introduction

Here is the source code for de.sainth.recipe.backend.db.repositories.UserRepository.java

Source

/*
 * Copyright (c) 2017 sainth (sainth@sainth.de)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package de.sainth.recipe.backend.db.repositories;

import de.sainth.recipe.backend.db.generated.tables.records.UsersRecord;
import de.sainth.recipe.backend.rest.views.User;
import de.sainth.recipe.backend.security.ScryptPasswordEncoder;
import org.jooq.DSLContext;
import org.jooq.impl.EnumConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import static de.sainth.recipe.backend.db.generated.tables.Users.USERS;
import static org.jooq.impl.DSL.using;

@Component
public class UserRepository {
    public static class PermissionConverter extends EnumConverter<String, User.Permission> {

        public PermissionConverter() {
            super(String.class, User.Permission.class);
        }
    }

    private final DSLContext create;

    @Autowired
    public UserRepository(DSLContext create) {
        this.create = create;
    }

    public List<User> findAll() {
        return create.select(USERS.ID, USERS.NAME, USERS.EMAIL, USERS.PERMISSION).from(USERS)
                .fetch(record -> new User(record.value1(), record.value2(), record.value3(), record.value4()));
    }

    public User findOne(Long id) {
        return create.select(USERS.ID, USERS.NAME, USERS.EMAIL, USERS.PERMISSION).from(USERS).where(USERS.ID.eq(id))
                .fetchOne(record -> new User(record.value1(), record.value2(), record.value3(), record.value4()));
    }

    public User findOne(String username) {
        return create.select(USERS.ID, USERS.NAME, USERS.EMAIL, USERS.PERMISSION).from(USERS)
                .where(USERS.NAME.eq(username))
                .fetchOne(record -> new User(record.value1(), record.value2(), record.value3(), record.value4()));
    }

    public Optional<String> getEncryptedPassword(String username) {
        return Optional.ofNullable(create.select(USERS.PASSWORD).from(USERS).where(USERS.NAME.eq(username))
                .fetchOneInto(String.class));
    }

    public void delete(Long id) {
        create.delete(USERS).where(USERS.ID.eq(id)).execute();
    }

    public void deleteAll() {
        create.deleteFrom(USERS).execute();
    }

    public User save(User user) {
        AtomicReference<User> u = new AtomicReference<>();
        create.transaction(configuration -> {
            Long id = using(configuration).select(USERS.ID).from(USERS).where(USERS.NAME.eq(user.getUsername()))
                    .forUpdate().fetchOneInto(Long.class);
            UsersRecord record;
            if (id == null) {
                String encrypted = ScryptPasswordEncoder.sEncode(user.getPassword());
                record = using(configuration)
                        .insertInto(USERS, USERS.NAME, USERS.EMAIL, USERS.PASSWORD, USERS.PERMISSION)
                        .values(user.getUsername(), user.getEmail(), encrypted, user.getPermission()).returning()
                        .fetchOne();
            } else {
                record = using(configuration).update(USERS).set(USERS.EMAIL, user.getEmail())
                        .set(USERS.PERMISSION, user.getPermission()).where(USERS.ID.eq(id)).returning().fetchOne();
            }
            u.set(new User(record.getId(), record.getName(), record.getEmail(), record.getPermission()));
        });

        return u.get();
    }

    public User updatePassword(Long id, String password) {
        String encrypted = ScryptPasswordEncoder.sEncode(password);
        UsersRecord record = create.update(USERS).set(USERS.PASSWORD, encrypted).where(USERS.ID.eq(id)).returning()
                .fetchOne();
        return new User(record.getId(), record.getName(), record.getEmail(), record.getPermission());
    }
}