adjustments to entities, service, repo and db layers

This commit is contained in:
Rohan Sircar 2020-01-31 10:16:13 +05:30
parent f34a5524fd
commit f6576026a2
15 changed files with 119 additions and 94 deletions

View File

@ -308,7 +308,7 @@
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/> <setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" value="1"/> <setting id="org.eclipse.jdt.core.formatter.blank_lines_between_import_groups" value="1"/>
<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="120"/> <setting id="org.eclipse.jdt.core.formatter.lineSplit" value="80"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation" value="do not insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch" value="insert"/> <setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch" value="insert"/>
</profile> </profile>

View File

@ -66,7 +66,7 @@ public class RegistrationController {
logger.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID())); logger.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID()));
if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) {
logger.info("Registration captcha equal success"); logger.info("Registration captcha equal success");
userService.registerUser(userRegistrationDTO); userService.createUser(userRegistrationDTO);
return "redirect:registration?success"; return "redirect:registration?success";
} else { } else {
logger.warn("Registration captcha equal fail"); logger.warn("Registration captcha equal fail");

View File

@ -1,21 +0,0 @@
package org.ros.chatto.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter
@Setter
@Table(name="status")
public class ApplicationStatus {
@Id
private int id;
private String name;
@Column(name="value")
private boolean done;
}

View File

@ -9,6 +9,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
@ -24,15 +25,19 @@ public class ChatMessage {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "m_id") @Column(name = "m_id")
private Long messageID; private Long messageID;
@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_user") @JoinColumn(name = "from_user")
private ChatUser fromUser; private ChatUser fromUser;
@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "to_user") @JoinColumn(name = "to_user")
private ChatUser toUser; private ChatUser toUser;
@OneToOne(fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "message") @JoinColumn(name = "message")
private MessageCipher messageCipher; private MessageCipher messageCipher;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date messageTime; private Date messageTime;
} }

View File

@ -1,6 +1,7 @@
package org.ros.chatto.model; package org.ros.chatto.model;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
@ -22,7 +23,9 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity @Entity
@Table(name = "users") @Table(name = "users")
@ -35,18 +38,26 @@ import lombok.NoArgsConstructor;
public class ChatUser { public class ChatUser {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
// @SequenceGenerator(name="user_generator", sequenceName = "user_seq", allocationSize=50) //mysql does not support sequence id generator
@Column(name = "user_id") @Column(name = "user_id")
private int userID; private int userID;
@Column(name = "name") @Column(name = "name")
private String userName; private String userName;
String password; String password;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date joinDate; private Date joinDate;
// @ManyToMany(cascade = CascadeType.ALL)
// @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) @ToString.Exclude
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL) @EqualsAndHashCode.Exclude
@OneToMany(mappedBy = "user", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH,
CascadeType.REFRESH })
@JsonBackReference @JsonBackReference
// private Set<UserRole> userRoles = new HashSet<UserRole>();
private Set<UserRole> userRoles; private Set<UserRole> userRoles;
// @ToString.Exclude
// @EqualsAndHashCode.Exclude
// @OneToMany(mappedBy = "fromUser", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH,
// CascadeType.REFRESH })
// private List<ChatMessage> chatMessages;
} }

View File

@ -13,6 +13,8 @@ import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Entity @Entity
@ -22,10 +24,16 @@ public class Role {
@Id @Id
@Column(name = "role_id") @Column(name = "role_id")
private int roleID; private int roleID;
@Column(name = "role_name") @Column(name = "role_name")
private String name; private String name;
private String description; private String description;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "role", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH,
CascadeType.REFRESH })
@JsonBackReference @JsonBackReference
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Set<UserRole> userRoles = new HashSet<>(); private Set<UserRole> userRoles = new HashSet<>();
} }

View File

@ -2,7 +2,6 @@ package org.ros.chatto.model;
import java.time.Instant; import java.time.Instant;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -22,7 +21,7 @@ public class UserSession {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
private ChatUser user; private ChatUser user;

View File

@ -23,7 +23,7 @@ public class UserToken implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "token_id") @Column(name = "token_id")
private long tokenID; private int tokenID;
private String userName; private String userName;
private String tokenContent; private String tokenContent;
private String role; private String role;

View File

@ -12,6 +12,9 @@ public interface UserRepository extends JpaRepository<ChatUser, Long>{
@Query("select cu from ChatUser cu where cu.userName = ?1") @Query("select cu from ChatUser cu where cu.userName = ?1")
public ChatUser findByUserName(String userName); public ChatUser findByUserName(String userName);
@Query("select cu from ChatUser cu join fetch cu.userRoles where cu.userName = ?1")
public ChatUser findByUserNameWithRole(String userName);
@Query("select cu.userName from ChatUser cu where cu.userName != ?1") @Query("select cu.userName from ChatUser cu where cu.userName != ?1")
public List<String> findAllOtherUserNames(String userName); public List<String> findAllOtherUserNames(String userName);
} }

