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;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
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.repository.MessageRepository;
|
||||
|
@ -15,6 +11,7 @@ import me.imsonmia.epqapi.repository.MessageRepository;
|
|||
@Controller
|
||||
// @RequestMapping("/api/v1")
|
||||
public class MessageController {
|
||||
@Autowired
|
||||
private MessageRepository repository;
|
||||
@MessageMapping("/chat")
|
||||
@SendTo("/sub/chat")
|
||||
|
@ -25,17 +22,7 @@ public class MessageController {
|
|||
// Forward message to subscribers of Stomp endpoint
|
||||
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}")
|
||||
// public ChatMessage getMessageById(@PathVariable(value = "id") Long id) {
|
||||
// return chatMessageRepository.findById(id).get();
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package me.imsonmia.epqapi.controller;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.RestController;
|
||||
|
||||
import me.imsonmia.epqapi.model.Message;
|
||||
import me.imsonmia.epqapi.model.User;
|
||||
import me.imsonmia.epqapi.repository.MessageRepository;
|
||||
import me.imsonmia.epqapi.repository.UserRepository;
|
||||
|
||||
@RestController
|
||||
|
@ -17,6 +22,8 @@ import me.imsonmia.epqapi.repository.UserRepository;
|
|||
public class UserController {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private MessageRepository messageRepository;
|
||||
@GetMapping("/user/{id}")
|
||||
public User getUserById(@PathVariable(value = "id") Long id) {
|
||||
return userRepository.findById(id).get();
|
||||
|
@ -34,4 +41,15 @@ public class UserController {
|
|||
) {
|
||||
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.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "message")
|
||||
public class Message {
|
||||
@GeneratedValue
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Id
|
||||
private Long id;
|
||||
private String from;
|
||||
private String to;
|
||||
private String content;
|
||||
private Long timestamp;
|
||||
Long id;
|
||||
String fromUserId;
|
||||
String toUserId;
|
||||
String content;
|
||||
Long timeMillis;
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getFrom() {
|
||||
return this.from;
|
||||
public String getFromUserId() {
|
||||
return fromUserId;
|
||||
}
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
public String getToUserId() {
|
||||
return toUserId;
|
||||
}
|
||||
public String getTo() {
|
||||
return to;
|
||||
public void setFromUserId(String fromUserId) {
|
||||
this.fromUserId = fromUserId;
|
||||
}
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
public void setToUserId(String toUserId) {
|
||||
this.toUserId = toUserId;
|
||||
}
|
||||
public String getContent() {
|
||||
return content;
|
||||
|
@ -37,10 +38,24 @@ public class Message {
|
|||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
public Long getTimestamp() {
|
||||
return timestamp;
|
||||
public Long getTimeMillis() {
|
||||
return timeMillis;
|
||||
}
|
||||
public void setTimestamp(Long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
public void setTimeMillis(Long timeMillis) {
|
||||
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
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Getter
|
||||
Long id;
|
||||
Long id;
|
||||
@Getter
|
||||
@Setter
|
||||
String userName;
|
||||
String userName;
|
||||
@Getter
|
||||
@Setter
|
||||
Date dateJoined;
|
||||
Date dateJoined;
|
||||
public User() {
|
||||
|
||||
}
|
||||
|
|
|
@ -7,3 +7,6 @@ import me.imsonmia.epqapi.model.User;
|
|||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
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.password=dbpasswd
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue