Added beans for messaging system. WIP
This commit is contained in:
parent
e40a448f6d
commit
3b7f380b57
|
@ -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,
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package com.fnecan.study.bookapi.api;
|
||||
|
||||
public class BookController {
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.fnecan.study.bookapi.doman;
|
||||
|
||||
public enum Genre {
|
||||
HORROR,
|
||||
SCIFI,
|
||||
DRAMA
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<ConversationDocument, String> {
|
||||
}
|
|
@ -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<MessageDocument, String> {
|
||||
|
||||
@Query("{ $and: [{'conversationId': ?1} , {$or: [{ 'authorId': ?0 },{ 'recipientId': ?0 }]}] }")
|
||||
List<MessageDocument> findMessagesForUserAndConversation(String userId, String conversationId); // TODO: Test query or create a repository implementation
|
||||
}
|
|
@ -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<MessageDocument> getMessagesForUserAndConversation(String userId, String conversationId) {
|
||||
return messageRepository.findMessagesForUserAndConversation(userId, conversationId);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue