Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.app.backend.model.StudentModel;
import com.app.backend.service.impl.StudentServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -16,4 +17,8 @@ public interface StudentApiController {
public StudentModel createStudent(@RequestBody StudentModel newStudent);
@GetMapping("/students/{id}")
public StudentModel getStudentById(@PathVariable int id);
@PutMapping("/update-student/{id}")
public ResponseEntity<String> updateStudentById(@PathVariable int id, @RequestBody StudentModel newStudent);
@DeleteMapping("delete-student/{id}")
public ResponseEntity<String> deleteStudentById(@PathVariable int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.app.backend.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -30,4 +31,11 @@ public StudentModel createStudent(@RequestBody StudentModel newStudent){
public StudentModel getStudentById(@PathVariable int id){
return studentService.getStudentById(id);
}
@Override
public ResponseEntity<String> deleteStudentById(@PathVariable int id){
return studentService.deleteStudentById(id);
}
public ResponseEntity<String> updateStudentById(@PathVariable int id, @RequestBody StudentModel newStudent){
return studentService.updateStudentById(id, newStudent);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/app/backend/model/StudentModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import java.util.Date;

public class StudentModel {
private Integer id;
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/app/backend/service/StudentService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.app.backend.service;

import com.app.backend.model.StudentModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -14,4 +17,6 @@ public interface StudentService {
public List<StudentModel> getStudentsList();
public StudentModel createStudent(StudentModel newStudent);
public StudentModel getStudentById(int id);
public ResponseEntity<String> deleteStudentById(int Id);
public ResponseEntity<String> updateStudentById(int id, StudentModel newStudent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import com.app.backend.model.StudentModel;
import com.app.backend.service.StudentService;
import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.lang.model.type.NullType;
import java.net.http.HttpResponse;
import java.util.List;

import static org.springframework.http.HttpStatus.OK;

@Service
public class StudentServiceImpl implements StudentService {
public List<StudentModel> getStudentsList() {
Expand All @@ -15,7 +22,34 @@ public StudentModel createStudent(StudentModel newStudent){
this.students.add(newStudent);
return newStudent;
}
@Nullable
public StudentModel getStudentById(int id){
return this.students.get(id);
for(StudentModel student : this.students){
if(student.getId() == id){
return student;
}
}
return null;
}

public ResponseEntity<String> deleteStudentById(int id){
for(StudentModel student : this.students){
if(student.getId() == id){
students.remove(student);
return ResponseEntity.status(OK).body(String.format("Estudiante con id %d eliminado correctamente", id));
}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(String.format("Estudiante con id %d no encontrado", id));
}

public ResponseEntity<String> updateStudentById(int id, StudentModel newStudent){
for(int i = 0; i < this.students.size(); i++) {
if(this.students.get(i).getId() == id){
newStudent.setId(id);
this.students.set(i, newStudent);
return ResponseEntity.status(OK).body(String.format("Estudiante con id %d actualizado correctamente", id));
}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(String.format("Estudiante con id %d no encontrado", id));
}
}