// 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/repository/UserRepository.java
// @JdbiRepository
// @InTransaction
// public abstract class UserRepository extends Crud<User> {
// 
//     // have to use field injection because class is still used by dbi (which is no aware of guice) for proxy creation
//     @Inject
//     private RandomNameGenerator generator;
// 
//     // sample of hybrid method in repository, using injected service
//     public User createRandomUser() {
//         final User user = new User();
//         user.setName(generator.generateName());
//         save(user);
//         return user;
//     }
// 
//     @Override
//     @SqlUpdate("insert into users (name, version) values (:name, :version)")
//     @GetGeneratedKeys
//     public abstract long insert(@UserBind User entry);
// 
//     @SqlUpdate("update users set version=:version, name=:name where id=:id and version=:version - 1")
//     @Override
//     public abstract int update(@UserBind User entry);
// 
//     @SqlQuery("select * from users")
//     public abstract List<User> findAll();
// 
//     @SqlQuery("select * from users where name = :name")
//     public abstract User findByName(@Bind("name") String name);
// }


import com.google.common.base.Preconditions;
import ru.vyarus.dropwizard.guice.examples.model.User;
import ru.vyarus.dropwizard.guice.examples.repository.UserRepository;
import javax.inject.Inject;
import javax.ws.rs.*;
import java.util.List;
package ru.vyarus.dropwizard.guice.examples.resource;



/**
 * @author Vyacheslav Rusakov
 * @since 09.12.2016
 */
@Path("/users")
@Produces("application/json")
public class UserResource {

    private final UserRepository repository;

    @Inject
    public UserResource(final UserRepository repository) {
        this.repository = repository;
    }

    @POST
    @Path("/")
    public User create(String name) {
        Preconditions.checkState(repository.findByName(name) == null,
                "User with name %s already exists", name);
// 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/repository/UserRepository.java
// @JdbiRepository
// @InTransaction
// public abstract class UserRepository extends Crud<User> {
// 
//     // have to use field injection because class is still used by dbi (which is no aware of guice) for proxy creation
//     @Inject
//     private RandomNameGenerator generator;
// 
//     // sample of hybrid method in repository, using injected service
//     public User createRandomUser() {
//         final User user = new User();
//         user.setName(generator.generateName());
//         save(user);
//         return user;
//     }
// 
//     @Override
//     @SqlUpdate("insert into users (name, version) values (:name, :version)")
//     @GetGeneratedKeys
//     public abstract long insert(@UserBind User entry);
// 
//     @SqlUpdate("update users set version=:version, name=:name where id=:id and version=:version - 1")
//     @Override
//     public abstract int update(@UserBind User entry);
// 
//     @SqlQuery("select * from users")
//     public abstract List<User> findAll();
// 
//     @SqlQuery("select * from users where name = :name")
//     public abstract User findByName(@Bind("name") String name);
// }


// Path: ext-jdbi/src/main/java/ru/vyarus/dropwizard/guice/examples/resource/UserResource.java
import com.google.common.base.Preconditions;
import ru.vyarus.dropwizard.guice.examples.model.User;
import ru.vyarus.dropwizard.guice.examples.repository.UserRepository;
import javax.inject.Inject;
import javax.ws.rs.*;
import java.util.List;

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



/**
 * @author Vyacheslav Rusakov
 * @since 09.12.2016
 */
@Path("/users")
@Produces("application/json")
public class UserResource {

    private final UserRepository repository;

    @Inject
    public UserResource(final UserRepository repository) {
        this.repository = repository;
    }

    @POST
    @Path("/")
    public User create(String name) {
        Preconditions.checkState(repository.findByName(name) == null,
                "User with name %s already exists", name);
        User user = new User();