rajah 1 год назад
Родитель
Сommit
8cd41422a4

+ 6 - 5
src/main/java/fr/triplea/demovote/persistence/dao/ProductionRepository.java

@@ -6,6 +6,7 @@ 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.persistence.dto.ProductionShort;
 import fr.triplea.demovote.persistence.model.Participant;
 import fr.triplea.demovote.persistence.model.Production;
 
@@ -15,11 +16,11 @@ public interface ProductionRepository extends JpaRepository<Production, Integer>
   @NativeQuery("SELECT DISTINCT p.* FROM vote.productions AS p WHERE p.numero_production = :id AND p.flag_actif IS TRUE ")
   Production findById(@Param("id") int id);
   
-  @NativeQuery("SELECT DISTINCT p.* FROM vote.productions AS p WHERE p.flag_actif IS TRUE ORDER BY p.titre ASC ")
-  List<Production> findAll();
-
-  @NativeQuery("SELECT DISTINCT p.* FROM vote.productions AS p WHERE p.numero_participant = :participant AND p.flag_actif IS TRUE ")
-  List<Production> findByParticipant(@Param("participant") Participant participant);
+  @NativeQuery("SELECT DISTINCT p.numero_production, p.titre, p.auteurs, p.groupes, p.plateforme, p.commentaire, p.informations_privees, p.numero_participant, p.nom_archive, p.vignette, p.numero_version FROM vote.productions AS p WHERE p.flag_actif IS TRUE ORDER BY p.titre ASC ")
+  List<ProductionShort> findAllEnabled();
+  
+  @NativeQuery("SELECT DISTINCT p.date_creation, p.date_modification, p.numero_production, p.adresse_ip, p.type, p.titre, p.auteurs, p.groupes, p.plateforme, p.commentaire, p.informations_privees, p.numero_participant, p.nom_archive, p.vignette, p.numero_version FROM vote.productions AS p WHERE p.numero_participant = :participant AND p.flag_actif IS TRUE ")
+  List<ProductionShort> findByParticipant(@Param("participant") Participant participant);
 
   @Override
   void delete(Production production);

+ 8 - 1
src/main/java/fr/triplea/demovote/persistence/dto/ParticipantOptionList.java

@@ -1,3 +1,10 @@
 package fr.triplea.demovote.persistence.dto;
 
-public record ParticipantOptionList(Integer numeroParticipant, String pseudonyme, String nom, String prenom) { }
+public record ParticipantOptionList
+(
+  Integer numeroParticipant, 
+  String pseudonyme, 
+  String nom, 
+  String prenom
+) 
+{ }

+ 19 - 0
src/main/java/fr/triplea/demovote/persistence/dto/ProductionDTO.java

@@ -0,0 +1,19 @@
+package fr.triplea.demovote.persistence.dto;
+
+public record ProductionDTO
+(
+  int numeroProduction,
+  String type,
+  String titre,
+  String auteurs,
+  String groupes,
+  String plateforme,
+  String commentaire,
+  String informationsPrivees,
+  int numeroParticipant,
+  String nomArchive,
+  String archive,
+  String vignette,
+  int numeroVersion
+) 
+{ }

+ 44 - 0
src/main/java/fr/triplea/demovote/persistence/dto/ProductionShort.java

@@ -0,0 +1,44 @@
+package fr.triplea.demovote.persistence.dto;
+
+import java.time.LocalDateTime;
+
+import fr.triplea.demovote.persistence.model.Production;
+
+public record ProductionShort
+(
+  //LocalDateTime dateCreation,
+  //LocalDateTime dateModification,
+  Integer numeroProduction,
+  String titre,
+  String auteurs,
+  String groupes,
+  String plateforme,
+  String commentaire,
+  String informationsPrivees,
+  Integer numeroParticipant,
+  String nomArchive,
+  byte[] vignette,
+  Integer numeroVersion
+) 
+{ 
+  public Production toProduction() 
+  {
+    Production p = new Production();
+    
+    //p.setDateCreation(dateCreation);
+    //p.setDateModification(dateModification);
+    p.setNumeroProduction(numeroProduction);
+    p.setAdresseIP(null);
+    p.setTitre(titre);
+    p.setAuteurs(auteurs);
+    p.setGroupes(groupes);
+    p.setPlateforme(plateforme);
+    p.setCommentaire(commentaire);
+    p.setInformationsPrivees(informationsPrivees);
+    p.setNomArchive(nomArchive);
+    p.setVignette(vignette);
+    p.setNumeroVersion(numeroVersion);
+    
+    return p;
+  }  
+}

+ 5 - 1
src/main/java/fr/triplea/demovote/persistence/dto/VariableTypeOptionList.java

@@ -1,3 +1,7 @@
 package fr.triplea.demovote.persistence.dto;
 
-public record VariableTypeOptionList(String type) { }
+public record VariableTypeOptionList
+(
+  String type
+)
+{ }

+ 5 - 2
src/main/java/fr/triplea/demovote/persistence/model/Production.java

@@ -144,8 +144,10 @@ public class Production
   public Production() { super(); }
 
   
+  public void setDateCreation(LocalDateTime d) { this.dateCreation = d; }
   public LocalDateTime getDateCreation() { return this.dateCreation; }
   
+  public void setDateModification(LocalDateTime d) { this.dateModification = d; }
   public LocalDateTime getDateModification() { return this.dateModification; }
   
   public void setNumeroProduction(Integer numeroProduction) { this.numeroProduction = numeroProduction; }
