diff --git a/initdb/1_TABLES.sql b/initdb/1_TABLES.sql index 3440d84da14a74169ed6ad07dcb063022c27b483..8d9a2a5cc094a8c3f62a7ec00dfbd1b568319bfd 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 5f637dd7d1326969fa5186f66fb0e1a1852f5cdb..84be1b6d6b2c6f90bfa6111e3e06721e8cc4ea7d 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 0000000000000000000000000000000000000000..de9212b58aa36687e2d8c994d178709727b5f36b --- /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 f0ac0f4037bf67c85e014ae87e4db6f8cfc8aa81..0ab5ff77e517ccd008fcaa3d782d42b5a7e227e0 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 0000000000000000000000000000000000000000..3194e2e45fb3e29dda628ad7910545fc55b1a7fb --- /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 0000000000000000000000000000000000000000..eb1405e5210bfcaeb4d24fe901b17db8065998fa --- /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 11f811366d028be77288bb0634bf5612cefe8b6e..c51881235b35156af01dd2c5031301e4b3edc488 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 6688b4cf1c201953433226992b66028c1875d34a..b5317b324e7c95a74787ee465538d53268a74b59 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 0000000000000000000000000000000000000000..e1f0169e5611bdcb0fbfa585c26785456825e9b2 --- /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 211a08b2d93005b1a1a8bfa4298d6fe993206c34..a0e9bb0cf3ad74c9c0d34bdd723bceee2e887e03 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