Daniel Garcia Costa 2 năm trước cách đây
mục cha
commit
bbe71c49ab

+ 15 - 0
src/main/java/es/uv/garcosda/config/WebConfig.java

@@ -0,0 +1,15 @@
+package es.uv.garcosda.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+	@Override
+	public void addCorsMappings(CorsRegistry registry) {
+		registry.addMapping("/**").allowedOrigins("*")
+				.allowedMethods("*").allowedHeaders("*")
+				.allowCredentials(false).maxAge(3600);
+	}
+}

+ 36 - 1
src/main/java/es/uv/garcosda/controllers/FraudController.java

@@ -1,18 +1,53 @@
 package es.uv.garcosda.controllers;
 
+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.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.RestController;
 
+import es.uv.garcosda.domain.Document;
+import es.uv.garcosda.services.DocumentService;
+
 @RestController
 @RequestMapping("/api/v1/documents")
 public class FraudController {
+	
+	@Autowired()
+	DocumentService ds;
 
 	@GetMapping("")
 	public ResponseEntity<?> documents(){
-		return new ResponseEntity<String>("", HttpStatus.OK);
+		return new ResponseEntity<List<Document>>(this.ds.findAll(), HttpStatus.OK);
+	}
+	
+	@GetMapping("{owner}")
+	public ResponseEntity<?> documents(@PathVariable("owner") String owner){
+		List<Document> documents = this.ds.findByOwner(owner);
+		if(documents.size() > 0) {
+			return new ResponseEntity<List<Document>>(documents, HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<String>("No documents found for owner "+owner, HttpStatus.NOT_FOUND);
+		}
 	}
 	
+	@PostMapping("")
+	public ResponseEntity<?> documents(@RequestBody Document document){
+		if(this.ds.add(document)) {
+			return new ResponseEntity<String>("Document added to collection", HttpStatus.CREATED);
+		}
+		else {
+			return new ResponseEntity<String>("Document cannot be added to collection", HttpStatus.CONFLICT);
+		}
+	}
+	
+	
+	
 }

+ 47 - 0
src/main/java/es/uv/garcosda/controllers/NotificationController.java

@@ -0,0 +1,47 @@
+package es.uv.garcosda.controllers;
+
+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.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import es.uv.garcosda.services.NotificationService;
+import es.uv.garcosda.domain.Notification;
+
+@RestController
+@RequestMapping("/api/v1/notifications")
+public class NotificationController {
+
+	@Autowired
+	NotificationService ns;
+	
+	@GetMapping("")
+	public ResponseEntity<?> notifications() {
+		return new ResponseEntity<List<Notification>>(this.ns.findAll(), HttpStatus.OK);
+	}
+	
+	@GetMapping("{owner}")
+	public ResponseEntity<?> notifications(@PathVariable("owner") String owner){
+		List<Notification> notifications = this.ns.findByOwner(owner);
+		if(notifications.size() > 0) {
+			return new ResponseEntity<List<Notification>>(notifications, HttpStatus.OK);
+		}
+		else {
+			return new ResponseEntity<String>("No notifications found for owner "+owner, HttpStatus.NOT_FOUND);
+		}
+		
+	}
+	
+	@DeleteMapping("")
+	public ResponseEntity<?> delete(){
+		this.ns.clear();
+		return new ResponseEntity<String>("Notification list cleaned", HttpStatus.OK);
+	}
+	
+}

+ 14 - 12
src/main/java/es/uv/garcosda/domain/Document.java

@@ -1,25 +1,20 @@
 package es.uv.garcosda.domain;
 
-public class Document {
+import java.io.Serializable;
 
-	private String id;
+public class Document implements Serializable{
+
+	private static final long serialVersionUID = 1L;
 	private String name;
 	private String content;
+	private String owner;
 	
 	public Document() {}
 	
-	public Document(String id, String name, String content) {
-		this.id = id;
+	public Document(String name, String content, String owner) {
 		this.name = name;
 		this.content = content;
-	}
-	
-	public String getId() {
-		return id;
-	}
-	
-	public void setId(String id) {
-		this.id = id;
+		this.owner = owner;
 	}
 	
 	public String getName() {
@@ -38,4 +33,11 @@ public class Document {
 		this.content = content;
 	}
 	
+	public String getOwner() {
+		return owner;
+	}
+	
+	public void setOwner(String owner) {
+		this.owner = owner;
+	}
 }

+ 28 - 6
src/main/java/es/uv/garcosda/domain/Notification.java

@@ -1,17 +1,23 @@
 package es.uv.garcosda.domain;
 
-import java.util.Date;
+import java.io.Serializable;
+import java.time.LocalDate;
 
-public class Notification {
+public class Notification implements Serializable {
 
+	private static final long serialVersionUID = 1L;
 	private String content;
-	private Date date;
+	private boolean fraud;
+	private LocalDate date;
+	private Document document;
 	
 	public Notification() {}
 	
-	public Notification(String content, Date date) {
+	public Notification(String content, boolean fraud, LocalDate date, Document document) {
 		this.content = content;
+		this.fraud = fraud;
 		this.date = date;
+		this.document = document;
 	}
 
 	public String getContent() {
@@ -22,12 +28,28 @@ public class Notification {
 		this.content = content;
 	}
 	
-	public Date getDate() {
+	public boolean getFraud() {
+		return fraud;
+	}
+	
+	public void setFraud(boolean fraud) {
+		this.fraud = fraud;
+	}
+	
+	public LocalDate getDate() {
 		return date;
 	}
 	
-	public void setDate(Date date) {
+	public void setDate(LocalDate date) {
 		this.date = date;
 	}
+
+	public Document getDocument() {
+		return document;
+	}
+
+	public void setDocument(Document document) {
+		this.document = document;
+	}
 	
 }

+ 65 - 0
src/main/java/es/uv/garcosda/services/DocumentService.java

@@ -0,0 +1,65 @@
+package es.uv.garcosda.services;
+
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import es.uv.garcosda.domain.Document;
+import es.uv.garcosda.domain.Notification;
+
+@Service
+public class DocumentService {
+	
+	@Autowired
+	NotificationService ns;
+	
+	private List<Document> documents;
+	private Random rd;
+	
+	public DocumentService() {
+		this.documents = new ArrayList<Document>();
+		this.rd = new Random();
+	}
+	
+	public List<Document> findAll(){
+		return this.documents;
+	}
+	
+	public List<Document> findByOwner(String owner) {
+		return this.documents.stream().filter(x -> x.getOwner().toLowerCase().equals(owner.toLowerCase())).collect(Collectors.toList());
+	}
+	
+	public boolean add(Document document) {
+		this.compute(document);
+		boolean n = this.documents.add(document);
+		return n;
+	}
+	
+	public void compute(Document document) {
+		try {
+		    //Thread.sleep(this.rd.ints(20, 62).findFirst().getAsInt() * 1000);
+			Thread.sleep(1 * 1000);
+		} 
+		catch (InterruptedException ie) {
+		    Thread.currentThread().interrupt();
+		}
+		
+		Notification n;
+		if(this.rd.nextBoolean()){
+			n = new Notification("Fraud found in document owned by "+document.getOwner(), true, LocalDate.now(), document);
+		}
+		else {
+			n = new Notification("No fraud found in document owned by "+document.getOwner(), false, LocalDate.now(), document);
+		}
+		this.ns.add(n);
+	}
+	
+	
+	
+
+}

+ 38 - 0
src/main/java/es/uv/garcosda/services/NotificationService.java

@@ -0,0 +1,38 @@
+package es.uv.garcosda.services;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.springframework.stereotype.Service;
+
+import es.uv.garcosda.domain.Notification;
+
+@Service
+public class NotificationService {
+
+	List<Notification> notifications;
+	
+	public NotificationService() {
+		this.notifications = new ArrayList<Notification>();
+	}
+	
+	public List<Notification> findAll(){
+		return this.notifications;
+	}
+	
+	public List<Notification> findByOwner(String owner) {
+		List<Notification> ps = this.notifications.stream().filter(x -> x.getDocument().getOwner().toLowerCase().equals(owner.toLowerCase())).collect(Collectors.toList());
+		return ps;
+	}
+	
+	public void add(Notification notification) {
+		this.notifications.add(notification);
+	}
+	
+	public void clear() {
+		this.notifications.clear();
+	}
+	
+	
+}