@@ -192,7 +194,7 @@ public class Production
   
     try { this.archive = Base64.getDecoder().decode(a); } catch(Exception e) { this.archive = null; }
   }
-  public String getArchive() { return "data:application/zip;base64," + Base64.getEncoder().encodeToString(this.archive); }
+  public String getArchive() { if (this.archive == null) { return ""; } return "data:application/zip;base64," + Base64.getEncoder().encodeToString(this.archive); }
   
   public void setVignette(String v) 
   { 
@@ -222,7 +224,8 @@ public class Production
     } 
     catch(Exception e) { this.vignette = null; }
   }
-  public String getVignette() { return "data:image/png;base64," + Base64.getEncoder().encodeToString(this.vignette); }
+  public void setVignette(byte[] a) { this.vignette = (a == null) ? null : a.clone(); }
+  public String getVignette() { if (this.vignette == null) { return ""; } return "data:image/png;base64," + Base64.getEncoder().encodeToString(this.vignette); }
  
   //@Version
   public void setNumeroVersion(int n) { this.numeroVersion = Integer.valueOf(n); }

+ 52 - 12
src/main/java/fr/triplea/demovote/web/controller/ProductionController.java

@@ -1,6 +1,7 @@
 package fr.triplea.demovote.web.controller;
 
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -19,7 +20,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import fr.triplea.demovote.persistence.dao.ParticipantRepository;
 import fr.triplea.demovote.persistence.dao.ProductionRepository;
+import fr.triplea.demovote.persistence.dto.ProductionDTO;
+import fr.triplea.demovote.persistence.dto.ProductionShort;
+import fr.triplea.demovote.persistence.model.Participant;
 import fr.triplea.demovote.persistence.model.Production;
 import fr.triplea.demovote.persistence.model.ProductionType;
 import io.hypersistence.utils.hibernate.type.basic.Inet;
@@ -34,6 +39,9 @@ public class ProductionController
   @Autowired
   private ProductionRepository productionRepository;
 
+  @Autowired
+  private ParticipantRepository participantRepository;
+
   @Autowired 
   private HttpServletRequest request;
 
@@ -42,7 +50,12 @@ public class ProductionController
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
   public List<Production> getList(@RequestParam(required = false) String type) 
   { 
-    return productionRepository.findAll(); 
+    List<ProductionShort> prods = productionRepository.findAllEnabled();
+    List<Production> ret = new ArrayList<Production>();
+    
+    for (ProductionShort prod: prods) { ret.add(prod.toProduction()); }
+    
+    return ret; 
   }
  
   @GetMapping(value = "/form/{id}")
@@ -58,19 +71,46 @@ public class ProductionController
 
   @PostMapping(value = "/create")
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
-  public Production create(@RequestBody(required = true) Production production, HttpServletRequest request) 
+  public ResponseEntity<Map<String, Boolean>> create(@RequestBody(required = true) ProductionDTO prod_dto, HttpServletRequest request) 
   { 
-    Production found = productionRepository.findById(0);
-    
-    if (found == null) { production.setNumeroProduction(null); }
+    Participant participant = participantRepository.findById(prod_dto.numeroParticipant());
 
-    production.setAdresseIP(new Inet(request.getRemoteAddr()));
-    
-    if (production.getType() == null) { production.setType(ProductionType.AUTRE); }
-    
-    if (production.getNumeroVersion() == null) { production.setNumeroVersion(1); }
-    
-    return productionRepository.save(production);
+    if (participant != null) 
+    {
+      Production prod_new = new Production();
+            
+      prod_new.setNumeroProduction(null);
+      prod_new.setAdresseIP(new Inet(request.getRemoteAddr()));
+      
+      if(prod_dto.type().equals("EXECUTABLE")) { prod_new.setType(ProductionType.EXECUTABLE); }
+      else if(prod_dto.type().equals("GRAPHE")) { prod_new.setType(ProductionType.GRAPHE); }
+      else if(prod_dto.type().equals("MUSIQUE")) { prod_new.setType(ProductionType.MUSIQUE); }
+      else if(prod_dto.type().equals("VIDEO")) { prod_new.setType(ProductionType.VIDEO); }
+      else if(prod_dto.type().equals("TOPIC")) { prod_new.setType(ProductionType.TOPIC); }
+      else { prod_new.setType(ProductionType.AUTRE); }
+        
+      prod_new.setTitre(prod_dto.titre());
+      prod_new.setAuteurs(prod_dto.auteurs());
+      prod_new.setGroupes(prod_dto.groupes());
+      prod_new.setPlateforme(prod_dto.plateforme());
+      prod_new.setCommentaire(prod_dto.commentaire());
+      prod_new.setInformationsPrivees(prod_dto.informationsPrivees());
+
+      prod_new.setParticipant(participant);
+      prod_new.setNomArchive(prod_dto.nomArchive());
+      prod_new.setArchive(prod_dto.archive());
+      prod_new.setVignette(prod_dto.vignette());
+      prod_new.setNumeroVersion(prod_dto.numeroVersion());
+      
+      productionRepository.save(prod_new);
+
+      Map<String, Boolean> response = new HashMap<>();
+      response.put("created", Boolean.TRUE);
+      
+      return ResponseEntity.ok(response); 
+    }
+
+    return ResponseEntity.notFound().build(); 
   }
  
   @PutMapping(value = "/update/{id}")