From 19d8be17831c50bf2a215ff86df87a56168269e2 Mon Sep 17 00:00:00 2001
From: Laurine <lenetlaurine@gmail.com>
Date: Tue, 17 Oct 2023 09:47:18 +0200
Subject: [PATCH] poc load file

---
 initdb/1_TABLES.sql                           |  8 ++++-
 initdb/2_DEFAULT_ENTRIES.sql                  | 16 ++++-----
 .../AddFileToUserRequest.java                 |  8 +++++
 .../com/example/livecodingjavaspring/App.java |  1 +
 .../example/livecodingjavaspring/Image.java   | 24 +++++++++++++
 .../livecodingjavaspring/ImageDao.java        |  8 +++++
 .../example/livecodingjavaspring/User.java    |  5 +++
 .../UserLibraryController.java                | 35 +++++++++++++++++++
 .../livecodingjavaspring/UserMapper.java      |  5 +++
 src/main/resources/application.properties     |  3 +-
 10 files changed, 103 insertions(+), 10 deletions(-)
 create mode 100644 src/main/java/com/example/livecodingjavaspring/AddFileToUserRequest.java
 create mode 100644 src/main/java/com/example/livecodingjavaspring/Image.java
 create mode 100644 src/main/java/com/example/livecodingjavaspring/ImageDao.java
 create mode 100644 src/main/java/com/example/livecodingjavaspring/UserMapper.java

diff --git a/initdb/1_TABLES.sql b/initdb/1_TABLES.sql
index 3440d84..8d9a2a5 100644
--- a/initdb/1_TABLES.sql
+++ b/initdb/1_TABLES.sql
@@ -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
 )
diff --git a/initdb/2_DEFAULT_ENTRIES.sql b/initdb/2_DEFAULT_ENTRIES.sql
index 5f637dd..84be1b6 100644
--- a/initdb/2_DEFAULT_ENTRIES.sql
+++ b/initdb/2_DEFAULT_ENTRIES.sql
@@ -1,8 +1,8 @@
-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);
diff --git a/src/main/java/com/example/livecodingjavaspring/AddFileToUserRequest.java b/src/main/java/com/example/livecodingjavaspring/AddFileToUserRequest.java
new file mode 100644
index 0000000..de9212b
--- /dev/null
+++ b/src/main/java/com/example/livecodingjavaspring/AddFileToUserRequest.java
@@ -0,0 +1,8 @@
+package com.example.livecodingjavaspring;
+
+import org.springframework.web.multipart.MultipartFile;
+
+public record AddFileToUserRequest(
+        MultipartFile file
+) {
+}
diff --git a/src/main/java/com/example/livecodingjavaspring/App.java b/src/main/java/com/example/livecodingjavaspring/App.java
index f0ac0f4..0ab5ff7 100644
--- a/src/main/java/com/example/livecodingjavaspring/App.java
+++ b/src/main/java/com/example/livecodingjavaspring/App.java
@@ -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);
     }
diff --git a/src/main/java/com/example/livecodingjavaspring/Image.java b/src/main/java/com/example/livecodingjavaspring/Image.java
new file mode 100644
index 0000000..3194e2e
--- /dev/null
+++ b/src/main/java/com/example/livecodingjavaspring/Image.java
@@ -0,0 +1,24 @@
+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;
+}
diff --git a/src/main/java/com/example/livecodingjavaspring/ImageDao.java b/src/main/java/com/example/livecodingjavaspring/ImageDao.java
new file mode 100644
index 0000000..eb1405e
--- /dev/null
+++ b/src/main/java/com/example/livecodingjavaspring/ImageDao.java
@@ -0,0 +1,8 @@
+package com.example.livecodingjavaspring;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ImageDao extends JpaRepository<Image, Integer> {
+}
diff --git a/src/main/java/com/example/livecodingjavaspring/User.java b/src/main/java/com/example/livecodingjavaspring/User.java
index 11f8113..c518812 100644
--- a/src/main/java/com/example/livecodingjavaspring/User.java
+++ b/src/main/java/com/example/livecodingjavaspring/User.java
@@ -1,5 +1,6 @@
 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;
 }
diff --git a/src/main/java/com/example/livecodingjavaspring/UserLibraryController.java b/src/main/java/com/example/livecodingjavaspring/UserLibraryController.java
index 6688b4c..b5317b3 100644
--- a/src/main/java/com/example/livecodingjavaspring/UserLibraryController.java
+++ b/src/main/java/com/example/livecodingjavaspring/UserLibraryController.java
@@ -1,16 +1,24 @@
 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);
+    }
+
 }
+
diff --git a/src/main/java/com/example/livecodingjavaspring/UserMapper.java b/src/main/java/com/example/livecodingjavaspring/UserMapper.java
new file mode 100644
index 0000000..e1f0169
--- /dev/null
+++ b/src/main/java/com/example/livecodingjavaspring/UserMapper.java
@@ -0,0 +1,5 @@
+package com.example.livecodingjavaspring;
+
+public class UserMapper {
+
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 211a08b..a0e9bb0 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -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
-- 
GitLab