Skip to content

added delivery employee controller, service and dao to enable getAll … #3

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 1 commit into from
Aug 8, 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
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
</dependencyManagement>
<properties>
<dropwizard.version>2.0.3</dropwizard.version>
<mainClass>org.example.TestApplication</mainClass>
<mainClass>org.example.DJ2Application</mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<dropwizard.version>2.0.3</dropwizard.version>
<mainClass>org.example.TestApplication</mainClass>
<mainClass>org.example.DJ2Application</mainClass>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import io.dropwizard.setup.Environment;
import io.federecio.dropwizard.swagger.SwaggerBundle;
import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration;
import org.example.controllers.DeliveryEmployeeController;
import org.example.controllers.TestController;
import org.example.daos.DeliveryEmployeeDao;
import org.example.daos.TestDao;
import org.example.services.DeliveryEmployeeService;
import org.example.services.TestService;

public class TestApplication extends Application<TestConfiguration> {
public class DJ2Application extends Application<TestConfiguration> {
public static void main(final String[] args) throws Exception {
new TestApplication().run(args);
new DJ2Application().run(args);
}
@Override
public String getName() {
Expand All @@ -32,6 +35,8 @@ public void run(final TestConfiguration configuration,
final Environment environment) {
environment.jersey()
.register(new TestController(new TestService(new TestDao())));
environment.jersey().register(new DeliveryEmployeeController(
new DeliveryEmployeeService(new DeliveryEmployeeDao())));
}

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

import io.swagger.annotations.Api;
import org.example.services.DeliveryEmployeeService;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.SQLException;



@Api("Delivery Employee API")
@Path("/api/DeliveryEmployees")
public class DeliveryEmployeeController {
DeliveryEmployeeService deliveryEmployeeService;

public DeliveryEmployeeController(
final DeliveryEmployeeService deliveryEmployeeService) {
this.deliveryEmployeeService = deliveryEmployeeService;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response testConnection() {
try {
return Response.ok()
.entity(deliveryEmployeeService.getAllDeliveryEmployees())
.build();
} catch (SQLException e) {
return Response.serverError().build();
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/org/example/daos/DeliveryEmployeeDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.example.daos;

import org.example.models.DeliveryEmployee;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeliveryEmployeeDao {

public List<DeliveryEmployee> getAllDeliveryEmployees()
throws SQLException {
List<DeliveryEmployee> deliveryEmployees = new ArrayList<>();

try (Connection connection = DatabaseConnector.getConnection()) {
Statement statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery(
"SELECT * FROM `DeliveryEmployees`;");

while (resultSet.next()) {
DeliveryEmployee deliveryEmployee = new DeliveryEmployee(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getDouble("salary"),
resultSet.getInt("bank_acc"),
resultSet.getString("ni")
);
deliveryEmployees.add(deliveryEmployee);
}
}

return deliveryEmployees;
}
}
59 changes: 59 additions & 0 deletions src/main/java/org/example/models/DeliveryEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.example.models;

public class DeliveryEmployee {
private int id;
private String name;
private double salary;
private int bankAccount;
private String nI;

public DeliveryEmployee(final int id, final String name,
final double salary, final int bankAccount,
final String nI) {
this.setId(id);
this.setName(name);
this.setSalary(salary);
this.setBankAccount(bankAccount);
this.setnI(nI);
}

public int getId() {
return id;
}

public void setId(final int id) {
this.id = id;
}

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 int getBankAccount() {
return bankAccount;
}

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

public String getnI() {
return nI;
}

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

import org.example.daos.DeliveryEmployeeDao;
import org.example.models.DeliveryEmployee;

import java.sql.SQLException;
import java.util.List;

public class DeliveryEmployeeService {

DeliveryEmployeeDao deliveryEmployeeDao;

public DeliveryEmployeeService(
final DeliveryEmployeeDao deliveryEmployeeDao) {
this.deliveryEmployeeDao = deliveryEmployeeDao;
}
public List<DeliveryEmployee> getAllDeliveryEmployees()
throws SQLException {
return deliveryEmployeeDao.getAllDeliveryEmployees();
}


}
Loading