Browse Source

base project

dagarcos 11 months ago
parent
commit
1b20f5d427

+ 0 - 4
pom.xml

@@ -24,10 +24,6 @@
 			<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>

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

@@ -1,81 +0,0 @@
-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();
-  }
-}

+ 87 - 0
src/main/java/es/uv/garcosda/mongo/controllers/UserController.java

@@ -0,0 +1,87 @@
+package es.uv.garcosda.mongo.controllers;
+
+import es.uv.garcosda.mongo.domain.User;
+import es.uv.garcosda.mongo.services.UserService;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+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.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/api/v1/users")
+public class UserController {
+
+	@Autowired
+	UserService us;
+
+	@GetMapping
+	public ResponseEntity<List<User>> getAll(@RequestParam(value="firstname", required = false) Optional<String> first, 
+			@RequestParam(value="lastname", required = false) Optional<String> last, 
+			@RequestParam(value="start", required = false) Optional<Boolean> start) {
+		List<User> users = new ArrayList<User>();
+		if(first.isPresent()) {
+			if(start.isPresent()) {
+				users = this.us.findByFirstnameStartingWith(first.get());
+			}
+			else {
+				users = this.us.findByFirstname(first.get());
+			}
+		}
+		else if(last.isPresent()) {
+			if(start.isPresent()) {
+				users = this.us.findByLastnameStartingWith(last.get());
+			}
+			else {
+				users = this.us.findByLastname(last.get());
+			}
+		}
+		else {
+			users = us.findAll();
+		}
+		
+		return new ResponseEntity<>(users, HttpStatus.OK);
+	}
+
+	@GetMapping("/{id}")
+	public ResponseEntity<?> getById(@PathVariable Integer id) {
+		User u = us.findById(id);
+		if (u == null) {
+			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+		}
+
+		return new ResponseEntity<>(u, HttpStatus.OK);
+	}
+
+	@PostMapping
+	public ResponseEntity<?> create(@RequestBody User user) throws IOException {
+		User u = us.create(user);
+		if (u == null) {
+			return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
+		}
+
+		return new ResponseEntity<>(u, HttpStatus.CREATED);
+	}
+
+	@DeleteMapping("/{id}")
+	public ResponseEntity<?> delete(@PathVariable Integer id) {
+		User u = us.findById(id);
+		if (u == null) {
+			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+		}
+		us.delete(u);
+		return new ResponseEntity<>(u, HttpStatus.OK);
+	}
+}

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

@@ -1,37 +0,0 @@
-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;
-  }
-}

+ 56 - 0
src/main/java/es/uv/garcosda/mongo/domain/User.java

@@ -0,0 +1,56 @@
+package es.uv.garcosda.mongo.domain;
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+@Document
+public class User {
+
+	@Id
+	private Integer id;
+	private String firstname;
+	private String lastname;
+	private String email;
+	
+	public User() {}
+
+	public User(Integer id, String firstname, String lastname, String email) {
+		this.id = id;
+		this.firstname = firstname;
+		this.lastname = lastname;
+		this.email = email;
+	}
+
+	public Integer getId() {
+		return id;
+	}
+
+	public void setId(Integer id) {
+		this.id = id;
+	}
+
+	public String getFirstname() {
+		return firstname;
+	}
+
+	public void setFirstname(String firstname) {
+		this.firstname = firstname;
+	}
+
+	public String getLastname() {
+		return lastname;
+	}
+
+	public void setLastname(String lastname) {
+		this.lastname = lastname;
+	}
+
+	public String getEmail() {
+		return email;
+	}
+
+	public void setEmail(String email) {
+		this.email = email;
+	}
+  
+}

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

@@ -1,7 +0,0 @@
-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> {}

+ 23 - 0
src/main/java/es/uv/garcosda/mongo/repositories/UserRepository.java

@@ -0,0 +1,23 @@
+package es.uv.garcosda.mongo.repositories;
+
+import java.util.List;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+import org.springframework.data.mongodb.repository.Query;
+
+import es.uv.garcosda.mongo.domain.User;
+
+public interface UserRepository extends MongoRepository<User, Integer> {
+	
+	List<User> findByFirstname(String firstname);
+	
+	List<User> findByFirstnameStartingWith(String regexp);
+	
+	List<User> findByLastname(String lastname);
+	
+	@Query("{ 'lastname' : { $regex: ?0 } }")
+	List<User> findByLastnameStartingWith(String regexp);
+	
+	List<User> findByEmail(String email);
+	
+}

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

@@ -1,31 +0,0 @@
-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);
-  }
-}

+ 56 - 0
src/main/java/es/uv/garcosda/mongo/services/UserService.java

@@ -0,0 +1,56 @@
+package es.uv.garcosda.mongo.services;
+
+import es.uv.garcosda.mongo.domain.User;
+import es.uv.garcosda.mongo.repositories.UserRepository;
+
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserService {
+
+	@Autowired
+	UserRepository ur;
+
+	public List<User> findAll() {
+		return this.ur.findAll();
+	}
+	
+	public List<User> findByFirstname(String firstname) {
+		return this.ur.findByFirstname(firstname);
+	}
+	
+	public List<User> findByFirstnameStartingWith(String firstname) {
+		return this.ur.findByFirstnameStartingWith(firstname);
+	}
+	
+	public List<User> findByLastname(String lastname) {
+		return this.ur.findByLastname(lastname);
+	}
+	
+	public List<User> findByLastnameStartingWith(String lastname) {
+		return this.ur.findByLastnameStartingWith(lastname);
+	}
+	
+	public List<User> findByEmail(String email) {
+		return this.ur.findByEmail(email);
+	}
+
+	public User create(User user) {
+		try {
+			User u = this.ur.save(user);
+			return u;
+		} catch (IllegalArgumentException e) {
+			return null;
+		}
+	}
+
+	public User findById(Integer id) {
+		return this.ur.findById(id).orElse(null);
+	}
+
+	public void delete(User user) {
+		this.ur.delete(user);
+	}
+}

+ 1 - 1
src/main/resources/application.properties

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