Skip to content
Snippets Groups Projects
Commit 19d8be17 authored by Laurine's avatar Laurine
Browse files

poc load file

parent 6d25f173
No related branches found
No related tags found
No related merge requests found
......@@ -3,5 +3,11 @@ create table users
id SERIAL PRIMARY KEY,
first_name TEXT not null,
last_name TEXT not null,
age int null
age int null,
image_id int null
);
create table images (
id SERIAL PRIMARY KEY,
bytes bytea
)
INSERT INTO users (id, first_name, last_name, age) VALUES (1, 'Laurine', 'LE NET', 22);
INSERT INTO users (id, first_name, last_name, age) VALUES (2, 'Pilou', 'BERTRAND', 24);
INSERT INTO users (id, first_name, last_name, age) VALUES (3, 'Alain', 'CONNU', 26);
INSERT INTO users (id, first_name, last_name, age) VALUES (4, 'Aurélie', 'BIENCEQUETAECRIT', 20);
INSERT INTO users (id, first_name, last_name, age) VALUES (5, 'Barack', 'AFFRITTE', 22);
INSERT INTO users (id, first_name, last_name, age) VALUES (6, 'Camille', 'ONETTE', 23);
INSERT INTO users (id, first_name, last_name, age) VALUES (7, 'Jean', 'TENRIEN', 22);
INSERT INTO users (id, first_name, last_name, age) VALUES (8, 'Lara', 'TATOUILLE', 24);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (1, 'Laurine', 'LE NET', 22, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (2, 'Pilou', 'BERTRAND', 24, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (3, 'Alain', 'CONNU', 26, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (4, 'Aurélie', 'BIENCEQUETAECRIT', 20, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (5, 'Barack', 'AFFRITTE', 22, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (6, 'Camille', 'ONETTE', 23, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (7, 'Jean', 'TENRIEN', 22, null);
INSERT INTO users (id, first_name, last_name, age, image_id) VALUES (8, 'Lara', 'TATOUILLE', 24, null);
package com.example.livecodingjavaspring;
import org.springframework.web.multipart.MultipartFile;
public record AddFileToUserRequest(
MultipartFile file
) {
}
......@@ -5,6 +5,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication()
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class).run(args);
}
......
package com.example.livecodingjavaspring;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity
@Table(name = "images")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private byte[] bytes;
@OneToOne(mappedBy = "image")
@JsonIgnore
private User user;
}
package com.example.livecodingjavaspring;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ImageDao extends JpaRepository<Image, Integer> {
}
package com.example.livecodingjavaspring;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
......@@ -19,4 +20,8 @@ public class User {
private String firstName;
private String lastName;
private Integer age;
@OneToOne
@JoinColumn(name = "image_id", referencedColumnName = "id")
@JsonIgnore
private Image image;
}
package com.example.livecodingjavaspring;
import lombok.RequiredArgsConstructor;
import org.springframework.http.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
@RestController
@RequestMapping("users")
@RequiredArgsConstructor
@CrossOrigin
public class UserLibraryController {
private final UserDao userDao;
private final ImageDao imageDao;
@GetMapping("helloWord")
public String helloWord(@RequestParam String name) {
return "Coucou " + name + " ! Tu vas bien ?";
......@@ -30,4 +38,31 @@ public class UserLibraryController {
return getUsers();
}
@PostMapping(value ="/{id}/file", consumes = MULTIPART_FORM_DATA_VALUE)
@Transactional
public User addFileToUser(@ModelAttribute AddFileToUserRequest addFileToUserRequest, @PathVariable Integer id) {
User user = userDao.findById(id).orElseThrow(() -> new NoSuchElementException("No user with this id : " + id));
Image image;
try {
image = Image.builder().bytes(addFileToUserRequest.file().getBytes()).build();
} catch (IOException e) {
throw new RuntimeException("An error occurred with the file");
}
imageDao.save(image);
user.setImage(image);
return user;
}
@GetMapping(value = "{id}/image", produces = MediaType.ALL_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable Integer id) {
HttpHeaders headers = new HttpHeaders();
headers.setContentDisposition(ContentDisposition.builder("attachement").filename("filename.png").build());
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return this.userDao.findById(id)
.map(User::getImage)
.map(image -> new ResponseEntity<>(image.getBytes(), headers, HttpStatus.OK))
.orElse(null);
}
}
package com.example.livecodingjavaspring;
public class UserMapper {
}
......@@ -2,4 +2,5 @@ spring.datasource.url=jdbc:postgresql://localhost/db-name
spring.datasource.username=user
spring.datasource.password=pwd
spring.datasource.driver-class-name=org.postgresql.Driver
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment