refactor student and logic

This commit is contained in:
Patrick
2025-10-29 09:53:42 +01:00
parent 599823deeb
commit d6eea4cc57
10 changed files with 130 additions and 275 deletions

View File

@ -24,13 +24,19 @@
4. _Swagger config_ manually
5. _Dockerfile_ manually
_env (only during image build & deployment)_ manually
- add changes `git add .`
- commit changes `git commit -m "Set version to v<major>.<minor>.<patch>"`
- push chnages `git push --set-upstream origin release/v<major>.<minor>.<patch>`
- Create and review the release pull request, then MERGE the release branch `git merge --no-ff release/v<major>.<minor>.<patch> -m "Merge release v<major>.<minor>.<patch>"`.
- checkout and update main/master branch `git checkout main/master` & `git pull origin main/master`
- add changes
`git add .`
- commit changes
`git commit -m "Set version to v<major>.<minor>.<patch>"`
- push changes
`git push --set-upstream origin release/v<major>.<minor>.<patch>`
- Create and review the release pull request, then MERGE the release branch
`git merge --no-ff release/v<major>.<minor>.<patch> -m "Merge release v<major>.<minor>.<patch>"`.
- checkout and update main/master branch
`git checkout main/master` & `git pull origin main/master`
- Create tag with the version number (format is very important) `git tag v<major>.<minor>.<patch> -m "Release v<major>.<minor>.<patch>"`
- Push tag `git push origin v<major>.<minor>.<patch>`
- Push tag
`git push origin v<major>.<minor>.<patch>`
- Create a GitHub Release from the tag - (from UI)
# 2. PATCH (hotfix) RELEASES FOR MY_APP

View File

@ -45,6 +45,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,13 +1,7 @@
package com.ebrains.cruddemo;
import com.ebrains.cruddemo.dao.StudentDAO;
import com.ebrains.cruddemo.entity.Student;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.List;
@SpringBootApplication
public class CruddemoApplication {
@ -17,108 +11,4 @@ public class CruddemoApplication {
SpringApplication.run(CruddemoApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(StudentDAO studentDAO) {
return runner ->{
createMultipleStudent(studentDAO);
//queryForStudent(studentDAO);
//queryForStudentByLastName(studentDAO);
//updateStudent(studentDAO);
//deleteStudent(studentDAO);
//deleteAllStudent(studentDAO);
};
}
private void deleteAllStudent(StudentDAO studentDAO) {
int numOfRowDeleted = studentDAO.deleteAll();
System.out.println("Number of row deleted: " + numOfRowDeleted);
}
private void deleteStudent(StudentDAO studentDAO) {
Student student = studentDAO.findById(3);
System.out.println("Student to be deleted: "+student);
studentDAO.delete(3);
}
private void updateStudent(StudentDAO studentDAO) {
//retrieve the student base on id
Student student = studentDAO.findById(1);
//change the lastname
System.out.println("updating the student ...");
student.setLastName("Lobby");
//update the student
studentDAO.update(student);
//Display the updated student
System.out.println("student updated: "+student);
}
private void queryForStudentByLastName(StudentDAO studentDAO) {
//get the list of student
List<Student> students = studentDAO.findByLastName("Doe");
for (Student student : students) {
System.out.println(student);
}
}
private void queryForStudent(StudentDAO studentDAO) {
//get the list of student
List<Student> students = studentDAO.findAll();
//display the student
for (Student student : students) {
System.out.println(student);
}
}
private void createStudent(StudentDAO studentDAO) {
System.out.println("Creating student ....");
Student student = new Student("paul","okeke","paul@gmail.com");
//saving the student object
System.out.println("saving student ....");
studentDAO.save(student);
System.out.println("Student saved successfully.Generated Id: " + student.getId());
}
private void createMultipleStudent(StudentDAO studentDAO) {
System.out.println("Creating student ....");
Student student = new Student("John","smith","smith@gmail.com");
Student student1 = new Student("Mary","Doe","doe@gmail.com");
Student student2= new Student("Eric","onu","onu@gmail.com");
//saving the multiple student object
studentDAO.save(student);
studentDAO.save(student1);
studentDAO.save(student2);
System.out.println("Students saved successfully");
}
private void readStudent(StudentDAO studentDAO) {
//create a student object
System.out.println("Creating student ....");
Student student = new Student("Onowu","Igbo","igbo@gmail.com");
//save the save
studentDAO.save(student);
System.out.println("Student saved successfully");
//display id of the saved student
long theId= student.getId();
System.out.println("saved student successfully.Generated Id: " + theId);
//retrieved the student based on the id ie the primary key
System.out.println("retrieving student saved successfully");
//Student savedStudent= studentDAO.findById(theId);
//display the student
//System.out.println("student retrieved successfully: " + savedStudent);
}
}

View File

@ -1,22 +0,0 @@
package com.ebrains.cruddemo.dao;
import com.ebrains.cruddemo.entity.Student;
import java.util.List;
public interface StudentDAO {
void save(Student student);
Student findById(Integer id);
List<Student> findAll();
List<Student> findByLastName(String lastName);
void update(Student student);
void delete(Integer id);
int deleteAll();
}

View File

@ -1,79 +0,0 @@
package com.ebrains.cruddemo.dao;
import com.ebrains.cruddemo.entity.Student;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Repository
public class StudentDAOImpl implements StudentDAO {
//we define feilds for entity manager
private EntityManager entityManager;
//eject entity manager using constructor injection
public StudentDAOImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
//implement the save method
//this method saves the studnet to the database
@Transactional
@Override
public void save(Student student) {
entityManager.persist(student);
}
@Override
public Student findById(Integer id) {
//Student.class is the entity class
return entityManager.find(Student.class, id);
}
@Override
public List<Student> findAll() {
//create the query
TypedQuery<Student> query = entityManager.createQuery("from Student", Student.class);
//TypedQuery<Student> query = entityManager.createQuery("from Student order by lastName desc", Student.class);
//return the list of student
return query.getResultList();
}
@Override
public List<Student> findByLastName(String lastName) {
//create the query
TypedQuery<Student> query = entityManager.createQuery("from Student where lastName=:theData", Student.class);
//set query parameter
query.setParameter("theData", lastName);
//return the list of student with the last name
return query.getResultList();
}
@Override
@Transactional
public void update(Student student) {
entityManager.merge(student);
}
@Override
@Transactional
public void delete(Integer id) {
//retrieve the student
Student student = entityManager.find(Student.class, id);
//delete the student
entityManager.remove(student);
}
@Override
@Transactional
public int deleteAll() {
return entityManager.createQuery("delete from Student").executeUpdate();
}
}

View File

@ -1,70 +1,20 @@
package com.ebrains.cruddemo.entity;
import jakarta.persistence.*;
import lombok.Data;
@Entity
@Table(name="student")
@Entity(name="student")
@Table
@Data
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@Column
private Long id;
@Column(name = "first_name")
@Column
private String firstName;
@Column(name = "last_name")
@Column
private String lastName;
@Column(name = "email")
@Column
private String email;
public Student() {
}
public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}

View File

@ -0,0 +1,9 @@
package com.ebrains.cruddemo.repository;
import com.ebrains.cruddemo.entity.Student;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
}

