Skip to content

003 create delivery employee #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.example.controllers;

import io.swagger.annotations.Api;
import org.example.exceptions.DoesNotExistException;
import org.example.exceptions.FailedToCreateException;
import org.example.models.DeliveryEmployeeRequest;
import org.example.services.DeliveryEmployeeService;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
Expand All @@ -15,6 +19,7 @@
@Api("Delivery Employee API")
@Path("/api/DeliveryEmployees")
public class DeliveryEmployeeController {

DeliveryEmployeeService deliveryEmployeeService;

public DeliveryEmployeeController(
Expand All @@ -33,4 +38,26 @@ public Response testConnection() {
return Response.serverError().build();
}
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response createProduct(
final DeliveryEmployeeRequest deliveryEmployeeRequest) {
try {
return Response
.status(Response.Status.CREATED)
.entity(deliveryEmployeeService.createDeliveryEmployee(
deliveryEmployeeRequest))
.build();
} catch (SQLException | FailedToCreateException e) {
return Response.serverError().build();
} catch (DoesNotExistException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(e.getMessage()).build();
}
}

}



2 changes: 1 addition & 1 deletion src/main/java/org/example/daos/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class DatabaseConnector {
private DatabaseConnector() { }
public static Connection getConnection() throws SQLException {

if (conn != null && !conn.isClosed()) {
if (conn != null && conn.isValid(0)) {
return conn;
}

Expand Down
39 changes: 34 additions & 5 deletions src/main/java/org/example/daos/DeliveryEmployeeDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.example.daos;

import org.example.models.DeliveryEmployee;
import org.example.models.DeliveryEmployeeRequest;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -26,7 +25,7 @@ public List<DeliveryEmployee> getAllDeliveryEmployees()
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getDouble("salary"),
resultSet.getInt("bank_acc"),
resultSet.getString("bank_acc"),
resultSet.getString("ni")
);
deliveryEmployees.add(deliveryEmployee);
Expand All @@ -35,4 +34,34 @@ public List<DeliveryEmployee> getAllDeliveryEmployees()

return deliveryEmployees;
}

public int createDeliveryEmployee(
final DeliveryEmployeeRequest deliveryEmployeeRequest)
throws SQLException {
Connection c =
DatabaseConnector.getConnection();
String insertStatement =
"INSERT INTO DeliveryEmployees "
+ "(`name`, salary, bank_acc, ni) VALUES (?,?,?,?);";

PreparedStatement st =
c.prepareStatement(
insertStatement, Statement.RETURN_GENERATED_KEYS);

st.setString(1, deliveryEmployeeRequest.getName());
st.setDouble(2, deliveryEmployeeRequest.getSalary());
st.setString(2 + 1, deliveryEmployeeRequest.getBankAccount());
st.setString(2 + 2, deliveryEmployeeRequest.getnI());

st.executeUpdate();

ResultSet rs = st.getGeneratedKeys();

if (rs.next()) {
return rs.getInt(1);
}

return -1;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.exceptions;

public class DoesNotExistException extends Throwable{
public DoesNotExistException(final Entity entity){super(entity.getEntity() + " does not exist");
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/example/exceptions/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.exceptions;

public enum Entity {
DELIVERYEMPLOYEE("Delivery Employee");

private final String entity;

Entity(String entity) {
this.entity = entity;
}

public String getEntity() {
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exceptions;

public class FailedToCreateException extends Throwable {
public FailedToCreateException(Entity entity){
super(entity.getEntity()+ " has failed to create");
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/example/exceptions/InvalidException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.exceptions;

public class InvalidException extends Throwable {

public InvalidException(final Entity entity, final String reason){
super(entity.getEntity() + " is not valid: " + reason);
}

}
8 changes: 4 additions & 4 deletions src/main/java/org/example/models/DeliveryEmployee.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class DeliveryEmployee {
private int id;
private String name;
private double salary;
private int bankAccount;
private String bankAccount;
private String nI;

public DeliveryEmployee(final int id, final String name,
final double salary, final int bankAccount,
final double salary, final String bankAccount,
final String nI) {
this.setId(id);
this.setName(name);
Expand Down Expand Up @@ -41,11 +41,11 @@ public void setSalary(final double salary) {
this.salary = salary;
}

public int getBankAccount() {
public String getBankAccount() {
return bankAccount;
}

public void setBankAccount(final int bankAccount) {
public void setBankAccount(final String bankAccount) {
this.bankAccount = bankAccount;
}

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/example/models/DeliveryEmployeeRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.example.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class DeliveryEmployeeRequest {
private String name;
private double salary;
private String bankAccount;
private String nI;

@JsonCreator
public DeliveryEmployeeRequest(
final @JsonProperty("name") String name,
final @JsonProperty("salary") double salary,
final @JsonProperty("bankAccount") String bankAccount,
final @JsonProperty("nI") String nI) {

this.name = name;
this.salary = salary;
this.bankAccount = bankAccount;
this.nI = nI;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public double getSalary() {
return salary;
}

public void setSalary(final double salary) {
this.salary = salary;
}

public String getBankAccount() {
return bankAccount;
}

public void setBankAccount(final String bankAccount) {
this.bankAccount = bankAccount;
}

public String getnI() {
return nI;
}

public void setnI(final String nI) {
this.nI = nI;
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/example/services/DeliveryEmployeeService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.example.services;

import org.example.daos.DeliveryEmployeeDao;
import org.example.exceptions.DoesNotExistException;
import org.example.exceptions.FailedToCreateException;
import org.example.models.DeliveryEmployee;
import org.example.models.DeliveryEmployeeRequest;
import org.example.exceptions.Entity;

import java.sql.SQLException;
import java.util.List;
Expand All @@ -19,5 +23,18 @@ public List<DeliveryEmployee> getAllDeliveryEmployees()
return deliveryEmployeeDao.getAllDeliveryEmployees();
}

public int createDeliveryEmployee(
final DeliveryEmployeeRequest deliveryEmployeeRequest)
throws FailedToCreateException,
SQLException, DoesNotExistException {
int id = deliveryEmployeeDao
.createDeliveryEmployee(deliveryEmployeeRequest);

if (id == -1) {
throw new FailedToCreateException(Entity.DELIVERYEMPLOYEE);
}
return id;
}


}
Loading