enchere done
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -32,6 +32,7 @@ out/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
/src/main/resources/logs/eni.log
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
@@ -16,4 +16,5 @@ public interface ArticleService {
|
||||
int updateArticle(Article article);
|
||||
List<Article> findArticleByTitle(String title);
|
||||
Page<Article> searchArticlePageable(SearchArticleCritere critere, Pageable pageable);
|
||||
void setSellPrice(int id, float sellPrice);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,11 @@ public class ArticleServiceImpl implements ArticleService{
|
||||
return articleRepository.updateArticle(article);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSellPrice(int id, float sellPrice) {
|
||||
this.articleRepository.setSellPrice(id, sellPrice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Article> findArticleByTitle(String title) {
|
||||
return articleRepository.findArticleByTitle(title);
|
||||
|
||||
@@ -5,8 +5,8 @@ import fr.eni.enchere.bo.Enchere;
|
||||
import java.util.List;
|
||||
|
||||
public interface EnchereService {
|
||||
Enchere enchereByUserAndArticle(int idUser, int idArticle);
|
||||
List<Enchere> enchereByUser(int idUser);
|
||||
List<Enchere> enchereByArticle(int idArticle);
|
||||
void setEnchere(Enchere enchere);
|
||||
void deleteEnchere(int id);
|
||||
}
|
||||
|
||||
@@ -15,11 +15,6 @@ public class EnchereServiceImpl implements EnchereService{
|
||||
this.enchereRepository = enchereRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enchere enchereByUserAndArticle(int idUser, int idArticle) {
|
||||
return enchereRepository.findByIdUserAndIdArticle(idUser, idArticle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Enchere> enchereByUser(int idUser) {
|
||||
return enchereRepository.findByIdUser(idUser);
|
||||
@@ -34,4 +29,9 @@ public class EnchereServiceImpl implements EnchereService{
|
||||
public void setEnchere(Enchere enchere) {
|
||||
enchereRepository.save(enchere);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteEnchere(int id) {
|
||||
this.enchereRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ public class Enchere {
|
||||
|
||||
public Enchere(){}
|
||||
|
||||
public Enchere(int noUtilisateur, int noArticle, String pseudoUtilisateur, Date dateEnchere, float montantEnchere ) {
|
||||
public Enchere(int id, int noUtilisateur, int noArticle, String pseudoUtilisateur, Date dateEnchere, float montantEnchere ) {
|
||||
setId(id);
|
||||
setNoUtilisateur(noUtilisateur);
|
||||
setPseudoUtilisateur(pseudoUtilisateur);
|
||||
setNoArticle(noArticle);
|
||||
|
||||
@@ -79,7 +79,7 @@ public class ArticleController {
|
||||
.map(Enchere::getPseudoUtilisateur);
|
||||
|
||||
model.addAttribute("encheres", lastEnchere);
|
||||
model.addAttribute("maxEncherePseudo", pseudoMaxEnchere.orElse(""));
|
||||
model.addAttribute("maxEncherePseudo", pseudoMaxEnchere.orElse(null));
|
||||
model.addAttribute("isArticleCurrentUser", isArticleCurrentUser);
|
||||
model.addAttribute("article", article);
|
||||
model.addAttribute("username", user);
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,4 +15,5 @@ public interface ArticleRepository {
|
||||
int saveArticle(Article article);
|
||||
void deleteArticle(int id);
|
||||
int updateArticle(Article article);
|
||||
void setSellPrice(int id, float sellPrice);
|
||||
}
|
||||
|
||||
@@ -349,4 +349,10 @@ public class ArticleRepositoryImpl implements ArticleRepository {
|
||||
jdbcTemplate.update(sql, article.getNom(), article.getDesc(), article.getDateDebutEnch(), article.getDateFinEnch(), article.getPrixInitial(), article.getNumCategorie(), article.getId());
|
||||
return article.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSellPrice(int id, float price) {
|
||||
String sql = "UPDATE ARTICLES_VENDUS SET prix_vente = ?, isDelete = 1 WHERE no_article = ?";
|
||||
jdbcTemplate.update(sql, price, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import fr.eni.enchere.bo.Enchere;
|
||||
import java.util.List;
|
||||
|
||||
public interface EnchereRepository {
|
||||
Enchere findByIdUserAndIdArticle(int idUser, int idArticle);
|
||||
List<Enchere> findByIdUser(int idUser);
|
||||
List<Enchere> findByIdArticle(int idArticle);
|
||||
void save(Enchere enchere);
|
||||
void delete(int id);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class EnchereRepositoryImpl implements EnchereRepository {
|
||||
@Override
|
||||
public Enchere mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Enchere enchere = new Enchere();
|
||||
enchere.setId(rs.getInt("id"));
|
||||
enchere.setNoUtilisateur(rs.getInt("no_utilisateur"));
|
||||
enchere.setNoArticle(rs.getInt("no_article"));
|
||||
enchere.setMontantEnchere(rs.getInt("montant_enchere"));
|
||||
@@ -40,13 +41,16 @@ public class EnchereRepositoryImpl implements EnchereRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enchere findByIdUserAndIdArticle(int idUser, int idArticle) {
|
||||
return null;
|
||||
public List<Enchere> findByIdUser(int idUser) {
|
||||
String sql = "SELECT * FROM ENCHERES WHERE no_utilisateur = ? AND isDelete = 0";
|
||||
List<Enchere> encheres = jdbcTemplate.query(sql, new EnchereRowMapper(), idUser);
|
||||
return encheres;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Enchere> findByIdUser(int idUser) {
|
||||
return List.of();
|
||||
public void delete(int id) {
|
||||
String sql = "UPDATE ENCHERES SET isDelete = 1 WHERE id = ?";
|
||||
jdbcTemplate.update(sql, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package fr.eni.enchere.interceptor;
|
||||
|
||||
import fr.eni.enchere.bll.EnchereService;
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import fr.eni.enchere.bo.Enchere;
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -11,14 +13,18 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class UserInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final UserService userService;
|
||||
private final EnchereService enchereService;
|
||||
|
||||
@Autowired
|
||||
public UserInterceptor(UserService userService) {
|
||||
public UserInterceptor(UserService userService, EnchereService enchereService) {
|
||||
this.userService = userService;
|
||||
this.enchereService = enchereService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -27,8 +33,13 @@ public class UserInterceptor implements HandlerInterceptor {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.isAuthenticated() && !authentication.getName().equals("anonymousUser")) {
|
||||
UserProfil user = this.userService.utilisateurByName(authentication.getName());
|
||||
List<Enchere> enchere = this.enchereService.enchereByUser(user.getId());
|
||||
double encheresSum = enchere.stream()
|
||||
.mapToDouble(ench -> ench.getMontantEnchere()) // Mappez chaque enchère à son montant
|
||||
.sum();
|
||||
if (modelAndView != null && modelAndView.getViewName() != null && !modelAndView.getViewName().startsWith("redirect:")) {
|
||||
modelAndView.addObject("user", user);
|
||||
modelAndView.addObject("enchereUser", encheresSum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ home.button.end = End
|
||||
home.button.next = Next
|
||||
home.button.previous = Previous
|
||||
home.credit = My credits:
|
||||
home.credit.enchere = My auctions :
|
||||
home.nav.enchere = Auctions
|
||||
home.nav.vend = Sell an item
|
||||
home.nav.login = Register / Log in
|
||||
@@ -117,6 +118,7 @@ article.details.label.amount = Amount
|
||||
article.details.button.bid = Bid
|
||||
article.details.address.unknown = Unknown address
|
||||
article.details.validation.amount.required = Bid amount is required.
|
||||
article.details.retrait = Pickup done
|
||||
|
||||
edit.article.title = Edit my article
|
||||
edit.article.update = Edit
|
||||
|
||||
@@ -17,6 +17,7 @@ home.button.end = Fin
|
||||
home.button.next = Suivant
|
||||
home.button.previous = Pr\u00e9cedent
|
||||
home.credit = Mes cr\u00e9dits :
|
||||
home.credit.enchere = Mes ench\u00e9res :
|
||||
home.nav.enchere = Encheres
|
||||
home.nav.vend = Vendre un article
|
||||
home.nav.login = S'inscrire / Se connecter
|
||||
@@ -119,6 +120,7 @@ article.details.label.amount = Montant
|
||||
article.details.button.bid = Ench\u00E9rir
|
||||
article.details.address.unknown = Adresse inconnue
|
||||
article.details.validation.amount.required = Le montant de l'ench\u00E8re est requis.
|
||||
article.details.retrait = Retrait effectu\u00E8
|
||||
|
||||
|
||||
edit.article.title = Modifier mon article
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -12,7 +12,8 @@
|
||||
<h4 th:text="'Article - offre en cours'"></h4>
|
||||
</div>
|
||||
<div class="card-header" th:unless="${#dates.format(article.dateFinEnch, 'yyyy-MM-dd')} > ${#dates.format(#dates.createNow(), 'yyyy-MM-dd')}">
|
||||
<h4 th:text="'Article - offre remporté par ' + (${#authentication.getName() == maxEncherePseudo ? 'vous' : maxEncherePseudo})"></h4>
|
||||
<h4 th:if="${maxEncherePseudo} != null" th:text="'Article - offre remporté par ' + (${#authentication.getName() == maxEncherePseudo ? 'vous' : maxEncherePseudo})"></h4>
|
||||
<h4 th:unless="${maxEncherePseudo} != null" th:text="'Article - Personne n\'a fait d\'offre'"></h4>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
@@ -68,6 +69,10 @@
|
||||
<div class="mt-5 d-flex justify-content-end align-items-center">
|
||||
<a class="btn btn-secondary mr-2" href="/enchere" th:text="#{edit.article.back}"></a>
|
||||
<a th:if="${#strings.equals(user.getPseudo(), article.pseudoUtilisateur)} and ${#dates.format(article.dateDebutEnch, 'yyyy-MM-dd')} > ${#dates.format(#dates.createNow(), 'yyyy-MM-dd')}" class="btn btn-primary" th:href="@{/article/edit(id=${article.id})}" th:text="#{edit.article.update}"></a>
|
||||
<form th:if="${#dates.format(article.dateFinEnch, 'yyyy-MM-dd')} < ${#dates.format(#dates.createNow(), 'yyyy-MM-dd')} and ${#strings.equals(user.getPseudo(), article.pseudoUtilisateur)}" th:action="@{/enchere/enchereDone}" method="post">
|
||||
<input type="hidden" id="id" name="id" th:value="${article.id}">
|
||||
<button type="submit" class="btn btn-primary" th:text="#{article.details.retrait}"></button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -24,11 +24,12 @@
|
||||
<img href="/enchere" src="/img/logo.png" width="70" height="70" alt="Logo">
|
||||
<label href="/enchere" class="text-light"><strong>ENI-Encheres</strong></label>
|
||||
</a>
|
||||
<div th:if="${#authentication.principal != 'anonymousUser'}" class="text-light text-center d-flex justify-content-center align-items-center"><!-- Assurez-vous de remplacer "fa-credit-card" par la classe de votre icône -->
|
||||
<div th:if="${#authentication.principal != 'anonymousUser'}" class="text-light text-center d-flex justify-content-center align-items-center">
|
||||
<span th:text="#{home.credit} + ${user.getCredit()} + ' '"></span>
|
||||
<i class="fas fa-coins"></i>
|
||||
<i class="fas fa-coins ml-1"></i>
|
||||
<span th:text="#{home.credit.enchere} + ${enchereUser}" class="ml-4"></span>
|
||||
<i class="fas fa-coins ml-1"></i>
|
||||
</div>
|
||||
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user