Skip to content
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

Kyle Burchett - JPA SBA #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
34 changes: 31 additions & 3 deletions src/main/java/com/github/perscholas/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

/**
* Created by leon on 2/18/2020.
Expand Down Expand Up @@ -48,10 +50,10 @@ public Connection getDatabaseEngineConnection() {

@Override
public void create() {
String sqlStatement = null; // TODO - define statement
String sqlStatement = "CREATE DATABASE IF NOT EXISTS " + getDatabaseName() + ";";
String info;
try {
// TODO - execute statement
executeStatement(sqlStatement);
info = "Successfully executed statement `%s`.";
} catch (Exception sqlException) {
info = "Failed to executed statement `%s`.";
Expand All @@ -61,18 +63,44 @@ public void create() {

@Override
public void drop() {
String sqlStatement = "DROP DATABASE IF EXISTS " + getDatabaseName() + ";";
executeStatement(sqlStatement);
}

@Override
public void use() {
String sqlStatement = "USE " + getDatabaseName() + ";";
executeStatement(sqlStatement);
}

@Override
public void executeStatement(String sqlStatement) {
try {
Statement statement = getScrollableStatement(getDatabaseConnection());
statement.execute(sqlStatement);
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
public ResultSet executeQuery(String sqlQuery) {
return null;
try {
Statement statement = getScrollableStatement(getDatabaseConnection());
return statement.executeQuery(sqlQuery);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public Statement getScrollableStatement(Connection connection) {
int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
try {
return connection.createStatement(resultSetType, resultSetConcurrency);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/github/perscholas/JdbcConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public static void initialize() {
executeSqlFile("courses.populate-table.sql");
executeSqlFile("students.create-table.sql");
executeSqlFile("students.populate-table.sql");
executeSqlFile("studentcourse.create-table.sql");
executeSqlFile("studentcourse.populate-table.sql");
System.out.println("Initialized successfully");
}

private static void executeSqlFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.perscholas.dao.StudentDao;
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.service.StudentService;
import com.github.perscholas.utils.IOConsole;

import java.util.List;
Expand All @@ -15,7 +16,7 @@ public void run() {
do {
smsDashboardInput = getSchoolManagementSystemDashboardInput();
if ("login".equals(smsDashboardInput)) {
StudentDao studentService = null; // TODO - Instantiate `StudentDao` child
StudentDao studentService = new StudentService(DatabaseConnection.MANAGEMENT_SYSTEM); // TODO - Instantiate `StudentDao` child
String studentEmail = console.getStringInput("Enter your email:");
String studentPassword = console.getStringInput("Enter your password:");
Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword);
Expand Down
57 changes: 55 additions & 2 deletions src/main/java/com/github/perscholas/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
package com.github.perscholas.model;

// TODO - Annotate and Implement respective interface and define behaviors
public class Course {
import javax.persistence.*;

@Entity
@Table(name = "course")
public class Course implements CourseInterface{

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name = "name", nullable = false, length = 50)
private String name;

@Column(name = "instructor", nullable = false, length = 50)
private String instructor;

public Course() {
}

public Course(int id, String name, String instructor) {
this.id = id;
this.name = name;
this.instructor = instructor;
}

@Override
public Integer getId() {
return id;
}

@Override
public String getName() {
return name;
}

@Override
public String getInstructor() {
return instructor;
}

@Override
public void setId(Integer id) {
this.id = id;
}

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

@Override
public void setInstructor(String instructor) {
this.instructor = instructor;
}
}
57 changes: 55 additions & 2 deletions src/main/java/com/github/perscholas/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
package com.github.perscholas.model;

// TODO - Annotate and Implement respective interface and define behaviors
public class Student {
import javax.persistence.*;

@Entity
@Table(name = "student")
public class Student implements StudentInterface{

@Id
@Column(name = "email", nullable = false, length = 50)
@GeneratedValue(strategy = GenerationType.AUTO)
private String email;

@Column(name = "name", nullable = false, length = 50)
private String name;

@Column(name = "password", nullable = false, length = 50)
private String password;

public Student() {
}

public Student(String email, String name, String password) {
this.email = email;
this.name = name;
this.password = password;
}

@Override
public String getEmail() {
return email;
}

@Override
public void setEmail(String email) {
this.email = email;
}

@Override
public String getName() {
return name;
}

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

@Override
public String getPassword() {
return password;
}

@Override
public void setPassword(String password) {
this.password = password;
}
}
41 changes: 40 additions & 1 deletion src/main/java/com/github/perscholas/service/CourseService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package com.github.perscholas.service;

import com.github.perscholas.DatabaseConnection;
import com.github.perscholas.dao.CourseDao;
import com.github.perscholas.model.Course;
import com.github.perscholas.model.CourseInterface;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

// TODO - Implement respective DAO interface
public class CourseService {
public class CourseService implements CourseDao {

private final DatabaseConnection dbc;

public CourseService(DatabaseConnection dbc) {
this.dbc = dbc;
}

public CourseService() {
this(DatabaseConnection.UAT);
}

@Override
public List<CourseInterface> getAllCourses() {
List<CourseInterface> courseList = new ArrayList<>();
ResultSet resultSet = dbc.executeQuery("SELECT * FROM Course;");

try {
do {
courseList.add(new Course(resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getString("instructor")));

} while (Objects.requireNonNull(resultSet).next());
} catch (SQLException e) {
throw new RuntimeException(e);
}
return courseList;
}
}
29 changes: 22 additions & 7 deletions src/main/java/com/github/perscholas/service/StudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.github.perscholas.model.CourseInterface;
import com.github.perscholas.model.Student;
import com.github.perscholas.model.StudentInterface;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

// TODO - Implement respective DAO interface
public class StudentService implements StudentDao {
Expand All @@ -25,22 +25,37 @@ public StudentService() {

@Override
public List<StudentInterface> getAllStudents() {
ResultSet resultSet = dbc.executeQuery("SELECT * FROM students");
List<StudentInterface> studentList = new ArrayList<>();
ResultSet resultSet = dbc.executeQuery("SELECT * FROM Student;");

try {
return null; // TODO - Parse `List<StudentInterface>` from `resultSet`
} catch(Exception e) {
throw new Error(e);
do {
studentList.add(new Student(resultSet.getString("email"),
resultSet.getString("name"),
resultSet.getString("password")));

} while (Objects.requireNonNull(resultSet).next());
} catch (SQLException e) {
throw new RuntimeException(e);
}
return studentList;
}

@Override
public StudentInterface getStudentByEmail(String studentEmail) {
return null;
return getAllStudents().stream()
.filter(studentInterface -> studentInterface.getEmail().equals(studentEmail))
.findAny()
.get();
}

@Override
public Boolean validateStudent(String studentEmail, String password) {
return null;
return getAllStudents().stream()
DavidkBurchett marked this conversation as resolved.
Show resolved Hide resolved
.anyMatch(studentInterface ->
studentInterface.getEmail().equals(studentEmail)
&&
studentInterface.getPassword().equals(password));
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/courses.create-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE table Course(
id int not null primary key,
name varchar(50) not null,
instructor varchar(50) not null
)
6 changes: 6 additions & 0 deletions src/main/resources/studentcourse.create-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE table StudentCourse(
studentEmail varchar(50) not null,
foreign key (studentEmail) references Student(email),
courseId int not null,
foreign key (courseId) references Course(id),
)
6 changes: 6 additions & 0 deletions src/main/resources/studentcourse.populate-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
insert into Student (studentEmail, courseId) values ('[email protected]', 1);
insert into Student (studentEmail, courseId) values ('[email protected]', 2);
insert into Student (studentEmail, courseId) values ('[email protected]', 3);
insert into Student (studentEmail, courseId) values ('[email protected]', 4);
insert into Student (studentEmail, courseId) values ('[email protected]', 2);
insert into Student (studentEmail, courseId) values ('[email protected]', 8);
5 changes: 5 additions & 0 deletions src/main/resources/students.create-table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE table Student(
email varchar(50) not null primary key,
name varchar(50) not null,
password varchar(50) not null
)
3 changes: 2 additions & 1 deletion src/main/resources/students.populate-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ insert into Student (email, name, password) values ('[email protected]', '
insert into Student (email, name, password) values ('[email protected]', 'Holmes Taffley', 'xowtOQ');
insert into Student (email, name, password) values ('[email protected]', 'Alexandra Iannitti', 'TWP4hf5j');
insert into Student (email, name, password) values ('[email protected]', 'Laryssa Jiroudek', 'bXRoLUP');
insert into Student (email, name, password) values ('[email protected]', 'Cahra Jaulme', 'FnVklVgC6r6');
insert into Student (email, name, password) values ('[email protected]', 'Cahra Jaulme', 'FnVklVgC6r6');
insert into Student (email, name, password) values ('[email protected]', 'Kyle Burchett', 'password');