View File

@ -0,0 +1,11 @@
package com.ebrains.cruddemo.service;
import com.ebrains.cruddemo.entity.Student;
import org.springframework.stereotype.Service;
@Service
public interface StudentService {
Student getStudentById(long id);
void saveStudent(Student student);
void deleteStudentById(long id);
}

View File

@ -0,0 +1,28 @@
package com.ebrains.cruddemo.serviceimpl;
import com.ebrains.cruddemo.entity.Student;
import com.ebrains.cruddemo.repository.StudentRepository;
import com.ebrains.cruddemo.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@RequiredArgsConstructor
@Component
public class StudentServiceImpl implements StudentService {
private final StudentRepository studentRepository;
@Override
public Student getStudentById(long id) {
return studentRepository.findById(id).orElse(null);
}
@Override
public void saveStudent(Student student) {
studentRepository.save(student);
}
@Override
public void deleteStudentById(long id) {
studentRepository.deleteById(id);
}
}

View File

@ -0,0 +1,58 @@
package com.ebrains.cruddemo.serviceimpl;
import com.ebrains.cruddemo.entity.Student;
import com.ebrains.cruddemo.repository.StudentRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class StudentServiceImplTest {
private final StudentRepository studentRepository = mock(StudentRepository.class);
@InjectMocks
private StudentServiceImpl studentService;
@Test
void getStudentById_found_returnsStudent() {
Student s = new Student();
when(studentRepository.findById(1L)).thenReturn(Optional.of(s));
Student result = studentService.getStudentById(1L);
assertSame(s, result);
verify(studentRepository).findById(1L);
}
@Test
void getStudentById_notFound_returnsNull() {
when(studentRepository.findById(2L)).thenReturn(Optional.empty());
Student result = studentService.getStudentById(2L);
assertNull(result);
verify(studentRepository).findById(2L);
}
@Test
void saveStudent_delegatesToRepository() {
Student s = new Student();
studentService.saveStudent(s);
verify(studentRepository).save(s);
}
@Test
void deleteStudentById_delegatesToRepository() {
studentService.deleteStudentById(3L);
verify(studentRepository).deleteById(3L);
}
}