View File

@ -1,6 +1,6 @@
package org.ros.chatto.security; package org.ros.chatto.security;
import java.util.List; import java.util.Set;
import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserRole; import org.ros.chatto.model.UserRole;
@ -18,23 +18,26 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class MyUserDetailsService implements UserDetailsService { public class MyUserDetailsService implements UserDetailsService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Override @Override
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(final String username) {
log.trace("User Details - loading with username: {}", username); log.trace("User Details - loading with username: {}", username);
List<UserRole> userRoles = userService.getUserWithRole(username); ChatUser user = userService.getUserWithRole(username);
if (userRoles.size() == 0) {
if (user == null) {
log.warn("Request for unknown user {}", username); log.warn("Request for unknown user {}", username);
throw new UsernameNotFoundException(username); throw new UsernameNotFoundException(username);
} }
ChatUser user = userRoles.get(0).getUser();
return User.withUsername(user.getUserName()).password(user.getPassword()) Set<UserRole> userRoles = user.getUserRoles();
.roles(userRoles.stream().map(userRole -> {
log.trace("role = " + userRole.getRole().getName()); return User.withUsername(user.getUserName())
return userRole.getRole().getName(); .password(user.getPassword())
}).toArray(size -> new String[size])).build(); .roles(userRoles.stream()
.map(ur -> ur.getRole().getName())
.toArray(size -> new String[size]))
.build();
} }
} }

View File

