// Path: ext-jdbi/src/main/java/ru/vyarus/dropwizard/guice/examples/model/User.java
// public class User extends IdEntity {
// 
//     private String name;
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// }
// 
// Path: ext-jdbi/src/main/java/ru/vyarus/dropwizard/guice/examples/service/RandomNameGenerator.java
// public class RandomNameGenerator {
// 
//     public String generateName() {
//         // implementation doesn't matter
//         return "test" + Math.round(1000 * Math.random());
//     }
// }


import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import ru.vyarus.dropwizard.guice.examples.model.User;
import ru.vyarus.dropwizard.guice.examples.repository.mapper.bind.UserBind;
import ru.vyarus.dropwizard.guice.examples.service.RandomNameGenerator;
import ru.vyarus.guicey.jdbi3.installer.repository.JdbiRepository;
import ru.vyarus.guicey.jdbi3.tx.InTransaction;
import javax.inject.Inject;
import java.util.List;
package ru.vyarus.dropwizard.guice.examples.repository;



/**
 * Recognized and installed by special installer, registered by jdbi bundle.
 * Repository will be in singleton scope automatically.
 * <p>
 * {@link ru.vyarus.guicey.jdbi3.tx.InTransaction} declares lowest transaction scope to be able to use repository
 * without any extra tx definition (scope may be enlarged).
 *
 * @author Vyacheslav Rusakov
 * @since 01.11.2018
 */
@JdbiRepository
@InTransaction
public interface UserRepository extends Crud<User> {

    @Inject
    RandomNameGenerator getGenerator();

    // sample of hybrid method in repository, using injected service
    default User createRandomUser() {
        final User user = new User();
        user.setName(getGenerator().generateName());
        save(user);
        return user;
    }

    @Override
    @SqlUpdate("insert into users (name, version) values (:name, :version)")
    @GetGeneratedKeys
// Path: ext-jdbi/src/main/java/ru/vyarus/dropwizard/guice/examples/model/User.java
// public class User extends IdEntity {
// 
//     private String name;
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// }
// 
// Path: ext-jdbi/src/main/java/ru/vyarus/dropwizard/guice/examples/service/RandomNameGenerator.java
// public class RandomNameGenerator {
// 
//     public String generateName() {
//         // implementation doesn't matter
//         return "test" + Math.round(1000 * Math.random());
//     }
// }


// Path: ext-jdbi3/src/main/java/ru/vyarus/dropwizard/guice/examples/repository/UserRepository.java
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import ru.vyarus.dropwizard.guice.examples.model.User;
import ru.vyarus.dropwizard.guice.examples.repository.mapper.bind.UserBind;
import ru.vyarus.dropwizard.guice.examples.service.RandomNameGenerator;
import ru.vyarus.guicey.jdbi3.installer.repository.JdbiRepository;
import ru.vyarus.guicey.jdbi3.tx.InTransaction;
import javax.inject.Inject;
import java.util.List;

package ru.vyarus.dropwizard.guice.examples.repository;



/**
 * Recognized and installed by special installer, registered by jdbi bundle.
 * Repository will be in singleton scope automatically.
 * <p>
 * {@link ru.vyarus.guicey.jdbi3.tx.InTransaction} declares lowest transaction scope to be able to use repository
 * without any extra tx definition (scope may be enlarged).
 *
 * @author Vyacheslav Rusakov
 * @since 01.11.2018
 */
@JdbiRepository
@InTransaction
public interface UserRepository extends Crud<User> {

    @Inject
    RandomNameGenerator getGenerator();

    // sample of hybrid method in repository, using injected service
    default User createRandomUser() {
        final User user = new User();
        user.setName(getGenerator().generateName());
        save(user);
        return user;
    }

    @Override
    @SqlUpdate("insert into users (name, version) values (:name, :version)")
    @GetGeneratedKeys
    long insert(@UserBind User entry);