rajah 11 месяцев назад
Родитель
Сommit
adf4de7ddf

+ 6 - 17
src/main/java/fr/triplea/demovote/dao/MessageRepository.java

@@ -11,31 +11,20 @@ import fr.triplea.demovote.model.Message;
 
 public interface MessageRepository extends JpaRepository<Message, Integer> 
 {
-  
-  @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 "
+             + "  CASE WHEN m.numero_destinataire IS NULL THEN 0 ELSE m.numero_destinataire END AS numero_destinataire, "
+             + "  CASE WHEN m.numero_destinataire IS NULL THEN '' ELSE d.pseudonyme END AS pseudo_destinataire "
              + "FROM vote.messages AS m "
              + "INNER JOIN vote.participants AS p ON m.numero_participant = p.numero_participant "
+             + "LEFT JOIN vote.participants AS d ON m.numero_destinataire = d.numero_participant "
              + "WHERE "
-             + "     :last < m.numero_message "
-             + " AND (m.numero_destinataire = :participant) OR (m.numero_participant = :participant) OR (m.numero_destinataire IS NULL) "
+             + "     (m.numero_message > :last) "
+             + " 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);
 

+ 5 - 1
src/main/java/fr/triplea/demovote/dao/ParticipantRepository.java

@@ -8,6 +8,7 @@ import org.springframework.data.repository.query.Param;
 
 import fr.triplea.demovote.dto.ParticipantList;
 import fr.triplea.demovote.dto.ParticipantOptionList;
+import fr.triplea.demovote.dto.PseudonymeOptionList;
 import fr.triplea.demovote.model.Participant;
 import fr.triplea.demovote.model.Role;
 
@@ -81,6 +82,9 @@ public interface ParticipantRepository extends JpaRepository<Participant, Intege
   void delete(Participant participant);
 
   @NativeQuery("SELECT DISTINCT p.numero_participant, p.pseudonyme, p.nom, p.prenom FROM vote.participants AS p WHERE p.flag_actif IS TRUE ORDER BY p.pseudonyme ASC, p.nom ASC, p.prenom ASC ")
-  List<ParticipantOptionList> getOptionList();
+  List<ParticipantOptionList> getParticipantOptionList();
+
+  @NativeQuery("SELECT DISTINCT p.numero_participant, p.pseudonyme FROM vote.participants AS p WHERE (p.flag_actif IS TRUE) AND (p.numero_participant <> :id) AND (LENGTH(p.pseudonyme) > 0) ORDER BY p.pseudonyme ASC ")
+  List<PseudonymeOptionList> getPseudonymeOptionList(@Param("id") int id);
 
 }

+ 2 - 1
src/main/java/fr/triplea/demovote/dto/MessageShort.java

@@ -6,7 +6,8 @@ public record MessageShort
   int numeroMessage,
   String pseudonyme,
   String ligne,
-  int numeroDestinataire
+  int numeroDestinataire,
+  String pseudoDestinataire
 ) 
 {
 }

+ 8 - 0
src/main/java/fr/triplea/demovote/dto/PseudonymeOptionList.java

@@ -0,0 +1,8 @@
+package fr.triplea.demovote.dto;
+
+public record PseudonymeOptionList
+(
+  Integer numeroParticipant, 
+  String pseudonyme
+) 
+{ }

+ 27 - 16
src/main/java/fr/triplea/demovote/web/controller/MessageController.java

@@ -1,5 +1,6 @@
 package fr.triplea.demovote.web.controller;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.slf4j.Logger;
@@ -9,12 +10,15 @@ 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.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 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.dto.PseudonymeOptionList;
 import fr.triplea.demovote.model.Message;
 import fr.triplea.demovote.model.Participant;
 
@@ -34,45 +38,50 @@ public class MessageController
   private ParticipantRepository participantRepository;
  
   
-  @GetMapping(value = "/list")
+  @GetMapping(value = "/nickname-list")
   @PreAuthorize("hasRole('USER')")
-  public List<MessageShort> getList(final Authentication authentication)
+  public List<PseudonymeOptionList> getOptionList(final Authentication authentication) 
   { 
     if (authentication != null)
     {
       Participant found = participantRepository.findByPseudonyme(authentication.getName());
-      
-      if (found != null) 
-      { 
-        return messageRepository.findAll(found.getNumeroParticipant());
+
+      if (found != null)
+      {
+        return participantRepository.getPseudonymeOptionList(found.getNumeroParticipant()); 
       }
     }
     
-    return null; 
+    return new ArrayList<PseudonymeOptionList>();
   }
 
   @GetMapping(value = "/new/{last}")
   @PreAuthorize("hasRole('USER')")
   public List<MessageShort> getNew(@PathVariable int last, final Authentication authentication)
   { 
+    List<MessageShort> mlist = null;
+
     if (authentication != null)
     {
       Participant found = participantRepository.findByPseudonyme(authentication.getName());
       
       if ((found != null) && (last >= 0)) 
-      { 
-        return messageRepository.findNew(found.getNumeroParticipant(), last);
+      {         
+        mlist = messageRepository.findNew(found.getNumeroParticipant(), last);
       }
     }
-    
-    return null; 
-  }
 
+    if (mlist == null) { mlist = new ArrayList<MessageShort>(); }
+        
+    return mlist; 
+  }
 
-  @GetMapping(value = "/add/{last}")
+  @PostMapping(value = "/add/{last}")
   @PreAuthorize("hasRole('USER')")
-  public List<MessageShort> addMessage(MessageShort message, @PathVariable int last, final Authentication authentication)
+  public List<MessageShort> addMessage(@RequestBody(required = true) MessageShort message, @PathVariable int last, final Authentication authentication)
   { 
+    List<MessageShort> mlist = null;
+
     if ((authentication != null) && (message != null))
     {
       Participant found = participantRepository.findByPseudonyme(authentication.getName());
@@ -98,11 +107,13 @@ public class MessageController
           messageRepository.save(m);
         }
         
-        return messageRepository.findNew(found.getNumeroParticipant(), last);
+        mlist = messageRepository.findNew(found.getNumeroParticipant(), last);
       }
     }
+
+    if (mlist == null) { mlist = new ArrayList<MessageShort>(); }
     
-    return null; 
+    return mlist; 
   }
   
 }

+ 1 - 1
src/main/java/fr/triplea/demovote/web/controller/ParticipantController.java

@@ -77,7 +77,7 @@ public class ParticipantController
   @PreAuthorize("hasRole('ORGA')")
   public List<ParticipantOptionList> getOptionList(final Authentication authentication) 
   { 
-    return participantRepository.getOptionList(); 
+    return participantRepository.getParticipantOptionList(); 
   }