Skip to content

Partial completion of 11 #7

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

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 8 additions & 2 deletions src/main/java/org/example/DJ2Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import io.federecio.dropwizard.swagger.SwaggerBundle;
import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration;
import org.example.controllers.DeliveryEmployeeController;
import org.example.controllers.SalesEmployeeController;
import org.example.controllers.ProjectController;
import org.example.controllers.TestController;
import org.example.controllers.SalesEmployeeController;
import org.example.daos.DeliveryEmployeeDao;
import org.example.daos.SalesEmployeeDao;
import org.example.daos.ProjectDao;
import org.example.daos.TestDao;
import org.example.daos.SalesEmployeeDao;
import org.example.services.DeliveryEmployeeService;
import org.example.services.ProjectService;
import org.example.services.SalesEmployeeService;

import org.example.services.TestService;

public class DJ2Application extends Application<TestConfiguration> {
Expand Down Expand Up @@ -40,6 +44,8 @@ public void run(final TestConfiguration configuration,
.register(new TestController(new TestService(new TestDao())));
environment.jersey().register(new DeliveryEmployeeController(
new DeliveryEmployeeService(new DeliveryEmployeeDao())));
environment.jersey().register(new ProjectController(
new ProjectService(new ProjectDao())));
environment.jersey().register(new SalesEmployeeController(
new SalesEmployeeService(new SalesEmployeeDao())));
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/example/controllers/ProjectController.java
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.ProjectService;

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("Projects API")
@Path("/api/Projects")
public class ProjectController {
ProjectService projectService;

public ProjectController(
final ProjectService projectService) {
this.projectService = projectService;
}

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

import org.example.models.Project;

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 ProjectDao {

public List<Project> getAllProjects()
throws SQLException {
List<Project> projectList = new ArrayList<>();

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

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

while (resultSet.next()) {
Project project = new Project(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getDouble("value"),
resultSet.getInt("clientID"),
resultSet.getBoolean("completed")
);
projectList.add(project);
}
}

return projectList;
}
}

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

public class Project {
private int id;
private String name;
private double value;
private int clientID;
private boolean completed;

public Project(final int id, final String name,
final double value, final int clientID,
final boolean completed) {
this.setId(id);
this.setName(name);
this.setValue(value);
this.setClientID(clientID);
this.setCompleted(completed);
}

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 getValue() {
return value;
}

public void setValue(final double value) {
this.value = value;
}

public int getClientID() {
return clientID;
}

public void setClientID(final int clientID) {
this.clientID = clientID;
}

public boolean isCompleted() {
return completed;
}

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

import org.example.daos.ProjectDao;
import org.example.models.Project;

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

public class ProjectService {

ProjectDao projectDao;

public ProjectService(
final ProjectDao projectDao) {
this.projectDao = projectDao;
}
public List<Project> getAllProjects()
throws SQLException {
return projectDao.getAllProjects();
}


}
Loading