Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added my-app/.mvn/jvm.config
Empty file.
Empty file added my-app/.mvn/maven.config
Empty file.
95 changes: 95 additions & 0 deletions my-app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.loogibot.lil</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>

<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
82 changes: 82 additions & 0 deletions my-app/src/main/java/com/loogibot/lil/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.loogibot.lil;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import com.loogibot.lil.data.dao.CustomerDao;
import com.loogibot.lil.data.dao.ServiceDao;
import com.loogibot.lil.data.dao.SimpleProductDao;
import com.loogibot.lil.data.entity.Customer;
import com.loogibot.lil.data.entity.Service;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
ServiceDao serviceDao = new ServiceDao();
List<Service> services = serviceDao.getAll();
System.out.println("**** SERVICES BELOW ****");
System.out.println("\n *** GET_ALL ***");
services.forEach(System.out::println);

Optional<Service> service = serviceDao.getOne(services.get(0).getServiceId());
System.out.println("\n *** GET ONE ***\n" + service.get());

Service newService = new Service();
newService.setName("FooBarBaz" + System.currentTimeMillis());
newService.setPrice(new BigDecimal(4.35));
newService = serviceDao.create(newService);
System.out.println("\n *** CREATE ***\n" + newService);

newService.setPrice(new BigDecimal(13.45));
newService = serviceDao.update(newService);
System.out.println("\n *** UPDATE *** \n" + newService);

serviceDao.delete(newService.getServiceId());
System.out.println("\n *** DELETE *** \n");

//

CustomerDao customerDao = new CustomerDao();
List<Customer> customers = customerDao.getAll();
System.out.println("**** CUSTOMERS BELOW ****");
System.out.println("\n *** GET_ALL ***");
customers.forEach(System.out::println);

Optional<Customer> customer = customerDao.getOne(customers.get(0).getCustomerId());
System.out.println("\n *** GET ONE ***\n" + customer.get());

Customer newCustomer = new Customer();
newCustomer.setFirst_name("Joey" + System.currentTimeMillis());
newCustomer.setLast_name("Castillo" + System.currentTimeMillis());
newCustomer.setEmail("Joey@Castillo.com" + System.currentTimeMillis());
newCustomer.setPhone("5552746" + System.currentTimeMillis());

newCustomer = customerDao.create(newCustomer);
System.out.println("\n *** CREATE ***\n" + newCustomer);

newCustomer = customerDao.update(newCustomer);
System.out.println("\n *** UPDATE *** \n" + newCustomer);

customerDao.delete(newCustomer.getCustomerId());
System.out.println("\n *** DELETE *** \n");

System.out.println("**** Simple Product BELOW ****");
SimpleProductDao spdao = new SimpleProductDao();
UUID productId = spdao.createProduct("Zafa", new BigDecimal(83.76), "Jaloo");
System.out.println(productId);

System.out.println("**** LIMIT ****");
serviceDao.getAllLimit(2).forEach(System.out::println);

System.out.println("**** PAGED ****");

for(int i = 1; i<11; i++){
System.out.println("Page numbe: " + i);
customerDao.getAllPaged(i, 10).forEach(System.out::println);
}
}
}
165 changes: 165 additions & 0 deletions my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.loogibot.lil.data.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;

import com.loogibot.lil.data.entity.Customer;
import com.loogibot.lil.data.util.DatabaseUtils;

