enchere done

This commit is contained in:
Parpaillax
2024-05-02 11:57:16 +02:00
parent 8788aecb39
commit f46848644d
18 changed files with 13477 additions and 23 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Controller()
@RequestMapping("/enchere")
@@ -62,15 +63,19 @@ public class EnchereController {
}
//Empeche l'enchère si les crédits du user sont inférieur au prix initial de l'article
float userCredit = this.userService.utilisateurByName(authentication.getName()).getCredit();
if (userCredit < encherePrice) {
UserProfil userCredit = this.userService.utilisateurByName(authentication.getName());
List<Enchere> encheres = this.enchereService.enchereByUser(userCredit.getId());
double encheresSum = encheres.stream()
.mapToDouble(ench -> ench.getMontantEnchere()) // Mappez chaque enchère à son montant
.sum();
if ((userCredit.getCredit() - encheresSum) < encherePrice) {
result.rejectValue("montantEnchere", "error.enchere", "Vous ne pouvez pas enchérir si vous n'avez pas les fonds suffisant");
}
Optional<String> pseudoMaxEnchere = lastEnchere.stream()
.max(Comparator.comparing(Enchere::getMontantEnchere)) // Comparaison basée sur le montant d'enchère
.map(Enchere::getPseudoUtilisateur);
if (pseudoMaxEnchere.get().equals(authentication.getName())) {
if (pseudoMaxEnchere.isPresent() && pseudoMaxEnchere.get().equals(authentication.getName())) {
result.rejectValue("montantEnchere", "error.enchere", "Vous ne pouvez pas enchèrir sur votre propre offre");
}
@@ -80,9 +85,38 @@ public class EnchereController {
}
this.enchereService.setEnchere(enchere);
float newCredit = userCredit - enchere.getMontantEnchere();
this.userService.setCredit(newCredit, this.userService.utilisateurByName(authentication.getName()).getId());
return "redirect:/article/show?id=" + articleId;
}
@PostMapping("/enchereDone")
public String enchereDone(@RequestParam("id") int id) {
List<Enchere> listEncheres = this.enchereService.enchereByArticle(id);
List<Enchere> encheres = listEncheres.stream()
.sorted(Comparator.comparing(Enchere::getMontantEnchere).reversed())
.collect(Collectors.toList());
Optional<String> pseudoMaxEnchere = encheres.stream()
.max(Comparator.comparing(Enchere::getMontantEnchere)) // Comparaison basée sur le montant d'enchère
.map(Enchere::getPseudoUtilisateur);
UserProfil user = this.userService.utilisateurByName(pseudoMaxEnchere.get());
Optional<Float> maxMontantEnchere = listEncheres.stream()
.map(Enchere::getMontantEnchere) // Récupère seulement les montants d'enchère
.max(Float::compareTo);
//setCredit user.
float userCredit = user.getCredit();
float newCredit = userCredit - maxMontantEnchere.get();
this.userService.setCredit(newCredit, user.getId());
//Delete enchere
for (Enchere ench : listEncheres) {
this.enchereService.deleteEnchere(ench.getId());
}
//Delete article
this.articleService.setSellPrice(id, maxMontantEnchere.get());
return "redirect:/enchere";
}
}