rajah 11 mesi fa
parent
commit
9ebd178d9d

+ 27 - 5
src/main/java/fr/triplea/demovote/dao/MessageRepository.java

@@ -6,15 +6,37 @@ import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.NativeQuery;
 import org.springframework.data.repository.query.Param;
 
+import fr.triplea.demovote.dto.MessageShort;
 import fr.triplea.demovote.model.Message;
 
 public interface MessageRepository extends JpaRepository<Message, Integer> 
 {
   
-  @NativeQuery("SELECT DISTINCT m.* FROM vote.messages AS m WHERE m.numero_mesage = :id ")
-  Message findById(@Param("id") int id);
-
-  @NativeQuery("SELECT DISTINCT m.* FROM vote.messages AS m WHERE m.numero_destinataire = :destinataire OR m.numero_participant = :participant OR m.numero_destinataire IS NULL ORDER BY m.date_creation DESC ")
-  List<Message> findAll(@Param("participant") int participant, @Param("destinataire") int destinataire);
+  @NativeQuery("SELECT DISTINCT "
+             + "  TO_CHAR(m.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
+             + "  m.numero_message, "
+             + "  p.pseudonyme, "
+             + "  m.ligne, "
+             + "  m.numero_destinataire "
+             + "FROM vote.messages AS m "
+             + "INNER JOIN vote.participants AS p ON m.numero_participant = p.numero_participant "
+             + "WHERE "
+             + "  (m.numero_destinataire = :participant) OR (m.numero_participant = :participant) OR (m.numero_destinataire IS NULL) "
+             + "ORDER BY m.numero_message DESC ")
+  List<MessageShort> findAll(@Param("participant") int participant);
   
+  @NativeQuery("SELECT DISTINCT "
+             + "  TO_CHAR(m.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
+             + "  m.numero_message, "
+             + "  p.pseudonyme, "
+             + "  m.ligne, "
+             + "  m.numero_destinataire "
+             + "FROM vote.messages AS m "
+             + "INNER JOIN vote.participants AS p ON m.numero_participant = p.numero_participant "
+             + "WHERE "
+             + "     :last < m.numero_message "
+             + " AND (m.numero_destinataire = :participant) OR (m.numero_participant = :participant) OR (m.numero_destinataire IS NULL) "
+             + "ORDER BY m.numero_message DESC ")
+  List<MessageShort> findNew(@Param("participant") int participant, @Param("last") int last);
+
 }

+ 0 - 1
src/main/java/fr/triplea/demovote/dao/ProductionRepository.java

@@ -10,7 +10,6 @@ import fr.triplea.demovote.dto.ProductionFile;
 import fr.triplea.demovote.dto.ProductionShort;
 import fr.triplea.demovote.model.Participant;
 import fr.triplea.demovote.model.Production;
-import fr.triplea.demovote.model.ProductionType;
 
 
 public interface ProductionRepository extends JpaRepository<Production, Integer> 

+ 12 - 0
src/main/java/fr/triplea/demovote/dto/MessageShort.java

@@ -0,0 +1,12 @@
+package fr.triplea.demovote.dto;
+
+public record MessageShort
+(
+  String dateCreation,  
+  int numeroMessage,
+  String pseudonyme,
+  String ligne,
+  int numeroDestinataire
+) 
+{
+}

+ 4 - 4
src/main/java/fr/triplea/demovote/security/jwt/JwtTokenUtil.java

@@ -63,10 +63,10 @@ public class JwtTokenUtil
      
       return true;
     }
