Skip to content

Commit

Permalink
java pizza project #2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
biblelamp committed Jun 30, 2024
1 parent 98fc164 commit 51c4b8c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Extra (additional) component to pizza
*
* @author Sergey Iryupin
* @version 13-May-24
* @version 30-Jun-24
*/
public class ExtComponent {
private Integer id;
Expand All @@ -25,6 +25,14 @@ public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public int getPrice() {
return price;
}

public void update(String name, int price) {
this.name = name;
this.price = price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import pizza.domain.ExtComponent;
import pizza.repository.CrudRepository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

/**
* ExtComponent repository
* Implementation of access methods to the ExtComponent data source
*
* @author Sergey Iryupin
* @version 23-Jun-24
* @version 30-Jun-24
*/
public class ExtComponentDbRepository implements CrudRepository<Integer, ExtComponent> {

Expand All @@ -35,8 +41,31 @@ public ExtComponentDbRepository(String dbName) {
}

@Override
public ExtComponent save(ExtComponent value) {
return null;
public ExtComponent save(ExtComponent component) {
try (Connection connection = DriverManager.getConnection(dbName);
PreparedStatement psi = connection.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
PreparedStatement psu = connection.prepareStatement(SQL_UPDATE)) {
if (component.getId() == null) {
// insert record
psi.setString(1, component.getName());
psi.setInt(2, component.getPrice());
psi.executeUpdate();

ResultSet rs = psi.getGeneratedKeys();
if (rs.next()) {
component.setId(rs.getInt(1));
}
} else {
// update record
psi.setString(1, component.getName());
psi.setInt(2, component.getPrice());
psu.setInt(3, component.getId());
psu.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return component;
}

@Override
Expand Down

0 comments on commit 51c4b8c

Please sign in to comment.