Quellcode durchsuchen

CRUD operations and use of RequestBody

dagarcos vor 2 Jahren
Ursprung
Commit
aac334ecbb

+ 43 - 2
src/main/java/es/uv/garcosda/controllers/BlogController.java

@@ -1,7 +1,6 @@
 package es.uv.garcosda.controllers;
 
 import java.util.List;
-import java.util.Optional;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -12,11 +11,13 @@ 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.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import es.uv.garcosda.domain.Post;
+import es.uv.garcosda.domain.PostDTO;
 import es.uv.garcosda.services.PostService;
 
 
@@ -44,6 +45,46 @@ public class BlogController {
 		}
 	}
 	
-
+	@PostMapping("posts")
+	public ResponseEntity<Post> create(@RequestBody PostDTO dto){
+		Post p = ps.add(dto);
+		if(p.getId() != null) {
+			return new ResponseEntity<Post>(p, HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<Post>(new Post(), HttpStatus.NOT_FOUND);
+		}
+	}
+	
+	@DeleteMapping("posts")
+	public ResponseEntity<String> delete(@RequestBody Post p){
+		if(ps.delete(p)) {
+			return new ResponseEntity<String>("", HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<String>("", HttpStatus.NOT_FOUND);
+		}
+	}
+	
+	@DeleteMapping("posts/{id}")
+	public ResponseEntity<String> deleteById(@PathVariable String id){
+		if(ps.deleteById(id)) {
+			return new ResponseEntity<String>("", HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<String>("", HttpStatus.NOT_FOUND);
+		}
+	}
+	
+	@PutMapping("posts")
+	public ResponseEntity<Post> update(@RequestBody Post dto){
+		Post p = ps.update(dto);
+		if(p.getId() != null) {
+			return new ResponseEntity<Post>(p, HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<Post>(new Post(), HttpStatus.NOT_FOUND);
+		}
+	}
 	
 }

+ 43 - 0
src/main/java/es/uv/garcosda/domain/PostDTO.java

@@ -0,0 +1,43 @@
+package es.uv.garcosda.domain;
+
+import java.time.LocalDate;
+
+public class PostDTO {
+
+	String authorFirstname;
+	String authorLastname;
+	String title;
+	String content;
+	
+	public PostDTO() {}
+	public PostDTO(String authorFirstname, String authorLastname, String title, String content) {
+		this.authorFirstname = authorFirstname;
+		this.authorLastname = authorLastname;
+		this.title = title;
+		this.content = content;
+	}
+	public String getAuthorFirstname() {
+		return authorFirstname;
+	}
+	public void setAuthorFirstname(String authorFirstname) {
+		this.authorFirstname = authorFirstname;
+	}
+	public String getAuthorLastname() {
+		return authorLastname;
+	}
+	public void setAuthorLastname(String authorLastname) {
+		this.authorLastname = authorLastname;
+	}
+	public String getTitle() {
+		return title;
+	}
+	public void setTitle(String title) {
+		this.title = title;
+	}
+	public String getContent() {
+		return content;
+	}
+	public void setContent(String content) {
+		this.content = content;
+	}
+}

+ 52 - 2
src/main/java/es/uv/garcosda/services/PostService.java

@@ -8,11 +8,10 @@ import java.util.List;
 import java.util.UUID;
 import java.util.stream.Collectors;
 
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import es.uv.garcosda.domain.Post;
+import es.uv.garcosda.domain.PostDTO;
 
 @Service
 public class PostService {
@@ -39,5 +38,56 @@ public class PostService {
 			return new Post();
 		}
 	}
+	
+	public Post add(PostDTO dto) {
+		Post p = new Post();
+		p.setAuthorFirstname(dto.getAuthorFirstname());
+		p.setAuthorLastname(dto.getAuthorLastname());
+		p.setContent(dto.getContent());
+		p.setTitle(dto.getTitle());
+		p.setId(UUID.randomUUID().toString());
+		p.setDate(LocalDate.now());
+		if(this.posts.add(p)) {
+			return p;
+		}
+		else{
+			return new Post();
+		}
+
+	}
+	
+	public Boolean delete(Post p) {
+		Integer size = this.posts.size();
+		this.posts.remove(p);
+		if(this.posts.size() < size) {
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+	
+	public Boolean deleteById(String id) {
+		Integer size = this.posts.size();
+		this.posts = this.posts.stream().filter(x -> !x.getId().equals(id)).collect(Collectors.toList());
+		if(this.posts.size() < size) {
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+	
+	public Post update(Post p) {
+		List<Post> ps = this.posts.stream().filter(x -> x.getId().equals(p.getId())).collect(Collectors.toList());
+		if(!ps.isEmpty()) {
+			this.posts = this.posts.stream().filter(x -> !x.getId().equals(p.getId())).collect(Collectors.toList());
+			this.posts.add(p);
+			return p;
+		}
+		else {
+			return new Post();
+		}
+	}
 
 }