-    catch(UnsupportedJwtException exp) { LOG.error("claimsJws argument does not represent Claims JWS" + exp.getMessage()); }
-    catch(MalformedJwtException exp) { LOG.error("claimsJws string is not a valid JWS" + exp.getMessage()); }
-    catch(ExpiredJwtException exp) { LOG.error("Claims has an expiration time before the method is invoked" + exp.getMessage()); }
-    catch(IllegalArgumentException exp) { LOG.error("claimsJws string is null or empty or only whitespace" + exp.getMessage()); }
+    catch(UnsupportedJwtException exp) { LOG.error("claimsJws argument does not represent Claims JWS; " + exp.getMessage()); }
+    catch(MalformedJwtException exp) { LOG.error("claimsJws string is not a valid JWS; " + exp.getMessage()); }
+    catch(ExpiredJwtException exp) { /*LOG.info("Claims has an expiration time before the method is invoked; " + exp.getMessage());*/ }
+    catch(IllegalArgumentException exp) { LOG.error("claimsJws string is null or empty or only whitespace; " + exp.getMessage()); }
     
     return false;
   }

+ 81 - 6
src/main/java/fr/triplea/demovote/web/controller/MessageController.java

@@ -2,32 +2,107 @@ package fr.triplea.demovote.web.controller;
 
 import java.util.List;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.security.core.Authentication;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import fr.triplea.demovote.dao.MessageRepository;
+import fr.triplea.demovote.dao.ParticipantRepository;
+import fr.triplea.demovote.dto.MessageShort;
 import fr.triplea.demovote.model.Message;
+import fr.triplea.demovote.model.Participant;
 
 @RestController
-@RequestMapping("/message")
+@RequestMapping("/chat")
 public class MessageController 
 {
-
   // TODO
 
+  @SuppressWarnings("unused") 
+  private static final Logger LOG = LoggerFactory.getLogger(AccountController.class);
+
   @Autowired
   private MessageRepository messageRepository;
+
+  @Autowired
+  private ParticipantRepository participantRepository;
+ 
   
-  
-  @GetMapping(value = "/list/{id}")
+  @GetMapping(value = "/list")
+  @PreAuthorize("hasRole('USER')")
+  public List<MessageShort> getList(final Authentication authentication)
+  { 
+    if (authentication != null)
+    {
+      Participant found = participantRepository.findByPseudonyme(authentication.getName());
+      
+      if (found != null) 
+      { 
+        return messageRepository.findAll(found.getNumeroParticipant());
+      }
+    }
+    
+    return null; 
+  }
+
+  @GetMapping(value = "/new/{last}")
   @PreAuthorize("hasRole('USER')")
-  public List<Message> getList(@PathVariable int id)
+  public List<MessageShort> getNew(@PathVariable int last, final Authentication authentication)
   { 
-    return messageRepository.findAll(id, id); 
+    if (authentication != null)
+    {
+      Participant found = participantRepository.findByPseudonyme(authentication.getName());
+      
+      if ((found != null) && (last >= 0)) 
+      { 
+        return messageRepository.findNew(found.getNumeroParticipant(), last);
+      }
+    }
+    
+    return null; 
   }
 
+
+  @GetMapping(value = "/add/{last}")
+  @PreAuthorize("hasRole('USER')")
+  public List<MessageShort> addMessage(MessageShort message, @PathVariable int last, final Authentication authentication)
+  { 
+    if ((authentication != null) && (message != null))
+    {
+      Participant found = participantRepository.findByPseudonyme(authentication.getName());
+      
+      if ((found != null) && (last >= 0) && (authentication.getName().equals(message.pseudonyme()))) 
+      { 
+        String ligne = message.ligne();
+        
+        if (ligne == null) { ligne = ""; }
+        
+        if (!ligne.isBlank())
+        {
+          Message m = new Message();
+          
+          m.setNumeroMessage(null);
+          m.setParticipant(found);
+          m.setLigne(ligne);
+          
+          Participant destinataire = participantRepository.findById(message.numeroDestinataire());
+          
+          if (destinataire != null) { m.setDestinataire(destinataire); } else { m.setDestinataire(null); }
+          
+          messageRepository.save(m);
+        }
+        
+        return messageRepository.findNew(found.getNumeroParticipant(), last);
+      }
+    }
+    
+    return null; 
+  }
+  
 }