Skip to content

Commit

Permalink
Update v1.0.1
Browse files Browse the repository at this point in the history
-Modificaciones en los servicios de todas las modelos de datos
-Modificaciones en todos los modelos de datos y las clases DTO
-Modificaciones en los controllers solo para adaptarse a los cambios de los servicios
  • Loading branch information
Marc0Franc0 committed Aug 10, 2023
1 parent 956e03c commit 37e27c0
Show file tree
Hide file tree
Showing 23 changed files with 399 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.List;
import java.util.Optional;

import com.api.notemanagementapi.service.crud.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
Expand All @@ -25,7 +28,8 @@
public class NoteController {

@Autowired
NoteService noteService;
@Qualifier("NoteService")
CrudService noteService;

@GetMapping("/")
public ResponseEntity<List<Note>> getAll() {
Expand All @@ -34,7 +38,7 @@ public ResponseEntity<List<Note>> getAll() {

@GetMapping("/{id}")
public ResponseEntity<Note> getNotes(@PathVariable Long id) {
Optional<Note> student = noteService.getNoteById(id);
Optional<Note> student = noteService.getById(id);
return ResponseEntity.status(HttpStatus.OK).body(student.get());

}
Expand All @@ -47,7 +51,7 @@ public ResponseEntity<String> createNote(@Valid @RequestBody NoteDto note,
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
noteService.createNote(note);
noteService.create(note);
return ResponseEntity
.status(HttpStatus.OK)
.body("Note created");
Expand All @@ -58,9 +62,9 @@ public ResponseEntity<String> createNote(@Valid @RequestBody NoteDto note,
@PutMapping("/{id}")
public ResponseEntity<String> updateNote( @PathVariable Long id,@Valid @RequestBody NoteDto note) {

if (noteService.getNoteById(id).isPresent()) {
if (noteService.getById(id).isPresent()) {

noteService.updateNoteById(id, note);
noteService.updateById(id, note);

return ResponseEntity.status(HttpStatus.OK).body("Note modified");
} else {
Expand All @@ -72,8 +76,8 @@ public ResponseEntity<String> updateNote( @PathVariable Long id,@Valid @RequestB
@DeleteMapping("/{id}")
public ResponseEntity<String> removeNoteById(@PathVariable Long id) {

if (noteService.getNoteById(id).isPresent()) {
noteService.removeNoteById(id);
if (noteService.getById(id).isPresent()) {
noteService.removeById(id);
return ResponseEntity.status(HttpStatus.OK).body("Note deleted");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Note not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import com.api.notemanagementapi.dto.NoteDto;
import com.api.notemanagementapi.model.Note;
import com.api.notemanagementapi.service.crud.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
Expand All @@ -30,7 +32,7 @@ public ResponseEntity<List<Student>> getAll() {

@GetMapping("/{id}")
public ResponseEntity<Student> getStudent(@PathVariable Long id) {
Optional<Student> student = studentService.getStudentById(id);
Optional<Student> student = studentService.getById(id);
return ResponseEntity.status(HttpStatus.OK).body(student.get());

}
Expand Down Expand Up @@ -61,7 +63,7 @@ public ResponseEntity<String> createStudent(@Valid @RequestBody StudentDto stude
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
studentService.createStudent(student);
studentService.create(student);
return ResponseEntity
.status(HttpStatus.OK)
.body("Student created");
Expand All @@ -73,13 +75,13 @@ public ResponseEntity<String> createStudent(@Valid @RequestBody StudentDto stude
public ResponseEntity<String> updateStudent(@PathVariable Long id, @Valid @RequestBody StudentDto student
,BindingResult bindingResult) {

if (studentService.getStudentById(id).isPresent()) {
if (studentService.getById(id).isPresent()) {
if (bindingResult.hasErrors()) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
studentService.updateStudentById(id, student);
studentService.updateById(id, student);
return ResponseEntity
.status(HttpStatus.OK)
.body("Student modified");
Expand All @@ -94,8 +96,8 @@ public ResponseEntity<String> updateStudent(@PathVariable Long id, @Valid @Reque
@DeleteMapping("/{id}")
public ResponseEntity<String> removeStudentById(@PathVariable Long id) {

if (studentService.getStudentById(id).isPresent()) {
studentService.removeStudentById(id);
if (studentService.getById(id).isPresent()) {
studentService.removeById(id);
return ResponseEntity.status(HttpStatus.OK).body("Student deleted");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Student not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.List;
import java.util.Optional;

import com.api.notemanagementapi.service.crud.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
Expand All @@ -26,7 +28,8 @@
@RequestMapping("/api/subjects")
public class SubjectController {
@Autowired
SubjectService subjectService;
@Qualifier("SubjectService")
CrudService subjectService;

@GetMapping("/")
public ResponseEntity<List<Subject>> getAll() {
Expand All @@ -35,7 +38,7 @@ public ResponseEntity<List<Subject>> getAll() {

@GetMapping("/{id}")
public ResponseEntity<Subject> getSubject(@PathVariable Long id) {
Optional<Subject> subject = subjectService.getSubjectById(id);
Optional<Subject> subject = subjectService.getById(id);
return ResponseEntity.status(HttpStatus.OK).body(subject.get());
}

Expand All @@ -46,7 +49,7 @@ public ResponseEntity<String> createSubject(@Valid @RequestBody SubjectDto subje
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
subjectService.createSubject(subject);
subjectService.create(subject);
return ResponseEntity
.status(HttpStatus.OK)
.body("Subject created");
Expand All @@ -56,14 +59,14 @@ public ResponseEntity<String> createSubject(@Valid @RequestBody SubjectDto subje
@PutMapping("/{id}")
public ResponseEntity<String> updateSubject(@PathVariable Long id,@Valid @RequestBody SubjectDto subject,
BindingResult bindingResult) {
if (subjectService.getSubjectById(id).isPresent()) {
if (subjectService.getById(id).isPresent()) {

if (bindingResult.hasErrors()) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
subjectService.updateSubjectById(id, subject);
subjectService.updateById(id, subject);
return ResponseEntity
.status(HttpStatus.OK)
.body("Subject modified");
Expand All @@ -78,8 +81,8 @@ public ResponseEntity<String> updateSubject(@PathVariable Long id,@Valid @Reques
@DeleteMapping("/{id}")
public ResponseEntity<String> removeSubjectById(@PathVariable Long id) {

if (subjectService.getSubjectById(id).isPresent()) {
subjectService.removeSubjectById(id);
if (subjectService.getById(id).isPresent()) {
subjectService.removeById(id);
return ResponseEntity.status(HttpStatus.OK).body("Subject deleted");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Subject not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.List;
import java.util.Optional;

import com.api.notemanagementapi.service.crud.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
Expand All @@ -26,7 +29,8 @@
public class TeacherController {

@Autowired
TeacherService teacherService;
@Qualifier("TeacherService")
CrudService teacherService;

@GetMapping("/")
public ResponseEntity<List<Teacher>> getAll() {
Expand All @@ -35,7 +39,7 @@ public ResponseEntity<List<Teacher>> getAll() {

@GetMapping("/{id}")
public ResponseEntity<Teacher> getTeacher(@PathVariable Long id) {
Optional<Teacher> teacher = teacherService.getTeacherById(id);
Optional<Teacher> teacher = teacherService.getById(id);
return ResponseEntity.status(HttpStatus.OK).body(teacher.get());

}
Expand All @@ -47,7 +51,7 @@ public ResponseEntity<String> createTeacher(@Valid @RequestBody TeacherDto teach
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
teacherService.createTeacher(teacher);
teacherService.create(teacher);
return ResponseEntity
.status(HttpStatus.OK)
.body("Teacher created");
Expand All @@ -57,13 +61,13 @@ public ResponseEntity<String> createTeacher(@Valid @RequestBody TeacherDto teach
@PutMapping("/{id}")
public ResponseEntity<String> updateTeacher(@PathVariable Long id,@Valid @RequestBody TeacherDto teacher
,BindingResult bindingResult) {
if (teacherService.getTeacherById(id).isPresent()) {
if (teacherService.getById(id).isPresent()) {
if (bindingResult.hasErrors()) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(bindingResult.getFieldError().getDefaultMessage());
} else {
teacherService.updateTeacherById(id, teacher);
teacherService.updateById(id, teacher);
return ResponseEntity
.status(HttpStatus.OK)
.body("Teacher modified");
Expand All @@ -76,8 +80,8 @@ public ResponseEntity<String> updateTeacher(@PathVariable Long id,@Valid @Reque

@DeleteMapping("/{id}")
public ResponseEntity<String> removeTeacherById(@PathVariable Long id) {
if (teacherService.getTeacherById(id).isPresent()) {
teacherService.removeTeacherById(id);
if (teacherService.getById(id).isPresent()) {
teacherService.removeById(id);
return ResponseEntity.status(HttpStatus.OK).body("Teacher deleted");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Teacher not found");
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/api/notemanagementapi/dto/NoteDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class NoteDto {
@NotBlank(message = "Verificar nota ingresada")
private String note;
@NotBlank(message = "Verificar nota ingresada")
private String studentLastName;
private Long studentId;
@NotBlank(message = "Verificar nota ingresada")
private String subjectName;
private Long subjectId;

public NoteDto(String note, String studentLastName, String subjectName) {
public NoteDto(String note, Long studentId, Long subjectId) {
this.note=note;
this.studentLastName=studentLastName;
this.subjectName=subjectName;
this.studentId=studentId;
this.subjectId=subjectId;

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.api.notemanagementapi.model;
package com.api.notemanagementapi.dto;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.*;

@Data
public abstract class Person {
@Embeddable
@NoArgsConstructor
public class Person {

@NotBlank(message = "Verificar nombre ingresado")
private String name;
Expand All @@ -20,4 +23,11 @@ public abstract class Person {

@NotBlank(message = "Verificar celular ingresado")
private String cell_phone;

public Person(String name, String lastName,String email,String cell_phone) {
this.email = email;
this.lastName = lastName;
this.name = name;
this.cell_phone = cell_phone;
}
}
16 changes: 12 additions & 4 deletions src/main/java/com/api/notemanagementapi/dto/StudentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import java.util.Set;

import com.api.notemanagementapi.model.Person;
import lombok.Data;
import jakarta.persistence.Embedded;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Data
public class StudentDto extends Person {
@Getter
@Setter
@Builder
public class StudentDto{
@Embedded
private Person personalData;
private Set<Long> id_subjects;


}
2 changes: 2 additions & 0 deletions src/main/java/com/api/notemanagementapi/dto/SubjectDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class SubjectDto {

@NotBlank(message = "Verificar nombre ingresado")
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/api/notemanagementapi/dto/TeacherDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.api.notemanagementapi.dto;

import com.api.notemanagementapi.model.Person;
public class TeacherDto extends Person {

public TeacherDto(String name, String lastName, String email, String cell_phone) {
super(name, lastName, email, cell_phone);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/api/notemanagementapi/model/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ public class Note {
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "subject_id")
private Subject subject;


@Override
public String toString() {
return "Note{" +
"id=" + id +
", note='" + note + '\'' +
", student=" + student.getId() +
", subject=" + subject.getId() +
'}';
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/api/notemanagementapi/model/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.CascadeType;
Expand Down Expand Up @@ -38,6 +40,18 @@ public class Student {
@Column(unique = true)
private String email;

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", cell_phone='" + cell_phone + '\'' +
", IDsubjects=" + subjects.stream().map(subject -> subject.getId()).collect(Collectors.toList()) +
'}';
}

private String cell_phone;

//Materias de los estudiantes
Expand All @@ -48,7 +62,6 @@ public class Student {
List<Subject> subjects = new ArrayList<>();

//Notas de los alumnos
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL)
private List<Note> notes;
}
Loading

0 comments on commit 37e27c0

Please sign in to comment.