Skip to content

Commit 248fcd4

Browse files
committed
add student done ✅
1 parent bfd527d commit 248fcd4

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
<artifactId>spring-boot-starter-data-jpa</artifactId>
3232
</dependency>
3333

34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<version>1.18.22</version>
38+
<scope>provided</scope>
39+
</dependency>
40+
41+
3442

3543
<dependency>
3644
<groupId>org.springframework.boot</groupId>

src/main/java/com/gdsc/springbootworkshop/entites/Student.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.gdsc.springbootworkshop.entites;
22

33

4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
49
import javax.persistence.*;
510

611
@Entity
12+
@Setter
13+
@Getter
14+
@NoArgsConstructor
715
public class Student {
816

917

@@ -19,17 +27,6 @@ public class Student {
1927
@Enumerated(EnumType.STRING)
2028
private GENDER gender;
2129

22-
public Long getId() {
23-
return idStudent;
24-
}
25-
26-
public void setId(Long id) {
27-
this.idStudent = id;
28-
}
29-
30-
public Student() {
31-
}
32-
3330
public Student(String firstName, String lastName, String email, GENDER gender) {
3431
this.firstName = firstName;
3532
this.lastName = lastName;

src/main/java/com/gdsc/springbootworkshop/repositories/StudentRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
@Repository
88
public interface StudentRepository extends JpaRepository<Student, Long> {
9+
10+
Student findByEmail(String email);
911
}

src/main/java/com/gdsc/springbootworkshop/services/IStudentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public interface IStudentService {
88

99
List<Student> getAllStudents();
10-
Student addStudent(Student student);
10+
Student addStudent(Student student) throws Exception;
1111
Student updateStudent(Long id,Student student);
1212
void deleteStudent(Long id);
1313
Student findById(Long id);

src/main/java/com/gdsc/springbootworkshop/services/StudentService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
package com.gdsc.springbootworkshop.services;
22

33
import com.gdsc.springbootworkshop.entites.Student;
4+
import com.gdsc.springbootworkshop.repositories.StudentRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.stereotype.Service;
57

68
import java.util.List;
79

810
@Service
911
public class StudentService implements IStudentService {
1012

13+
@Autowired
14+
private StudentRepository studentRepository;
1115

1216
@Override
1317
public List<Student> getAllStudents() {
14-
return null;
18+
return studentRepository.findAll();
1519
}
1620

1721
@Override
18-
public Student addStudent(Student student) {
19-
return null;
22+
public Student addStudent(Student student) throws Exception {
23+
if(studentRepository.findByEmail(student.getEmail()) != null)
24+
throw new Exception("Student already exists");
25+
return studentRepository.save(student);
2026
}
2127

2228
@Override

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ spring.datasource.password=
99
## JPA / HIBERNATE ###
1010
spring.jpa.show-sql=true
1111
spring.jpa.generate-ddl=true
12-
spring.jpa.hibernate.ddl-auto=update
12+
spring.jpa.hibernate.ddl-auto=create

0 commit comments

Comments
 (0)