diff --git a/src/main/java/com/api/notemanagementapi/controller/NoteController.java b/src/main/java/com/api/notemanagementapi/controller/NoteController.java index b508e87..2834340 100644 --- a/src/main/java/com/api/notemanagementapi/controller/NoteController.java +++ b/src/main/java/com/api/notemanagementapi/controller/NoteController.java @@ -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; @@ -25,7 +28,8 @@ public class NoteController { @Autowired - NoteService noteService; + @Qualifier("NoteService") + CrudService noteService; @GetMapping("/") public ResponseEntity> getAll() { @@ -34,7 +38,7 @@ public ResponseEntity> getAll() { @GetMapping("/{id}") public ResponseEntity getNotes(@PathVariable Long id) { - Optional student = noteService.getNoteById(id); + Optional student = noteService.getById(id); return ResponseEntity.status(HttpStatus.OK).body(student.get()); } @@ -47,7 +51,7 @@ public ResponseEntity 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"); @@ -58,9 +62,9 @@ public ResponseEntity createNote(@Valid @RequestBody NoteDto note, @PutMapping("/{id}") public ResponseEntity 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 { @@ -72,8 +76,8 @@ public ResponseEntity updateNote( @PathVariable Long id,@Valid @RequestB @DeleteMapping("/{id}") public ResponseEntity 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"); diff --git a/src/main/java/com/api/notemanagementapi/controller/StudentController.java b/src/main/java/com/api/notemanagementapi/controller/StudentController.java index 169ef6d..d18b7a3 100644 --- a/src/main/java/com/api/notemanagementapi/controller/StudentController.java +++ b/src/main/java/com/api/notemanagementapi/controller/StudentController.java @@ -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; @@ -30,7 +32,7 @@ public ResponseEntity> getAll() { @GetMapping("/{id}") public ResponseEntity getStudent(@PathVariable Long id) { - Optional student = studentService.getStudentById(id); + Optional student = studentService.getById(id); return ResponseEntity.status(HttpStatus.OK).body(student.get()); } @@ -61,7 +63,7 @@ public ResponseEntity 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"); @@ -73,13 +75,13 @@ public ResponseEntity createStudent(@Valid @RequestBody StudentDto stude public ResponseEntity 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"); @@ -94,8 +96,8 @@ public ResponseEntity updateStudent(@PathVariable Long id, @Valid @Reque @DeleteMapping("/{id}") public ResponseEntity 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"); diff --git a/src/main/java/com/api/notemanagementapi/controller/SubjectController.java b/src/main/java/com/api/notemanagementapi/controller/SubjectController.java index c617d9b..2b85b9a 100644 --- a/src/main/java/com/api/notemanagementapi/controller/SubjectController.java +++ b/src/main/java/com/api/notemanagementapi/controller/SubjectController.java @@ -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; @@ -26,7 +28,8 @@ @RequestMapping("/api/subjects") public class SubjectController { @Autowired - SubjectService subjectService; + @Qualifier("SubjectService") + CrudService subjectService; @GetMapping("/") public ResponseEntity> getAll() { @@ -35,7 +38,7 @@ public ResponseEntity> getAll() { @GetMapping("/{id}") public ResponseEntity getSubject(@PathVariable Long id) { - Optional subject = subjectService.getSubjectById(id); + Optional subject = subjectService.getById(id); return ResponseEntity.status(HttpStatus.OK).body(subject.get()); } @@ -46,7 +49,7 @@ public ResponseEntity 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"); @@ -56,14 +59,14 @@ public ResponseEntity createSubject(@Valid @RequestBody SubjectDto subje @PutMapping("/{id}") public ResponseEntity 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"); @@ -78,8 +81,8 @@ public ResponseEntity updateSubject(@PathVariable Long id,@Valid @Reques @DeleteMapping("/{id}") public ResponseEntity 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"); diff --git a/src/main/java/com/api/notemanagementapi/controller/TeacherController.java b/src/main/java/com/api/notemanagementapi/controller/TeacherController.java index 47b6ff8..e27d437 100644 --- a/src/main/java/com/api/notemanagementapi/controller/TeacherController.java +++ b/src/main/java/com/api/notemanagementapi/controller/TeacherController.java @@ -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; @@ -26,7 +29,8 @@ public class TeacherController { @Autowired - TeacherService teacherService; + @Qualifier("TeacherService") + CrudService teacherService; @GetMapping("/") public ResponseEntity> getAll() { @@ -35,7 +39,7 @@ public ResponseEntity> getAll() { @GetMapping("/{id}") public ResponseEntity getTeacher(@PathVariable Long id) { - Optional teacher = teacherService.getTeacherById(id); + Optional teacher = teacherService.getById(id); return ResponseEntity.status(HttpStatus.OK).body(teacher.get()); } @@ -47,7 +51,7 @@ public ResponseEntity 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"); @@ -57,13 +61,13 @@ public ResponseEntity createTeacher(@Valid @RequestBody TeacherDto teach @PutMapping("/{id}") public ResponseEntity 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"); @@ -76,8 +80,8 @@ public ResponseEntity updateTeacher(@PathVariable Long id,@Valid @Reque @DeleteMapping("/{id}") public ResponseEntity 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"); diff --git a/src/main/java/com/api/notemanagementapi/dto/NoteDto.java b/src/main/java/com/api/notemanagementapi/dto/NoteDto.java index 1edba47..343d5cb 100644 --- a/src/main/java/com/api/notemanagementapi/dto/NoteDto.java +++ b/src/main/java/com/api/notemanagementapi/dto/NoteDto.java @@ -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; } } diff --git a/src/main/java/com/api/notemanagementapi/model/Person.java b/src/main/java/com/api/notemanagementapi/dto/Person.java similarity index 60% rename from src/main/java/com/api/notemanagementapi/model/Person.java rename to src/main/java/com/api/notemanagementapi/dto/Person.java index ff917f7..0c57de1 100644 --- a/src/main/java/com/api/notemanagementapi/model/Person.java +++ b/src/main/java/com/api/notemanagementapi/dto/Person.java @@ -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; @@ -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; + } } diff --git a/src/main/java/com/api/notemanagementapi/dto/StudentDto.java b/src/main/java/com/api/notemanagementapi/dto/StudentDto.java index 5a4ffc6..6afb8a3 100644 --- a/src/main/java/com/api/notemanagementapi/dto/StudentDto.java +++ b/src/main/java/com/api/notemanagementapi/dto/StudentDto.java @@ -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 id_subjects; + + } diff --git a/src/main/java/com/api/notemanagementapi/dto/SubjectDto.java b/src/main/java/com/api/notemanagementapi/dto/SubjectDto.java index 75ebed1..39c1e6c 100644 --- a/src/main/java/com/api/notemanagementapi/dto/SubjectDto.java +++ b/src/main/java/com/api/notemanagementapi/dto/SubjectDto.java @@ -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") diff --git a/src/main/java/com/api/notemanagementapi/dto/TeacherDto.java b/src/main/java/com/api/notemanagementapi/dto/TeacherDto.java index ebf6a4c..03b1fff 100644 --- a/src/main/java/com/api/notemanagementapi/dto/TeacherDto.java +++ b/src/main/java/com/api/notemanagementapi/dto/TeacherDto.java @@ -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); + } } diff --git a/src/main/java/com/api/notemanagementapi/model/Note.java b/src/main/java/com/api/notemanagementapi/model/Note.java index 035533b..03b8fca 100644 --- a/src/main/java/com/api/notemanagementapi/model/Note.java +++ b/src/main/java/com/api/notemanagementapi/model/Note.java @@ -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() + + '}'; + } } diff --git a/src/main/java/com/api/notemanagementapi/model/Student.java b/src/main/java/com/api/notemanagementapi/model/Student.java index 1ba9edc..1d262f1 100644 --- a/src/main/java/com/api/notemanagementapi/model/Student.java +++ b/src/main/java/com/api/notemanagementapi/model/Student.java @@ -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; @@ -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 @@ -48,7 +62,6 @@ public class Student { List subjects = new ArrayList<>(); //Notas de los alumnos - @JsonIgnore @OneToMany(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL) private List notes; } \ No newline at end of file diff --git a/src/main/java/com/api/notemanagementapi/model/Subject.java b/src/main/java/com/api/notemanagementapi/model/Subject.java index 0d0670a..39700e4 100644 --- a/src/main/java/com/api/notemanagementapi/model/Subject.java +++ b/src/main/java/com/api/notemanagementapi/model/Subject.java @@ -14,10 +14,7 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Getter @Builder @@ -36,9 +33,16 @@ public class Subject { private String description; //Profesor de la materia - @JsonIgnore - @ManyToOne(fetch = FetchType.LAZY, optional = false) + @ManyToOne(fetch = FetchType.EAGER, optional = false) @JoinColumn(name = "teacher_id") private Teacher teacher; + @Override + public String toString() { + return "Subject{" + + "id=" + id + + ", name='" + name + '\'' + + ", description='" + description + '\''+ + '}'; + } } \ No newline at end of file diff --git a/src/main/java/com/api/notemanagementapi/model/Teacher.java b/src/main/java/com/api/notemanagementapi/model/Teacher.java index db4eabf..4b66e25 100644 --- a/src/main/java/com/api/notemanagementapi/model/Teacher.java +++ b/src/main/java/com/api/notemanagementapi/model/Teacher.java @@ -11,16 +11,13 @@ import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Getter @Builder @NoArgsConstructor @AllArgsConstructor -@Entity +@Entity @Table(name = "teachers") public class Teacher { @Id @@ -30,14 +27,24 @@ public class Teacher { @Column(unique = true) private String name; + @Override + public String toString() { + return "Teacher{" + + "id=" + id + + ", name='" + name + '\'' + + ", lastName='" + lastName + '\'' + + ", email='" + email + '\'' + + ", cell_phone='" + cell_phone + '\'' + + '}'; + } + private String lastName; - + @Column(unique = true) private String email; private String cell_phone; - - //Materias a cargo del profesor - @OneToMany(fetch = FetchType.EAGER,mappedBy = "teacher", cascade = CascadeType.ALL) - private List subjects = new ArrayList<>(); + + @OneToMany(fetch = FetchType.LAZY,mappedBy = "teacher",cascade = CascadeType.ALL) + private List subjects; } diff --git a/src/main/java/com/api/notemanagementapi/service/NoteService.java b/src/main/java/com/api/notemanagementapi/service/NoteService.java index a7790d6..0fd0be5 100644 --- a/src/main/java/com/api/notemanagementapi/service/NoteService.java +++ b/src/main/java/com/api/notemanagementapi/service/NoteService.java @@ -1,18 +1,75 @@ package com.api.notemanagementapi.service; +import java.util.Collections; 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.stereotype.Service; import com.api.notemanagementapi.dto.NoteDto; import com.api.notemanagementapi.model.Note; +import com.api.notemanagementapi.repository.NoteRepository; +import com.api.notemanagementapi.repository.StudentRepository; +import com.api.notemanagementapi.repository.SubjectRepository; @Service -public interface NoteService { - public List getAll(); - public Optional getNoteById(Long id); - public Note createNote(NoteDto note); - public Optional updateNoteById(Long id, NoteDto note); - public void removeNoteById (Long id); +public class NoteService implements CrudService { + + @Autowired + NoteRepository noteRepository; + + @Autowired + StudentRepository studentRepository; + + @Autowired + SubjectRepository subjectRepository; + + @Override + public List getAll() { + return Collections.singletonList(noteRepository.findAll()); + } + + @Override + public Optional getById(Long id) { + // TODO Auto-generated method stub + return Optional.of(noteRepository.findById(id)); + } + + @Override + public Object create(Object object) { + NoteDto note = (NoteDto)object; + // TODO Auto-generated method stub + return noteRepository.save(Note + .builder() + .note(note.getNote()) + .student(studentRepository.findById(note.getStudentId()).get()) + .subject(subjectRepository.findById(note.getSubjectId()).get()) + .build()); + } + + @Override + public Optional updateById(Long id, Object object) { + NoteDto note = (NoteDto)object; + // TODO Auto-generated method stub + return noteRepository.findById(id) + .map(not -> { + not = Note + .builder() + .id(id) + .note(not.getNote()) + .student(studentRepository.findById(note.getStudentId()).get()) + .subject(subjectRepository.findById(note.getSubjectId()).get()) + .build(); + return noteRepository.save(not); + }); + } + + @Override + public void removeById(Long id) { + // TODO Auto-generated method stub + noteRepository.deleteById(id); + } + } diff --git a/src/main/java/com/api/notemanagementapi/service/NoteServiceImpl.java b/src/main/java/com/api/notemanagementapi/service/NoteServiceImpl.java deleted file mode 100644 index 85708c5..0000000 --- a/src/main/java/com/api/notemanagementapi/service/NoteServiceImpl.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.api.notemanagementapi.service; - -import java.util.List; -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.api.notemanagementapi.dto.NoteDto; -import com.api.notemanagementapi.model.Note; -import com.api.notemanagementapi.repository.NoteRepository; -import com.api.notemanagementapi.repository.StudentRepository; -import com.api.notemanagementapi.repository.SubjectRepository; - -@Service -public class NoteServiceImpl implements NoteService { - - @Autowired - NoteRepository noteRepository; - - @Autowired - StudentRepository studentRepository; - - @Autowired - SubjectRepository subjectRepository; - - @Override - public List getAll() { - // TODO Auto-generated method stub - return noteRepository.findAll(); - } - - @Override - public Optional getNoteById(Long id) { - // TODO Auto-generated method stub - return noteRepository.findById(id); - } - - @Override - public Note createNote(NoteDto note) { - // TODO Auto-generated method stub - return noteRepository.save(Note - .builder() - .note(note.getNote()) - .student(studentRepository.findByLastName(note.getStudentLastName()).get()) - .subject(subjectRepository.findByName(note.getSubjectName()).get()) - .build()); - } - - @Override - public Optional updateNoteById(Long id, NoteDto note) { - // TODO Auto-generated method stub - return noteRepository.findById(id) - .map(not -> { - not = Note - .builder() - .id(id) - .note(not.getNote()) - .student(studentRepository.findByLastName(note.getStudentLastName()).get()) - .subject(subjectRepository.findByName(note.getSubjectName()).get()) - .build(); - return noteRepository.save(not); - }); - } - - @Override - public void removeNoteById(Long id) { - // TODO Auto-generated method stub - noteRepository.deleteById(id); - } - -} diff --git a/src/main/java/com/api/notemanagementapi/service/StudentService.java b/src/main/java/com/api/notemanagementapi/service/StudentService.java index f4ce3e6..d8a5544 100644 --- a/src/main/java/com/api/notemanagementapi/service/StudentService.java +++ b/src/main/java/com/api/notemanagementapi/service/StudentService.java @@ -3,24 +3,59 @@ import java.util.List; import java.util.Optional; -import com.api.notemanagementapi.dto.NoteDto; -import com.api.notemanagementapi.model.Note; +import com.api.notemanagementapi.repository.StudentRepository; +import com.api.notemanagementapi.repository.SubjectRepository; +import com.api.notemanagementapi.service.crud.CrudService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.api.notemanagementapi.dto.StudentDto; import com.api.notemanagementapi.model.Student; @Service -public interface StudentService { - public List getAll(); - public Optional getStudentById(Long id); - public Student createStudent(StudentDto student); - public Optional updateStudentById(Long id, StudentDto student); - public void removeStudentById (Long id); - public Optional>getNotesById(Long id); - public Optional> getNotesByLastname(String lastName); +public class StudentService extends StudentServiceUtils implements CrudService { + @Autowired + StudentRepository studentRepository; + @Autowired + SubjectRepository subjectRepository; + public List getAll(){ + return studentRepository.findAll(); - public Optional> getSubjectsByLastName(String lastName); + } + public Optional getById(Long id){ + return studentRepository.findById(id); + } + public Student create(Object object){ + StudentDto student = (StudentDto) object; + return studentRepository.save( Student + .builder() + .name(student.getPersonalData().getName()) + .lastName(student.getPersonalData().getLastName()) + .cell_phone(student.getPersonalData().getCell_phone()) + .email(student.getPersonalData().getEmail()) + .subjects(subjectRepository.findAllById(student.getId_subjects())) + .build()); + } + public Optional updateById(Long id, Object object){ + StudentDto student = (StudentDto) object; + return studentRepository.findById(id) + .map(stu -> { + stu = Student + .builder() + .id(id) + .name(student.getPersonalData().getName()) + .lastName(student.getPersonalData().getLastName()) + .email(student.getPersonalData().getEmail()) + .cell_phone(student.getPersonalData().getCell_phone()) + .subjects(subjectRepository.findAllById(student.getId_subjects())) + .build(); + return studentRepository.save(stu); + }); + } + + @Override + public void removeById(Long id) { + studentRepository.deleteById(id); + } - public Optional> getSubjectsById(Long id); } diff --git a/src/main/java/com/api/notemanagementapi/service/StudentServiceImpl.java b/src/main/java/com/api/notemanagementapi/service/StudentServiceUtils.java similarity index 58% rename from src/main/java/com/api/notemanagementapi/service/StudentServiceImpl.java rename to src/main/java/com/api/notemanagementapi/service/StudentServiceUtils.java index ebd042e..e7a749b 100644 --- a/src/main/java/com/api/notemanagementapi/service/StudentServiceImpl.java +++ b/src/main/java/com/api/notemanagementapi/service/StudentServiceUtils.java @@ -3,71 +3,17 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; - import com.api.notemanagementapi.dto.NoteDto; -import com.api.notemanagementapi.model.Note; -import com.api.notemanagementapi.model.Subject; +import com.api.notemanagementapi.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.api.notemanagementapi.dto.StudentDto; -import com.api.notemanagementapi.model.Student; -import com.api.notemanagementapi.repository.StudentRepository; -import com.api.notemanagementapi.repository.SubjectRepository; - +//Clase con método ademas de los básicos para los estudiantes @Service -public class StudentServiceImpl implements StudentService { - +public class StudentServiceUtils { @Autowired StudentRepository studentRepository; - @Autowired - SubjectRepository subjectRepository; - @Override - public List getAll() { - return studentRepository.findAll(); - } - - @Override - public Optional getStudentById(Long id) { - return studentRepository.findById(id); - } - @Override - public Student createStudent(StudentDto student) { - return studentRepository.save( Student - .builder() - .name(student.getName()) - .lastName(student.getLastName()) - .cell_phone(student.getCell_phone()) - .email(student.getEmail()) - .subjects(subjectRepository.findAllById(student.getId_subjects())) - .build()); - } - - @Override - public Optional updateStudentById(Long id, StudentDto student) { - - return studentRepository.findById(id) - .map(stu -> { - stu = Student - .builder() - .id(id) - .name(student.getName()) - .lastName(student.getLastName()) - .email(student.getEmail()) - .cell_phone(student.getCell_phone()) - .subjects(subjectRepository.findAllById(student.getId_subjects())) - .build(); - return studentRepository.save(stu); - }); - } - - @Override - public void removeStudentById(Long id) { - studentRepository.deleteById(id); - } - - @Override public Optional> getNotesById(Long id) { //Se retorna una lista de NoteDto ,el cual contiene tres datos -> note,studentLastName y SubjectName return studentRepository.findById(id).map(stu->{ @@ -75,13 +21,12 @@ public Optional> getNotesById(Long id) { .stream() .map(note->{ return new NoteDto - (note.getNote(),note.getStudent().getLastName(),note.getSubject().getName()); + (note.getNote(),note.getStudent().getId(),note.getSubject().getId()); }).collect(Collectors.toList()); return notes; }); } - @Override public Optional> getNotesByLastname(String lastName) { //Se retorna una lista de NoteDto ,el cual contiene tres datos -> note,studentLastName y SubjectName return studentRepository.findByLastName(lastName) @@ -90,13 +35,13 @@ public Optional> getNotesByLastname(String lastName) { .stream() .map(note->{ return new NoteDto - (note.getNote(),note.getStudent().getLastName(),note.getSubject().getName()); + (note.getNote(),note.getStudent().getId(),note.getSubject().getId()); }).collect(Collectors.toList()); return notes; }); } - @Override + public Optional> getSubjectsByLastName(String lastName) { //Se retorna una lista de string la cual contiene los nombres de las materias return studentRepository.findByLastName(lastName) @@ -108,7 +53,6 @@ public Optional> getSubjectsByLastName(String lastName) { }); } - @Override public Optional> getSubjectsById(Long id) { //Se retorna una lista de string la cual contiene los nombres de las materias return studentRepository.findById(id) diff --git a/src/main/java/com/api/notemanagementapi/service/SubjectService.java b/src/main/java/com/api/notemanagementapi/service/SubjectService.java index dba7e1d..a10342b 100644 --- a/src/main/java/com/api/notemanagementapi/service/SubjectService.java +++ b/src/main/java/com/api/notemanagementapi/service/SubjectService.java @@ -2,16 +2,69 @@ 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.stereotype.Service; import com.api.notemanagementapi.dto.SubjectDto; import com.api.notemanagementapi.model.Subject; +import com.api.notemanagementapi.repository.SubjectRepository; +import com.api.notemanagementapi.repository.TeacherRepository; @Service -public interface SubjectService { - public List getAll(); - public Optional getSubjectById(Long id); - public Subject createSubject(SubjectDto subject); - public Optional updateSubjectById(Long id, SubjectDto subject); - public void removeSubjectById (Long id); +public class SubjectService implements CrudService { + + @Autowired + SubjectRepository subjectRepository; + + @Autowired + TeacherRepository teacherRepository; + + @Override + public List getAll() { + // TODO Auto-generated method stub + return subjectRepository.findAll(); + } + + @Override + public Optional getById(Long id) { + // TODO Auto-generated method stub + return subjectRepository.findById(id); + } + + @Override + public Subject create(Object object) { + SubjectDto subject = (SubjectDto) object; + // TODO Auto-generated method stub + return subjectRepository.save(Subject + .builder() + .name(subject.getName()) + .description(subject.getDescription()) + .teacher(teacherRepository.findById(subject.getIdTeacher()).get()) + .build()); + } + + @Override + public Optional updateById(Long id, Object object) { + SubjectDto subject = (SubjectDto) object; + + return subjectRepository.findById(id) + .map(subj -> { + subj = Subject + .builder() + .id(id) + .name(subject.getName()) + .description(subject.getDescription()) + .teacher(teacherRepository.findById(subject.getIdTeacher()).get()) + .build(); + return subjectRepository.save(subj); + }); + } + + @Override + public void removeById(Long id) { + subjectRepository.deleteById(id); + } + } diff --git a/src/main/java/com/api/notemanagementapi/service/SubjectServiceImpl.java b/src/main/java/com/api/notemanagementapi/service/SubjectServiceImpl.java deleted file mode 100644 index d914b99..0000000 --- a/src/main/java/com/api/notemanagementapi/service/SubjectServiceImpl.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.api.notemanagementapi.service; - -import java.util.List; -import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.api.notemanagementapi.dto.SubjectDto; -import com.api.notemanagementapi.model.Subject; -import com.api.notemanagementapi.repository.SubjectRepository; -import com.api.notemanagementapi.repository.TeacherRepository; - -@Service -public class SubjectServiceImpl implements SubjectService { - - @Autowired - SubjectRepository subjectRepository; - - @Autowired - TeacherRepository teacherRepository; - - @Override - public List getAll() { - // TODO Auto-generated method stub - return subjectRepository.findAll(); - } - - @Override - public Optional getSubjectById(Long id) { - // TODO Auto-generated method stub - return subjectRepository.findById(id); - } - - @Override - public Subject createSubject(SubjectDto subject) { - // TODO Auto-generated method stub - return subjectRepository.save(Subject - .builder() - .name(subject.getName()) - .description(subject.getDescription()) - .teacher(teacherRepository.findById(subject.getIdTeacher()).get()) - .build()); - } - - @Override - public Optional updateSubjectById(Long id, SubjectDto subject) { - - return subjectRepository.findById(id) - .map(subj -> { - subj = Subject - .builder() - .id(id) - .name(subject.getName()) - .description(subject.getDescription()) - .teacher(teacherRepository.findById(subject.getIdTeacher()).get()) - .build(); - return subjectRepository.save(subj); - }); - } - - @Override - public void removeSubjectById(Long id) { - subjectRepository.deleteById(id); - } - -} diff --git a/src/main/java/com/api/notemanagementapi/service/TeacherService.java b/src/main/java/com/api/notemanagementapi/service/TeacherService.java index ec0f4fe..bde5772 100644 --- a/src/main/java/com/api/notemanagementapi/service/TeacherService.java +++ b/src/main/java/com/api/notemanagementapi/service/TeacherService.java @@ -2,16 +2,64 @@ 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.stereotype.Service; import com.api.notemanagementapi.dto.TeacherDto; import com.api.notemanagementapi.model.Teacher; +import com.api.notemanagementapi.repository.TeacherRepository; @Service -public interface TeacherService { - public List getAll(); - public Optional getTeacherById(Long id); - public Teacher createTeacher(TeacherDto teacher); - public Optional updateTeacherById(Long id, TeacherDto teacher); - public void removeTeacherById (Long id); +public class TeacherService implements CrudService { + @Autowired + TeacherRepository teacherRepository; + + @Override + public List getAll() { + return teacherRepository.findAll(); + } + + @Override + public Optional getById(Long id) { + return teacherRepository.findById(id); + } + + @Override + public Object create(Object object) { + TeacherDto teacher = (TeacherDto)object; + // TODO Auto-generated method stub + return teacherRepository.save(Teacher + .builder() + .name(teacher.getName()) + .lastName(teacher.getLastName()) + .cell_phone(teacher.getCell_phone()) + .email(teacher.getEmail()) + .build()); + } + + @Override + public Optional updateById(Long id, Object object) { + TeacherDto teacher = (TeacherDto)object; + // TODO Auto-generated method stub + return teacherRepository.findById(id) + .map(tch -> { + tch = Teacher + .builder() + .id(id) + .name(teacher.getName()) + .lastName(teacher.getLastName()) + .email(teacher.getEmail()) + .cell_phone(teacher.getCell_phone()) + .build(); + return teacherRepository.save(tch); + }); + } + + @Override + public void removeById(Long id) { + teacherRepository.deleteById(id); + } + } diff --git a/src/main/java/com/api/notemanagementapi/service/TeacherServiceImpl.java b/src/main/java/com/api/notemanagementapi/service/TeacherServiceImpl.java deleted file mode 100644 index 6b7b2ba..0000000 --- a/src/main/java/com/api/notemanagementapi/service/TeacherServiceImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.api.notemanagementapi.service; - -import java.util.List; -import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.api.notemanagementapi.dto.TeacherDto; -import com.api.notemanagementapi.model.Teacher; -import com.api.notemanagementapi.repository.TeacherRepository; - -@Service -public class TeacherServiceImpl implements TeacherService { - @Autowired - TeacherRepository teacherRepository; - - @Override - public List getAll() { - return teacherRepository.findAll(); - } - - @Override - public Optional getTeacherById(Long id) { - return teacherRepository.findById(id); - } - - @Override - public Teacher createTeacher(TeacherDto teacher) { - // TODO Auto-generated method stub - return teacherRepository.save(Teacher - .builder() - .name(teacher.getName()) - .lastName(teacher.getLastName()) - .cell_phone(teacher.getCell_phone()) - .email(teacher.getEmail()) - .build()); - } - - @Override - public Optional updateTeacherById(Long id, TeacherDto teacher) { - // TODO Auto-generated method stub - return teacherRepository.findById(id) - .map(tch -> { - tch = Teacher - .builder() - .id(id) - .name(teacher.getName()) - .lastName(teacher.getLastName()) - .email(teacher.getEmail()) - .cell_phone(teacher.getCell_phone()) - .build(); - return teacherRepository.save(tch); - }); - } - - @Override - public void removeTeacherById(Long id) { - teacherRepository.deleteById(id); - } - -} diff --git a/src/main/java/com/api/notemanagementapi/service/crud/CrudService.java b/src/main/java/com/api/notemanagementapi/service/crud/CrudService.java new file mode 100644 index 0000000..d84374d --- /dev/null +++ b/src/main/java/com/api/notemanagementapi/service/crud/CrudService.java @@ -0,0 +1,20 @@ +package com.api.notemanagementapi.service.crud; + +import com.api.notemanagementapi.dto.NoteDto; +import com.api.notemanagementapi.model.Note; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; + +//Interface la cual es implementada en los service de cada entidad con los método crud básicos +@Component +public interface CrudService { + public List getAll(); + public Optional getById(Long id); + public Object create(Object object); + public Optional updateById(Long id, Object object); + public void removeById (Long id); +} diff --git a/src/main/java/com/api/notemanagementapi/service/crud/CrudServiceConfig.java b/src/main/java/com/api/notemanagementapi/service/crud/CrudServiceConfig.java new file mode 100644 index 0000000..00635db --- /dev/null +++ b/src/main/java/com/api/notemanagementapi/service/crud/CrudServiceConfig.java @@ -0,0 +1,25 @@ +package com.api.notemanagementapi.service.crud; + +import com.api.notemanagementapi.service.NoteService; +import com.api.notemanagementapi.service.StudentService; +import com.api.notemanagementapi.service.SubjectService; +import com.api.notemanagementapi.service.TeacherService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CrudServiceConfig { + @Bean(name="NoteService") + public NoteService getNoteService(){ + return new NoteService(); + } + @Bean(name="SubjectService") + public SubjectService getSubjectService(){ + return new SubjectService(); + } + @Bean(name="TeacherService") + public TeacherService getTeacherService(){ + return new TeacherService(); + } + +}