// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/api/model/Service.java
// @EqualsAndHashCode @ToString
// public class Service {
// 
//     @JsonProperty
//     @Getter private final String name;
// 
//     @JsonProperty
//     @Getter private final String baseUrl;
// 
//     @JsonProperty
//     @Getter private final Credentials credentials;
// 
//     public Service(@JsonProperty("name") final String name,
//                    @JsonProperty("baseUrl") final String baseUrl,
//                    @JsonProperty("credentials") final Credentials credentials) {
//         Preconditions.checkNotNull(name, "the service name may not be null");
//         Preconditions.checkArgument(!name.trim().isEmpty(), "the service name may not be empty");
// 
//         Preconditions.checkNotNull(baseUrl, "the base URL may not be null");
//         Preconditions.checkArgument(!baseUrl.trim().isEmpty(), "the base URL may not be empty");
// 
//         this.name = name;
//         this.baseUrl = baseUrl;
//         this.credentials = credentials;
//     }
// 
//     public String getUsername() {
//         return credentials.getUsername();
//     }
// 
//     public String getPassword() {
//         return credentials.getPassword();
//     }
// 
// }
// 
// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/core/auth/ServiceRegistryCaller.java
// @EqualsAndHashCode @ToString
// public class ServiceRegistryCaller {
// 
//     @Getter private final String name;
// 
//     public ServiceRegistryCaller(final String name) {
//         this.name = name;
//     }
// 
// }
// 
// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/jdbi/ServiceDAO.java
// @RegisterMapper(ServiceMapper.class)
// public interface ServiceDAO {
// 
//     @SqlUpdate("insert into service (name, baseUrl, username, password) values (:name, :baseUrl, :username, :password)")
//     void insert(@BindBean Service service);
// 
//     @SqlUpdate("update service set name=:name, username=:username, password=:password where baseUrl = :baseUrl")
//     void update(@BindBean Service service);
// 
//     @SqlQuery("select name, baseUrl, username, password from service where baseUrl = :baseUrl")
//     Service findByBaseUrl(@Bind("baseUrl") String baseUrl);
// 
//     @SqlQuery("select name, baseUrl, username, password from service where name = :name")
//     List<Service> findByName(@Bind("name") String name);
// }


import io.dropwizard.auth.Auth;
import lombok.extern.slf4j.Slf4j;
import org.bitionaire.elbombillo.registry.api.model.Service;
import org.bitionaire.elbombillo.registry.core.auth.ServiceRegistryCaller;
import org.bitionaire.elbombillo.registry.jdbi.ServiceDAO;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
package org.bitionaire.elbombillo.registry.resources;



@Path("/services")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class ServiceResource {

    private final ServiceDAO serviceDAO;

    public ServiceResource(final ServiceDAO serviceDAO) {
        this.serviceDAO = serviceDAO;
    }

    @GET
    public List<Service> services(@Auth final ServiceRegistryCaller caller, @QueryParam("name") final String serviceName) {
        return serviceDAO.findByName(serviceName);
    }

    @POST
    public void register(@Auth final ServiceRegistryCaller caller, final Service service) {
        log.debug("registry called with service {}", service);
        final Service foundService = serviceDAO.findByBaseUrl(service.getBaseUrl());
        if (foundService == null) {
            serviceDAO.insert(service);
            log.info("added new service {}", service);
        } else {
            if (!service.getName().equals(foundService.getName()) || !service.getCredentials().equals(foundService.getCredentials())) {
                serviceDAO.update(service);
// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/api/model/Service.java
// @EqualsAndHashCode @ToString
// public class Service {
// 
//     @JsonProperty
//     @Getter private final String name;
// 
//     @JsonProperty
//     @Getter private final String baseUrl;
// 
//     @JsonProperty
//     @Getter private final Credentials credentials;
// 
//     public Service(@JsonProperty("name") final String name,
//                    @JsonProperty("baseUrl") final String baseUrl,
//                    @JsonProperty("credentials") final Credentials credentials) {
//         Preconditions.checkNotNull(name, "the service name may not be null");
//         Preconditions.checkArgument(!name.trim().isEmpty(), "the service name may not be empty");
// 
//         Preconditions.checkNotNull(baseUrl, "the base URL may not be null");
//         Preconditions.checkArgument(!baseUrl.trim().isEmpty(), "the base URL may not be empty");
// 
//         this.name = name;
//         this.baseUrl = baseUrl;
//         this.credentials = credentials;
//     }
// 
//     public String getUsername() {
//         return credentials.getUsername();
//     }
// 
//     public String getPassword() {
//         return credentials.getPassword();
//     }
// 
// }
// 
// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/core/auth/ServiceRegistryCaller.java
// @EqualsAndHashCode @ToString
// public class ServiceRegistryCaller {
// 
//     @Getter private final String name;
// 
//     public ServiceRegistryCaller(final String name) {
//         this.name = name;
//     }
// 
// }
// 
// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/jdbi/ServiceDAO.java
// @RegisterMapper(ServiceMapper.class)
// public interface ServiceDAO {
// 
//     @SqlUpdate("insert into service (name, baseUrl, username, password) values (:name, :baseUrl, :username, :password)")
//     void insert(@BindBean Service service);
// 
//     @SqlUpdate("update service set name=:name, username=:username, password=:password where baseUrl = :baseUrl")
//     void update(@BindBean Service service);
// 
//     @SqlQuery("select name, baseUrl, username, password from service where baseUrl = :baseUrl")
//     Service findByBaseUrl(@Bind("baseUrl") String baseUrl);
// 
//     @SqlQuery("select name, baseUrl, username, password from service where name = :name")
//     List<Service> findByName(@Bind("name") String name);
// }


// Path: el-bombillo-service-registry/src/main/java/org/bitionaire/elbombillo/registry/resources/ServiceResource.java
import io.dropwizard.auth.Auth;
import lombok.extern.slf4j.Slf4j;
import org.bitionaire.elbombillo.registry.api.model.Service;
import org.bitionaire.elbombillo.registry.core.auth.ServiceRegistryCaller;
import org.bitionaire.elbombillo.registry.jdbi.ServiceDAO;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

package org.bitionaire.elbombillo.registry.resources;



@Path("/services")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class ServiceResource {

    private final ServiceDAO serviceDAO;

    public ServiceResource(final ServiceDAO serviceDAO) {
        this.serviceDAO = serviceDAO;
    }

    @GET
    public List<Service> services(@Auth final ServiceRegistryCaller caller, @QueryParam("name") final String serviceName) {
        return serviceDAO.findByName(serviceName);
    }

    @POST
    public void register(@Auth final ServiceRegistryCaller caller, final Service service) {
        log.debug("registry called with service {}", service);
        final Service foundService = serviceDAO.findByBaseUrl(service.getBaseUrl());
        if (foundService == null) {
            serviceDAO.insert(service);
            log.info("added new service {}", service);
        } else {
            if (!service.getName().equals(foundService.getName()) || !service.getCredentials().equals(foundService.getCredentials())) {
                serviceDAO.update(service);
                log.info("updated service {}", service);