Fixed bug with init of message repository
refactored GetMapping of restore message history function
This commit is contained in:
parent
fdedd75807
commit
7bbdb9c85d
7 changed files with 61 additions and 73 deletions
|
@ -1,13 +1,9 @@
|
||||||
package me.imsonmia.epqapi.controller;
|
package me.imsonmia.epqapi.controller;
|
||||||
|
|
||||||
import java.time.Instant;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||||
import org.springframework.messaging.handler.annotation.SendTo;
|
import org.springframework.messaging.handler.annotation.SendTo;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
|
|
||||||
import me.imsonmia.epqapi.model.Message;
|
import me.imsonmia.epqapi.model.Message;
|
||||||
import me.imsonmia.epqapi.repository.MessageRepository;
|
import me.imsonmia.epqapi.repository.MessageRepository;
|
||||||
|
@ -15,6 +11,7 @@ import me.imsonmia.epqapi.repository.MessageRepository;
|
||||||
@Controller
|
@Controller
|
||||||
// @RequestMapping("/api/v1")
|
// @RequestMapping("/api/v1")
|
||||||
public class MessageController {
|
public class MessageController {
|
||||||
|
@Autowired
|
||||||
private MessageRepository repository;
|
private MessageRepository repository;
|
||||||
@MessageMapping("/chat")
|
@MessageMapping("/chat")
|
||||||
@SendTo("/sub/chat")
|
@SendTo("/sub/chat")
|
||||||
|
@ -25,17 +22,7 @@ public class MessageController {
|
||||||
// Forward message to subscribers of Stomp endpoint
|
// Forward message to subscribers of Stomp endpoint
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
@GetMapping("/api/v1/chat/history/{from}")
|
|
||||||
public ArrayList<Message> getMessagesFromTimestamp(@PathVariable(value = "from") long fromTimestamp) {
|
|
||||||
ArrayList<Message> messages = new ArrayList<>();
|
|
||||||
Instant targetInstant = Instant.ofEpochMilli(fromTimestamp);
|
|
||||||
for (Message msg : repository.findAll()) {
|
|
||||||
Instant t = Instant.ofEpochMilli(msg.getTimestamp());
|
|
||||||
if (t.isBefore(targetInstant)) {continue;}
|
|
||||||
messages.add(msg);
|
|
||||||
}
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
// @GetMapping("/msg/{id}")
|
// @GetMapping("/msg/{id}")
|
||||||
// public ChatMessage getMessageById(@PathVariable(value = "id") Long id) {
|
// public ChatMessage getMessageById(@PathVariable(value = "id") Long id) {
|
||||||
// return chatMessageRepository.findById(id).get();
|
// return chatMessageRepository.findById(id).get();
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package me.imsonmia.epqapi.controller;
|
package me.imsonmia.epqapi.controller;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -9,7 +12,9 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import me.imsonmia.epqapi.model.Message;
|
||||||
import me.imsonmia.epqapi.model.User;
|
import me.imsonmia.epqapi.model.User;
|
||||||
|
import me.imsonmia.epqapi.repository.MessageRepository;
|
||||||
import me.imsonmia.epqapi.repository.UserRepository;
|
import me.imsonmia.epqapi.repository.UserRepository;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -17,6 +22,8 @@ import me.imsonmia.epqapi.repository.UserRepository;
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
private MessageRepository messageRepository;
|
||||||
@GetMapping("/user/{id}")
|
@GetMapping("/user/{id}")
|
||||||
public User getUserById(@PathVariable(value = "id") Long id) {
|
public User getUserById(@PathVariable(value = "id") Long id) {
|
||||||
return userRepository.findById(id).get();
|
return userRepository.findById(id).get();
|
||||||
|
@ -34,4 +41,15 @@ public class UserController {
|
||||||
) {
|
) {
|
||||||
userRepository.deleteById(id);
|
userRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
@GetMapping("/msg/{from}")
|
||||||
|
public ArrayList<Message> getMessagesFromTimestamp(@PathVariable(value = "from") Long fromTimestamp) {
|
||||||
|
ArrayList<Message> messages = new ArrayList<>();
|
||||||
|
Instant targetInstant = Instant.ofEpochMilli(fromTimestamp);
|
||||||
|
for (Message msg : messageRepository.findAll()) {
|
||||||
|
Instant t = Instant.ofEpochMilli(msg.getTimeMillis());
|
||||||
|
if (t.isBefore(targetInstant)) {continue;}
|
||||||
|
messages.add(msg);
|
||||||
|
}
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
package me.imsonmia.epqapi.model;
|
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
@Entity
|
|
||||||
@Table(name = "message")
|
|
||||||
public class ChatMessage {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
@Getter
|
|
||||||
Long id;
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
Long fromId;
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
String text;
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
String[] attachments;
|
|
||||||
public ChatMessage() {}
|
|
||||||
public ChatMessage(
|
|
||||||
Long fromId,
|
|
||||||
String text
|
|
||||||
) {
|
|
||||||
this.fromId = fromId;
|
|
||||||
this.text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -3,33 +3,34 @@ package me.imsonmia.epqapi.model;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "message")
|
@Table(name = "message")
|
||||||
public class Message {
|
public class Message {
|
||||||
@GeneratedValue
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
@Id
|
@Id
|
||||||
private Long id;
|
Long id;
|
||||||
private String from;
|
String fromUserId;
|
||||||
private String to;
|
String toUserId;
|
||||||
private String content;
|
String content;
|
||||||
private Long timestamp;
|
Long timeMillis;
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public String getFrom() {
|
public String getFromUserId() {
|
||||||
return this.from;
|
return fromUserId;
|
||||||
}
|
}
|
||||||
public void setFrom(String from) {
|
public String getToUserId() {
|
||||||
this.from = from;
|
return toUserId;
|
||||||
}
|
}
|
||||||
public String getTo() {
|
public void setFromUserId(String fromUserId) {
|
||||||
return to;
|
this.fromUserId = fromUserId;
|
||||||
}
|
}
|
||||||
public void setTo(String to) {
|
public void setToUserId(String toUserId) {
|
||||||
this.to = to;
|
this.toUserId = toUserId;
|
||||||
}
|
}
|
||||||
public String getContent() {
|
public String getContent() {
|
||||||
return content;
|
return content;
|
||||||
|
@ -37,10 +38,24 @@ public class Message {
|
||||||
public void setContent(String content) {
|
public void setContent(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
public Long getTimestamp() {
|
public Long getTimeMillis() {
|
||||||
return timestamp;
|
return timeMillis;
|
||||||
}
|
}
|
||||||
public void setTimestamp(Long timestamp) {
|
public void setTimeMillis(Long timeMillis) {
|
||||||
this.timestamp = timestamp;
|
this.timeMillis = timeMillis;
|
||||||
|
}
|
||||||
|
public Message() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Message(Long id,
|
||||||
|
String fromUserId,
|
||||||
|
String toUserId,
|
||||||
|
String content,
|
||||||
|
Long timeMillis) {
|
||||||
|
this.id = id;
|
||||||
|
this.fromUserId = fromUserId;
|
||||||
|
this.toUserId = toUserId;
|
||||||
|
this.content = content;
|
||||||
|
this.timeMillis = timeMillis;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,13 @@ public class User {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
@Getter
|
@Getter
|
||||||
Long id;
|
Long id;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
String userName;
|
String userName;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
Date dateJoined;
|
Date dateJoined;
|
||||||
public User() {
|
public User() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,3 +7,6 @@ import me.imsonmia.epqapi.model.User;
|
||||||
public interface UserRepository extends CrudRepository<User, Long> {
|
public interface UserRepository extends CrudRepository<User, Long> {
|
||||||
User findByUserName(String userName);
|
User findByUserName(String userName);
|
||||||
}
|
}
|
||||||
|
// NOTE timestamp is a reserved SQL keyword
|
||||||
|
// create table user (date_joined datetime(6), id bigint not null, user_name varchar(255), primary key (id)) engine=InnoDB
|
||||||
|
// create table message (id bigint not null, timestamp bigint, content varchar(255), from varchar(255), to varchar(255), primary key (id)) engine=InnoDB
|
|
@ -2,5 +2,5 @@ spring.datasource.url=jdbc:mariadb://127.0.0.1:3306/test
|
||||||
spring.datasource.username=dbuser
|
spring.datasource.username=dbuser
|
||||||
spring.datasource.password=dbpasswd
|
spring.datasource.password=dbpasswd
|
||||||
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
|
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
|
||||||
spring.jpa.show-sql: true
|
spring.jpa.show-sql: false
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
Loading…
Add table
Add a link
Reference in a new issue