public class CustomerDao implements Dao<Customer, UUID> {

private static final Logger LOGGER = Logger.getLogger(CustomerDao.class.getName());
private static final String GET_ALL = "select customer_id, first_name, last_name, email, phone from wisdom.customers";
private static final String GET_BY_ID = "select customer_id, last_name, first_name, email, phone from wisdom.customers where customer_id = ?";
private static final String CREATE = "insert into wisdom.customers (customer_id, last_name, first_name, email, phone) values (?,?,?,?,?)";
private static final String UPDATE = "update wisdom.customers set first_name = ?, last_name = ?, email = ?, phone = ? where customer_id = ?";
private static final String DELETE = "delete from wisdom.customers where customer_id = ?";
private static final String GET_ALL_PAGED = "select customer_id, first_name, last_name, email, phone from wisdom.customers order by last_name, first_name, email, phone LIMIT ? OFFSET ?";


public List<Customer> getAllPaged(int pageNumber, int limit){
List<Customer> customers = new ArrayList<>();
Connection connection = DatabaseUtils.getConnection();
int offset = ((pageNumber - 1) * limit);
try(PreparedStatement statement = connection.prepareStatement(GET_ALL_PAGED)){
statement.setInt(1, limit);
statement.setInt(2, offset);
ResultSet rs = statement.executeQuery();
customers = this.processResultSet(rs);
rs.close();
} catch (SQLException e){
DatabaseUtils.handleSqlException("CustomerDao.getAllPaged", e, LOGGER);
}
return customers;
}

@Override
public List<Customer> getAll() {
List<Customer> customers = new ArrayList<>();
Connection connection = DatabaseUtils.getConnection();
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(GET_ALL);
customers = this.processResultSet(rs);
rs.close();
} catch (SQLException e) {
DatabaseUtils.handleSqlException("CustomerDao.getAll", e, LOGGER);
}
return customers;
}

@Override
public Customer create(Customer entity) {
UUID customerId = UUID.randomUUID();
Connection connection = DatabaseUtils.getConnection();
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(CREATE);
statement.setObject(1, customerId);
statement.setString(2, entity.getFirst_name());
statement.setString(3, entity.getLast_name());
statement.setString(4, entity.getEmail());
statement.setString(5, entity.getPhone());
statement.execute();
connection.commit();
statement.close();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException sqle) {
DatabaseUtils.handleSqlException("CustomerDao.create.rollback", sqle, LOGGER);
}
DatabaseUtils.handleSqlException("CustomerDao.create", e, LOGGER);
}
Optional<Customer> customer = this.getOne(customerId);
if (!customer.isPresent()) {
return null;
}
return customer.get();
}

@Override
public Optional<Customer> getOne(UUID id) {
try (PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)) {
statement.setObject(1, id);
ResultSet rs = statement.executeQuery();
List<Customer> customers = this.processResultSet(rs);
if (customers.isEmpty()) {
return Optional.empty();
}
return Optional.of(customers.get(0));
} catch (SQLException e) {
// TODO: handle exception
DatabaseUtils.handleSqlException("CustomerDao.getOne", e, LOGGER);
}
return Optional.empty();
}

@Override
public Customer update(Customer entity) {
Connection connection = DatabaseUtils.getConnection();
try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(UPDATE);
// statement.setObject(1, entity.getCustomerId());
statement.setString(1, entity.getFirst_name());
statement.setString(2, entity.getLast_name());
statement.setString(3, entity.getEmail());
statement.setString(4, entity.getPhone());
statement.setObject(5, entity.getCustomerId());
statement.execute();
connection.commit();
statement.close();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException sqle) {
DatabaseUtils.handleSqlException("CustomerDao.update.rollback", sqle, LOGGER);
}
DatabaseUtils.handleSqlException("CustomerDao.update", e, LOGGER);
}
return this.getOne(entity.getCustomerId()).get();
}

@Override
public void delete(UUID id) {
Connection connection = DatabaseUtils.getConnection();

try {
connection.setAutoCommit(false);
PreparedStatement statement = connection.prepareStatement(DELETE);
statement.setObject(1, id);
statement.execute();
connection.commit();
statement.close();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException sqle) {
DatabaseUtils.handleSqlException("CustomerDao.delete.rollback", sqle, LOGGER);
}
DatabaseUtils.handleSqlException("CustomerDao.delete", e, LOGGER);
}
}

private List<Customer> processResultSet(ResultSet rs) throws SQLException {
List<Customer> customers = new ArrayList<>();
while (rs.next()) {
Customer customer = new Customer();
customer.setCustomerId((UUID) rs.getObject("customer_id"));
customer.setFirst_name(rs.getString("first_name"));
customer.setLast_name(rs.getString("last_name"));
customer.setEmail(rs.getString("email"));
customer.setPhone(rs.getString("phone"));
customers.add(customer);
}
return customers;
}
}
12 changes: 12 additions & 0 deletions my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.loogibot.lil.data.dao;
import java.util.UUID;
import java.util.Optional;
import java.util.List;

public interface Dao <T, Id extends UUID> {
List<T> getAll();
T create(T entity);
Optional<T> getOne(Id id);
T update(T entity);
void delete(Id id);
}
Loading