@ -29,10 +29,11 @@ public class ChatServiceImpl implements ChatService {
private final ChatMessageRepository chatMessageRepository; private final ChatMessageRepository chatMessageRepository;
private final MyConversionService myConversionService; private final MyConversionService myConversionService;
public ChatMessageDTO createMessage(final String fromUserName, final String toUserName, public ChatMessageDTO createMessage(final String fromUserName,
final MessageCipherDTO messageCipherDTO) { final String toUserName, final MessageCipherDTO messageCipherDTO) {
MessageCipher messageCipher = myConversionService.convertToMessageCipher(messageCipherDTO); MessageCipher messageCipher = myConversionService
.convertToMessageCipher(messageCipherDTO);
final ChatUser fromUser = userRepository.findByUserName(fromUserName); final ChatUser fromUser = userRepository.findByUserName(fromUserName);
final ChatUser toUser = userRepository.findByUserName(toUserName); final ChatUser toUser = userRepository.findByUserName(toUserName);
@ -49,51 +50,53 @@ public class ChatServiceImpl implements ChatService {
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<ChatMessageDTO> getAllMessages(final String fromUser, final String toUser) { public List<ChatMessageDTO> getAllMessages(final String fromUser,
final List<ChatMessage> chatMessages = chatMessageRepository.getAllMessages(fromUser, toUser); final String toUser) {
final List<ChatMessageDTO> chatMessageDTOs = myConversionService.convertToChatMessageDTOs(chatMessages); final List<ChatMessage> chatMessages = chatMessageRepository
.getAllMessages(fromUser, toUser);
final List<ChatMessageDTO> chatMessageDTOs = myConversionService
.convertToChatMessageDTOs(chatMessages);
return chatMessageDTOs; return chatMessageDTOs;
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<ReencryptionDTO> getAllMessagesForReencryption(final String fromUser, final String toUser) { public List<ReencryptionDTO> getAllMessagesForReencryption(
return myConversionService.convertToReencryptionDTOs(chatMessageRepository.getAllMessages(fromUser, toUser)); final String fromUser, final String toUser) {
return myConversionService.convertToReencryptionDTOs(
chatMessageRepository.getAllMessages(fromUser, toUser));
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<ChatMessageDTO> getMessagePage(final String fromUser, final String toUser, final int page, public List<ChatMessageDTO> getMessagePage(final String fromUser,
final int size) { final String toUser, final int page, final int size) {
// Sort sort = Sort
// Page<ChatMessage> chatMessages =
// chatMessageRepository.getAllMessages(fromUser, toUser,PageRequest.of(page,
// size));
// List<ChatMessageDTO> chatMessageDTOs =
// myConversionService.convertToChatMessageDTOs(chatMessages);
// return chatMessageDTOs;
return myConversionService.convertToChatMessageDTOs( return myConversionService.convertToChatMessageDTOs(
chatMessageRepository.getAllMessages(fromUser, toUser, PageRequest.of(page, size))); chatMessageRepository.getAllMessages(fromUser, toUser,
PageRequest.of(page, size)));
} }
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<ChatMessageDTO> getNewMessages(final String fromUser, final String toUser, final Date lastMessageTime) { public List<ChatMessageDTO> getNewMessages(final String fromUser,
final List<ChatMessage> chatMessages = chatMessageRepository.getNewMessages(fromUser, toUser, lastMessageTime); final String toUser, final Date lastMessageTime) {
// List<ChatMessageDTO> chatMessageDTOs final List<ChatMessage> chatMessages = chatMessageRepository
.getNewMessages(fromUser, toUser, lastMessageTime);
return myConversionService.convertToChatMessageDTOs(chatMessages); return myConversionService.convertToChatMessageDTOs(chatMessages);
} }
@Override @Override
public void reencryptMessages(final List<ReencryptionDTO> reencryptionDTOs) { public void reencryptMessages(
final List<ReencryptionDTO> reencryptionDTOs) {
final List<MessageCipher> messageCiphers = reencryptionDTOs.stream() final List<MessageCipher> messageCiphers = reencryptionDTOs.stream()
.map(reencryptionDTO -> reencryptionDTO.getMessageCipher()).collect(Collectors.toList()); .map(reencryptionDTO -> reencryptionDTO.getMessageCipher())
.collect(Collectors.toList());
messageCipherRepository.saveAll(messageCiphers); messageCipherRepository.saveAll(messageCiphers);
} }
@Override @Override
public List<ChatMessageDTO> getAllMessages(final String name, final String userName, public List<ChatMessageDTO> getAllMessages(final String name,
final PageRequest pageRequest) { final String userName, final PageRequest pageRequest) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }

View File

@ -1,22 +1,32 @@
package org.ros.chatto.service; package org.ros.chatto.service;
import java.util.List; import java.util.List;
import java.util.Set;
import org.ros.chatto.dto.ActiveUserDTO; import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.dto.UserRegistrationDTO; import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserRole; import org.ros.chatto.model.Role;
import org.ros.chatto.model.UserSession; import org.ros.chatto.model.UserSession;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public interface UserService { public interface UserService {
public List<String> findAllOtherUsers(String userName); public List<String> findAllOtherUsers(String userName);
public UserRole registerUser(UserRegistrationDTO userRegistrationDTO);
public ChatUser createUser(UserRegistrationDTO userRegistrationDTO);
public List<String> getAllRegularUsers(); public List<String> getAllRegularUsers();
public ChatUser findByUserName(String userName);
public ChatUser getUser(String userName);
public Set<Role> getRoles(ChatUser user);
public List<ActiveUserDTO> getOtherActiveUsers(String userName); public List<ActiveUserDTO> getOtherActiveUsers(String userName);
public List<UserRole> getUserWithRole(String userName);
public ChatUser getUserWithRole(String userName);
public UserSession incrementUserSession(String userName); public UserSession incrementUserSession(String userName);
public UserSession decrementUserSession(String userName); public UserSession decrementUserSession(String userName);
} }

View File

@ -6,6 +6,8 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.ros.chatto.dto.ActiveUserDTO; import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.dto.UserRegistrationDTO; import org.ros.chatto.dto.UserRegistrationDTO;
@ -44,7 +46,7 @@ public class UserServiceImpl implements UserService {
private final UserTokenService userTokenService; private final UserTokenService userTokenService;
@Override @Override
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) { public ChatUser createUser(final UserRegistrationDTO userRegistrationDTO) {
final ChatUser user = new ChatUser(); final ChatUser user = new ChatUser();
user.setUserName(userRegistrationDTO.getUserName()); user.setUserName(userRegistrationDTO.getUserName());
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword())); user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
@ -53,8 +55,7 @@ public class UserServiceImpl implements UserService {
final Role role = roleRepository.findByName("USER"); final Role role = roleRepository.findByName("USER");
userRole.setRole(role); userRole.setRole(role);
userRole.setUser(changedUser); userRole.setUser(changedUser);
userRoleRepository.save(userRole); return userRoleRepository.save(userRole).getUser();
return userRole;
} }
@Override @Override
@ -97,7 +98,7 @@ public class UserServiceImpl implements UserService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Override @Override
public ChatUser findByUserName(final String userName) { public ChatUser getUser(final String userName) {
return userRepository.findByUserName(userName); return userRepository.findByUserName(userName);
} }
@ -148,13 +149,14 @@ public class UserServiceImpl implements UserService {
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<UserRole> getUserWithRole(final String userName) { public ChatUser getUserWithRole(final String userName) {
return userRoleRepository.findByUser(userName); return userRepository.findByUserNameWithRole(userName);
} }
@Override @Override
public UserSession incrementUserSession(String userName) { public UserSession incrementUserSession(String userName) {
ChatUser chatUser = findByUserName(userName); ChatUser chatUser = getUser(userName);
Instant instant = Instant.now();
UserSession userSession = userSessionRepository.findByUserName(userName); UserSession userSession = userSessionRepository.findByUserName(userName);
@ -163,6 +165,7 @@ public class UserServiceImpl implements UserService {
} }
userSession.setUser(chatUser); userSession.setUser(chatUser);
userSession.setTimeStamp(instant);
userSession.setOnline(true); userSession.setOnline(true);
userSession.setNumSessions(userSession.getNumSessions() + 1); userSession.setNumSessions(userSession.getNumSessions() + 1);
return userSessionRepository.save(userSession); return userSessionRepository.save(userSession);
@ -171,6 +174,7 @@ public class UserServiceImpl implements UserService {
@Override @Override
public UserSession decrementUserSession(String userName) { public UserSession decrementUserSession(String userName) {
UserSession userSession = userSessionRepository.findByUserName(userName); UserSession userSession = userSessionRepository.findByUserName(userName);
Instant instant = Instant.now();
if (userSession == null) { if (userSession == null) {
log.error("User session is somehow null for user: " + userName); log.error("User session is somehow null for user: " + userName);
@ -190,6 +194,12 @@ public class UserServiceImpl implements UserService {
} }
userSession.setNumSessions(numSessions); userSession.setNumSessions(numSessions);
userSession.setTimeStamp(instant);
return userSessionRepository.save(userSession); return userSessionRepository.save(userSession);
} }
@Override
public Set<Role> getRoles(ChatUser user) {
return user.getUserRoles().stream().map(ur -> ur.getRole()).collect(Collectors.toSet());
}
} }

View File

@ -11,7 +11,7 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDial
# Hibernate ddl auto (create, create-drop, validate, update) # Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = none spring.jpa.hibernate.ddl-auto = validate
spring.jpa.open-in-view=false spring.jpa.open-in-view=false
logging.level.org.hibernate.stat=debug logging.level.org.hibernate.stat=debug

View File

@ -47,7 +47,7 @@ CREATE TABLE IF NOT EXISTS `users_roles` (
KEY `user` (`user_id`), KEY `user` (`user_id`),
KEY `role` (`role_id`), KEY `role` (`role_id`),
CONSTRAINT `FOREIGN KEY USER IN USERS-ROLES TABLE` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FOREIGN KEY USER IN USERS-ROLES TABLE` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_roles_roleAssignments` FOREIGN KEY (`role_id`) REFERENCES `roles` (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE CONSTRAINT `fk_roles_roleAssignments` FOREIGN KEY (`role_id`) REFERENCES `roles` (`role_id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `chat_messages` ( CREATE TABLE IF NOT EXISTS `chat_messages` (
@ -64,17 +64,10 @@ CREATE TABLE IF NOT EXISTS `chat_messages` (
KEY `FOREIGN KEY TO USER IN MESSAGES TABLE` (`to_user`), KEY `FOREIGN KEY TO USER IN MESSAGES TABLE` (`to_user`),
CONSTRAINT `FOREIGN KEY ENC MESSAGE TABLE` FOREIGN KEY (`message`) REFERENCES `message_ciphers` (`id`) ON UPDATE CASCADE, CONSTRAINT `FOREIGN KEY ENC MESSAGE TABLE` FOREIGN KEY (`message`) REFERENCES `message_ciphers` (`id`) ON UPDATE CASCADE,
CONSTRAINT `FOREIGN KEY FROM USER IN MESSAGES TABLE` FOREIGN KEY (`from_user`) REFERENCES `users` (`user_id`) ON UPDATE CASCADE, CONSTRAINT `FOREIGN KEY FROM USER IN MESSAGES TABLE` FOREIGN KEY (`from_user`) REFERENCES `users` (`user_id`) ON UPDATE CASCADE,
CONSTRAINT `FOREIGN KEY TO USER IN MESSAGES TABLE` FOREIGN KEY (`to_user`) REFERENCES `users` (`user_id`) ON UPDATE CASCADE CONSTRAINT `FOREIGN KEY TO USER IN MESSAGES TABLE` FOREIGN KEY (`to_user`) REFERENCES `users` (`user_id`) ON UPDATE CASCADE,
UNIQUE KEY `UNIQUE MESSAGE CIPHER` (`message`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `status` (
`id` int(2) NOT NULL AUTO_INCREMENT,
`name` varchar(15) NOT NULL,
`value` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `user_sessions` ( CREATE TABLE IF NOT EXISTS `user_sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(15) NOT NULL, `user_id` int(15) NOT NULL,
@ -83,7 +76,8 @@ CREATE TABLE IF NOT EXISTS `user_sessions` (
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `user_name` (`user_id`), KEY `user_name` (`user_id`),
CONSTRAINT `FOREIGN KEY USER ID` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE CONSTRAINT `FOREIGN KEY USER ID` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE KEY `UNIQUE USER CONSTRAINT` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `tokens` ( CREATE TABLE IF NOT EXISTS `tokens` (