Răsfoiți Sursa

base project

Daniel Garcia Costa 1 an în urmă
comite
21cc89032e

+ 62 - 0
pom.xml

@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>org.springframework.boot</groupId>
+		<artifactId>spring-boot-starter-parent</artifactId>
+		<version>3.0.6</version>
+		<relativePath /> 
+	</parent>
+	<groupId>es.uv.garcosda</groupId>
+	<artifactId>DBCDS_MongoExample</artifactId>
+	<version>1.0</version>
+	<name>DBCDS_MongoExample</name>
+	<description>DBCDS_MongoExample</description>
+	<properties>
+		<java.version>17</java.version>
+		<spring-cloud.version>2022.0.2</spring-cloud.version>
+	</properties>
+	<dependencies>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-config</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-devtools</artifactId>
+			<scope>runtime</scope>
+			<optional>true</optional>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-data-mongodb</artifactId>
+		</dependency>
+	</dependencies>
+	<dependencyManagement>
+		<dependencies>
+			<dependency>
+				<groupId>org.springframework.cloud</groupId>
+				<artifactId>spring-cloud-dependencies</artifactId>
+				<version>${spring-cloud.version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>

+ 12 - 0
src/main/java/es/uv/garcosda/mongo/MongoDataApplication.java

@@ -0,0 +1,12 @@
+package es.uv.garcosda.mongo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class MongoDataApplication {
+
+  public static void main(String[] args) {
+    SpringApplication.run(MongoDataApplication.class, args);
+  }
+}

+ 81 - 0
src/main/java/es/uv/garcosda/mongo/controllers/MongoController.java

@@ -0,0 +1,81 @@
+package es.uv.garcosda.mongo.controllers;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import es.uv.garcosda.mongo.domain.File;
+import es.uv.garcosda.mongo.services.FileService;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+@RestController
+@RequestMapping("/files")
+public class MongoController {
+
+  @Autowired
+  FileService fs;
+
+  @GetMapping
+  public ResponseEntity<List<File>> getAllFiles() {
+    List<File> files = fs.findAll();
+    return new ResponseEntity<>(files, HttpStatus.OK);
+  }
+
+  @GetMapping("/preview/{fileId}")
+  public ResponseEntity<?> previewFileById(@PathVariable Long fileId) {
+    File file = fs.findById(fileId);
+    if (file == null) {
+      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+    }
+    if (file.getData().size() > 10) {
+      return new ResponseEntity<>(file.getData().subList(0, 10), HttpStatus.OK);
+    } else {
+      return new ResponseEntity<>(file.getData(), HttpStatus.OK);
+    }
+  }
+
+  @GetMapping("/download/{fileId}")
+  public ResponseEntity<?> getFileById(@PathVariable Long fileId) {
+    File file = fs.findById(fileId);
+    if (file == null) {
+      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+    }
+
+    return new ResponseEntity<>(file.getData(), HttpStatus.OK);
+  }
+
+  @PostMapping
+  public ResponseEntity<?> saveFile(
+    @RequestParam MultipartFile file,
+    @RequestParam Long fileId
+  ) throws IOException {
+    String content = new String(file.getBytes(), StandardCharsets.UTF_8);
+    ObjectMapper mapper = new ObjectMapper();
+    List<Object> data = mapper.readValue(
+      content,
+      mapper.getTypeFactory().constructCollectionType(List.class, Object.class)
+    );
+
+    fs.create(new File(fileId, data));
+
+    return ResponseEntity.ok().build();
+  }
+
+  @DeleteMapping("/{fileId}")
+  public ResponseEntity<?> deleteFile(@PathVariable Long fileId) {
+    fs.deleteById(fileId);
+    return ResponseEntity.ok().build();
+  }
+}

+ 37 - 0
src/main/java/es/uv/garcosda/mongo/domain/File.java

@@ -0,0 +1,37 @@
+package es.uv.garcosda.mongo.domain;
+
+import java.util.List;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+@Document
+public class File {
+
+  @Id
+  private Long fileId;
+
+  private List<Object> data;
+
+  public File() {}
+
+  public File(Long fileId, List<Object> data) {
+    this.fileId = fileId;
+    this.data = data;
+  }
+
+  public Long getFileId() {
+    return fileId;
+  }
+
+  public void setFileId(Long fileId) {
+    this.fileId = fileId;
+  }
+
+  public List<Object> getData() {
+    return data;
+  }
+
+  public void setData(List<Object> data) {
+    this.data = data;
+  }
+}

+ 7 - 0
src/main/java/es/uv/garcosda/mongo/repositories/FileRepository.java

@@ -0,0 +1,7 @@
+package es.uv.garcosda.mongo.repositories;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import es.uv.garcosda.mongo.domain.File;
+
+public interface FileRepository extends MongoRepository<File, Long> {}

+ 31 - 0
src/main/java/es/uv/garcosda/mongo/services/FileService.java

@@ -0,0 +1,31 @@
+package es.uv.garcosda.mongo.services;
+
+import es.uv.garcosda.mongo.domain.File;
+import es.uv.garcosda.mongo.repositories.FileRepository;
+
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class FileService {
+
+  @Autowired
+  FileRepository fr;
+
+  public List<File> findAll() {
+    return this.fr.findAll();
+  }
+
+  public File create(File file) {
+    return this.fr.save(file);
+  }
+
+  public File findById(Long id) {
+    return this.fr.findById(id).orElse(null);
+  }
+
+  public void deleteById(Long id) {
+    this.fr.deleteById(id);
+  }
+}

+ 3 - 0
src/main/resources/application.properties

@@ -0,0 +1,3 @@
+spring.application.name=mongo-data
+server.port=8080
+spring.data.mongodb.uri=mongodb://root:password@hodor.uv.es:27017/admin