Skip to content
Snippets Groups Projects
Commit 49785fbd authored by Laurine's avatar Laurine
Browse files

add update student & image to student

parent 8065010d
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,8 @@ create table students
first_name TEXT not null,
last_name TEXT not null,
birthdate date null,
major_id int null
major_id int null,
image bytea null
);
create table majors
......
INSERT INTO majors (id, name, description) VALUES (1, 'MIN', 'Ouaiiis du code partout');
INSERT INTO students (id, first_name, last_name, birthdate, major_id) VALUES (1, 'Paul', 'Harrohide', '2002-06-15', 1);
INSERT INTO students (id, first_name, last_name, birthdate, major_id, image) VALUES (1, 'Paul', 'Harrohide', '2002-06-15', 1, null);
INSERT INTO courses (id, name, hours) VALUES (1, 'Java', 30);
INSERT INTO student_course (id, student_id, course_id) VALUES (1, 1, 1);
......
package com.takima.backskeleton.DTO;
import com.takima.backskeleton.models.Course;
import com.takima.backskeleton.models.Major;
import com.takima.backskeleton.models.Student;
import lombok.Builder;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;
import java.time.Instant;
import java.util.List;
@Builder
@Getter
public class StudentDto {
private String firstName;
private String lastName;
private Instant birthdate;
private List<Course> courses;
private Major major;
private MultipartFile image;
}
package com.takima.backskeleton.DTO;
import com.takima.backskeleton.models.Student;
import java.io.IOException;
public class StudentMapper {
public static Student fromDto(StudentDto dto, Long id) throws IOException {
return new Student.Builder()
.id(id)
.firstName(dto.getFirstName())
.lastName(dto.getLastName())
.birthdate(dto.getBirthdate())
.courses(dto.getCourses())
.major(dto.getMajor())
.image(dto.getImage().isEmpty() ? dto.getImage().getBytes() : null)
.build();
}
public static StudentDto toDto (Student student){
return StudentDto.builder()
.firstName(student.getFirstName())
.lastName(student.getLastName())
.birthdate(student.getBirthdate())
.courses(student.getCourses())
.major(student.getMajor())
.build();
}
}
package com.takima.backskeleton.controllers;
import com.takima.backskeleton.DTO.StudentDto;
import com.takima.backskeleton.models.Student;
import com.takima.backskeleton.services.StudentService;
import lombok.RequiredArgsConstructor;
......@@ -27,7 +28,12 @@ public class StudentController {
}
@PostMapping("")
public void addStudent(@RequestBody Student student) {
studentService.addStudent(student);
public void addStudent(@RequestBody StudentDto studentDto) {
studentService.addStudent(studentDto);
}
@PostMapping("/{id}")
public void updateStudent(@RequestBody StudentDto studentDto, @PathVariable Long id) {
studentService.updateStudent(studentDto, id);
}
}
......@@ -27,6 +27,8 @@ public class Student {
@ManyToOne()
@JoinColumn(name = "major_id")
private Major major;
@Column(name = "image")
private byte[] image;
private Student(Builder builder) {
this.id = builder.id;
......@@ -35,20 +37,23 @@ public class Student {
this.birthdate = builder.birthdate;
this.courses = builder.courses;
this.major = builder.major;
this.image = builder.image;
}
public Student() {
}
public static class Builder {
private final Long id;
private Long id;
private String firstName;
private String lastName;
private Instant birthdate;
private List<Course> courses;
private Major major;
private byte[] image;
public Builder(Long id) {
public Builder id (Long id) {
this.id = id;
return this;
}
public Builder firstName(String firstName) {
......@@ -71,6 +76,10 @@ public class Student {
this.birthdate = birthdate;
return this;
}
public Builder image(byte[] image) {
this.image = image;
return this;
}
public Student build() {
return new Student(this);
......
package com.takima.backskeleton.services;
import com.takima.backskeleton.DAO.StudentDao;
import com.takima.backskeleton.DTO.StudentDto;
import com.takima.backskeleton.DTO.StudentMapper;
import com.takima.backskeleton.models.Student;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
@Component
@RequiredArgsConstructor
......@@ -24,9 +28,26 @@ public class StudentService {
studentDao.deleteById(id);
}
public void addStudent(Student student) {
public void addStudent(StudentDto studentDto) {
Student student;
try {
student = StudentMapper.fromDto(studentDto, null);
} catch (IOException e) {
throw new RuntimeException("Error with Student image", e);
}
studentDao.save(student);
}
public void updateStudent(StudentDto studentDto, Long id) {
studentDao.findById(id)
.orElseThrow(() -> new NoSuchElementException("Student doesn't exist"));
Student student;
try {
student = StudentMapper.fromDto(studentDto, id);
} catch (IOException e) {
throw new RuntimeException("Error with Student image", e);
}
studentDao.save(student);
}
public List<Student> searchByMajorAndCourse(int majorId, int courseId) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment