Skip to content

Commit f1fb4d7

Browse files
committed
Subida practica crud con Springboot y BD PostgreSQL
1 parent 144a3b9 commit f1fb4d7

File tree

6 files changed

+140
-24
lines changed

6 files changed

+140
-24
lines changed

src/main/java/com/example/postgresql/student/Student.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.persistence.*;
44
import java.time.LocalDate;
55
import java.time.LocalDateTime;
6+
import java.time.Period;
67

78
@Entity
89
@Table
@@ -22,23 +23,22 @@ public class Student {
2223
private String name;
2324
private String email;
2425
private LocalDate dob;
26+
@Transient
2527
private Integer age;
2628

2729

2830

29-
public Student(Long id, String name, String email, LocalDate dob, Integer age) {
31+
public Student(Long id, String name, String email, LocalDate dob ) {
3032
this.id = id;
3133
this.name = name;
3234
this.email = email;
3335
this.dob = dob;
34-
this.age = age;
3536
}
3637

37-
public Student(String name, String email, LocalDate dob, Integer age) {
38+
public Student(String name, String email, LocalDate dob ) {
3839
this.name = name;
3940
this.email = email;
4041
this.dob = dob;
41-
this.age = age;
4242
}
4343

4444
public Student() {
@@ -78,7 +78,7 @@ public void setDob(LocalDate dob) {
7878
}
7979

8080
public Integer getAge() {
81-
return age;
81+
return Period.between(this.dob, LocalDate.now()).getYears();
8282
}
8383

8484
public void setAge(Integer age) {
@@ -88,11 +88,11 @@ public void setAge(Integer age) {
8888
@Override
8989
public String toString() {
9090
return "Student {" +
91-
"id=" + id +
92-
", name='" + name + '\'' +
93-
", email='" + email + '\'' +
94-
", dob=" + dob +
95-
", age=" + age +
91+
"\nid=" + id +
92+
", \nname='" + name + '\'' +
93+
", \nemail='" + email + '\'' +
94+
", \ndob=" + dob +
95+
", \nage=" + age +
9696
'}';
9797
}
9898
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.postgresql.student;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import java.time.LocalDate;
8+
import java.time.Month;
9+
import java.util.List;
10+
11+
@Configuration
12+
public class StudentConfig {
13+
14+
@Bean
15+
CommandLineRunner commandLineRunner(StudentRepository repository){
16+
return args -> {
17+
Student juancho = new Student( 1L,
18+
"Juancho",
19+
"juanchito@gmail.com",
20+
LocalDate.of(2000, Month.FEBRUARY, 3)
21+
);
22+
23+
Student petrico = new Student(
24+
"petrico",
25+
"petrico@gmail.com",
26+
LocalDate.of(2007, Month.JANUARY, 15)
27+
);
28+
29+
Student uribe = new Student( 3L,
30+
"uribe",
31+
"falsospositivos@gmail.com",
32+
LocalDate.of(2004, Month.MARCH, 9)
33+
);
34+
35+
repository.saveAll(
36+
List.of(juancho, petrico, uribe)
37+
);
38+
};
39+
}
40+
}

src/main/java/com/example/postgresql/student/StudentController.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.example.postgresql.student;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.web.bind.annotation.GetMapping;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RestController;
4+
import org.springframework.web.bind.annotation.*;
75

86
import java.util.List;
97

@@ -23,4 +21,25 @@ public StudentController(StudentService studentService) {
2321
public List<Student> getStudent(){
2422
return studentService.getStudent();
2523
}
24+
25+
@PostMapping
26+
public void registerNewStudent(@RequestBody Student student){
27+
studentService.addNewStudent(student);
28+
}
29+
30+
@DeleteMapping(path = "{studentId}")
31+
public void deleteStudent(@PathVariable("studentId") Long studentId){
32+
studentService.deleteStudent(studentId);
33+
}
34+
35+
@PutMapping(path = "{studentID}")
36+
public void updateStudent(
37+
@PathVariable("studentId") Long studentId,
38+
@RequestParam(required = false) String name,
39+
@RequestParam(required = false) String email){
40+
41+
studentService.updateStudent(studentId, name, email);
42+
}
43+
44+
2645
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.example.postgresql.student;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
45
import org.springframework.stereotype.Repository;
56

7+
import java.util.Optional;
8+
69
@Repository
7-
public interface StudentRepository extends JpaRepository<Student, Long> {
10+
public interface StudentRepository
11+
extends JpaRepository<Student, Long> {
812

913

14+
//SELECT * FROM student WHERE email = ?
15+
@Query("SELECT s FROM Student s WHERE s.email = ?1")
16+
Optional<Student> findStudentByEmail(String email);
1017

1118
}
Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,73 @@
11
package com.example.postgresql.student;
22

3-
import org.springframework.stereotype.Component;
3+
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Transactional;
56

67
import java.time.LocalDate;
78
import java.time.Month;
89
import java.util.List;
10+
import java.util.Objects;
11+
import java.util.Optional;
912

1013
@Service
1114
public class StudentService {
1215

16+
private final StudentRepository studentRepository;
17+
18+
@Autowired
19+
public StudentService(StudentRepository studentRepository) {
20+
this.studentRepository = studentRepository;
21+
}
22+
1323

1424
public List<Student> getStudent(){
15-
return List.of(
16-
new Student(
17-
1L,
18-
"Juancho",
19-
"juanchito@gmail.com",
20-
LocalDate.of(2000, Month.FEBRUARY, 3),
21-
21
22-
)
23-
);
25+
return studentRepository.findAll();
26+
}
27+
28+
public void addNewStudent(Student student) {
29+
Optional<Student> studentOptional = studentRepository.findStudentByEmail(student.getEmail());
30+
31+
if (studentOptional.isPresent()) {
32+
throw new IllegalStateException("email taken");
33+
}
34+
studentRepository.save(student);
2435
}
36+
37+
public void deleteStudent(Long studentId) {
38+
boolean exists = studentRepository.existsById(studentId);
39+
if(!exists) throw new IllegalStateException("student with id: "+ studentId + " does not exits");
40+
41+
studentRepository.deleteById(studentId);
42+
}
43+
44+
@Transactional
45+
public void updateStudent(Long studentId, String name, String email){
46+
47+
Student student = studentRepository.findById(studentId)
48+
.orElseThrow(() -> new IllegalStateException(
49+
"student with id --> [ "+studentId+ " ] does not exits"
50+
));
51+
52+
if (name != null && name.length() > 0 &&
53+
!Objects.equals(student.getName(), name)) {
54+
55+
student.setName(name);
56+
}
57+
58+
if (email != null && email.length() > 0 &&
59+
!Objects.equals(student.getEmail(), email)) {
60+
61+
Optional<Student> studentOptional = studentRepository.findStudentByEmail(email);
62+
63+
if (studentOptional.isPresent()) {
64+
throw new IllegalStateException("email taken");
65+
}
66+
67+
student.setEmail(email);
68+
69+
}
70+
71+
}
72+
2573
}

src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ spring.jpa.hibernate.ddl-auto=create-drop
55
spring.jpa.show-sql=true
66
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
77
spring.jpa.properties.hibernate.format_sql=true
8+
9+
server.error.include-message=always

0 commit comments

Comments
 (0)