From 3b7f380b57550860fb5ddb9575ca77c31724e775 Mon Sep 17 00:00:00 2001 From: Artur Kmieckowiak Date: Fri, 10 Jan 2020 18:15:47 +0100 Subject: [PATCH] Added beans for messaging system. WIP --- .gitignore | 1 + .../study/bookapi/api/BookController.java | 4 ++ .../study/bookapi/api/MessageController.java | 34 ++++++++++++++ .../com/fnecan/study/bookapi/doman/Genre.java | 7 +++ .../study/bookapi/model/BookDocument.java | 44 +++++++++++++++++ .../bookapi/model/ConversationDocument.java | 20 ++++++++ .../study/bookapi/model/MessageDocument.java | 47 +++++++++++++++++++ .../repository/ConversationRepository.java | 7 +++ .../bookapi/repository/MessageRepository.java | 13 +++++ .../study/bookapi/service/MessageService.java | 24 ++++++++++ 10 files changed, 201 insertions(+) create mode 100644 src/main/java/com/fnecan/study/bookapi/api/BookController.java create mode 100644 src/main/java/com/fnecan/study/bookapi/api/MessageController.java create mode 100644 src/main/java/com/fnecan/study/bookapi/doman/Genre.java create mode 100644 src/main/java/com/fnecan/study/bookapi/model/BookDocument.java create mode 100644 src/main/java/com/fnecan/study/bookapi/model/ConversationDocument.java create mode 100644 src/main/java/com/fnecan/study/bookapi/model/MessageDocument.java create mode 100644 src/main/java/com/fnecan/study/bookapi/repository/ConversationRepository.java create mode 100644 src/main/java/com/fnecan/study/bookapi/repository/MessageRepository.java create mode 100644 src/main/java/com/fnecan/study/bookapi/service/MessageService.java diff --git a/.gitignore b/.gitignore index 875824c..2e0c967 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ HELP.md # Gradle .idea/**/gradle.xml .idea/**/libraries +.idea # Gradle and Maven with auto-import # When using Gradle or Maven with auto-import, you should exclude module files, diff --git a/src/main/java/com/fnecan/study/bookapi/api/BookController.java b/src/main/java/com/fnecan/study/bookapi/api/BookController.java new file mode 100644 index 0000000..c47d43f --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/api/BookController.java @@ -0,0 +1,4 @@ +package com.fnecan.study.bookapi.api; + +public class BookController { +} diff --git a/src/main/java/com/fnecan/study/bookapi/api/MessageController.java b/src/main/java/com/fnecan/study/bookapi/api/MessageController.java new file mode 100644 index 0000000..f82b3f7 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/api/MessageController.java @@ -0,0 +1,34 @@ +package com.fnecan.study.bookapi.api; + +import com.fnecan.study.bookapi.service.MessageService; +import org.springframework.beans.factory.annotation.Autowired; +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.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api/v1/conversation") +public class MessageController{ + + MessageService messageService; + + @Autowired + public MessageController(MessageService messageService) { + this.messageService = messageService; + } + + @GetMapping("/getMessages/{conversationId}") + public ResponseEntity getMessageForUser( + @RequestHeader("userId") String userId, + @PathVariable("conversationId") String conversationId + ) { + List conversationMessagesForUser = messageService.getMessagesForUserAndConversation(userId, conversationId); + return ResponseEntity.ok(conversationMessagesForUser); + } + +} diff --git a/src/main/java/com/fnecan/study/bookapi/doman/Genre.java b/src/main/java/com/fnecan/study/bookapi/doman/Genre.java new file mode 100644 index 0000000..a301fd9 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/doman/Genre.java @@ -0,0 +1,7 @@ +package com.fnecan.study.bookapi.doman; + +public enum Genre { + HORROR, + SCIFI, + DRAMA +} diff --git a/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java b/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java new file mode 100644 index 0000000..acae3ce --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/model/BookDocument.java @@ -0,0 +1,44 @@ +package com.fnecan.study.bookapi.model; + +import com.fnecan.study.bookapi.doman.Genre; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class BookDocument { + @Id + private String id; + private String author; + private String isbn; + private Genre genre; + + public BookDocument(String author, String isbn, Genre genre) { + this.author = author; + this.isbn = isbn; + this.genre = genre; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } +} diff --git a/src/main/java/com/fnecan/study/bookapi/model/ConversationDocument.java b/src/main/java/com/fnecan/study/bookapi/model/ConversationDocument.java new file mode 100644 index 0000000..9afb4a3 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/model/ConversationDocument.java @@ -0,0 +1,20 @@ +package com.fnecan.study.bookapi.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.Date; + +@Document +public class ConversationDocument { + @Id + String id; + + ConversationDocument(){ + + } + + public String getId() { + return id; + } +} diff --git a/src/main/java/com/fnecan/study/bookapi/model/MessageDocument.java b/src/main/java/com/fnecan/study/bookapi/model/MessageDocument.java new file mode 100644 index 0000000..c7915c6 --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/model/MessageDocument.java @@ -0,0 +1,47 @@ +package com.fnecan.study.bookapi.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class MessageDocument { + @Id + private String id; + private String conversationId; + private String authorId; + private String recipientId; + + public MessageDocument(String conversationId, String authorId, String recipientId) { + this.conversationId = conversationId; + this.authorId = authorId; + this.recipientId = recipientId; + } + + public String getConversationId() { + return conversationId; + } + + public void setConversationId(String conversationId) { + this.conversationId = conversationId; + } + + public String getId() { + return id; + } + + public String getAuthorId() { + return authorId; + } + + public void setAuthorId(String authorId) { + this.authorId = authorId; + } + + public String getRecipientId() { + return recipientId; + } + + public void setRecipientId(String recipientId) { + this.recipientId = recipientId; + } +} diff --git a/src/main/java/com/fnecan/study/bookapi/repository/ConversationRepository.java b/src/main/java/com/fnecan/study/bookapi/repository/ConversationRepository.java new file mode 100644 index 0000000..091b4eb --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/repository/ConversationRepository.java @@ -0,0 +1,7 @@ +package com.fnecan.study.bookapi.repository; + +import com.fnecan.study.bookapi.model.ConversationDocument; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface ConversationRepository extends MongoRepository { +} diff --git a/src/main/java/com/fnecan/study/bookapi/repository/MessageRepository.java b/src/main/java/com/fnecan/study/bookapi/repository/MessageRepository.java new file mode 100644 index 0000000..4c0071a --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/repository/MessageRepository.java @@ -0,0 +1,13 @@ +package com.fnecan.study.bookapi.repository; + +import com.fnecan.study.bookapi.model.MessageDocument; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; + +import java.util.List; + +public interface MessageRepository extends MongoRepository { + + @Query("{ $and: [{'conversationId': ?1} , {$or: [{ 'authorId': ?0 },{ 'recipientId': ?0 }]}] }") + List findMessagesForUserAndConversation(String userId, String conversationId); // TODO: Test query or create a repository implementation +} diff --git a/src/main/java/com/fnecan/study/bookapi/service/MessageService.java b/src/main/java/com/fnecan/study/bookapi/service/MessageService.java new file mode 100644 index 0000000..1da1dcf --- /dev/null +++ b/src/main/java/com/fnecan/study/bookapi/service/MessageService.java @@ -0,0 +1,24 @@ +package com.fnecan.study.bookapi.service; + +import com.fnecan.study.bookapi.model.MessageDocument; +import com.fnecan.study.bookapi.repository.ConversationRepository; +import com.fnecan.study.bookapi.repository.MessageRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class MessageService { + + @Autowired + MessageRepository messageRepository; + + @Autowired + ConversationRepository conversationRepository; + + public List getMessagesForUserAndConversation(String userId, String conversationId) { + return messageRepository.findMessagesForUserAndConversation(userId, conversationId); + } + +}