Un peu de tout

This commit is contained in:
mepiphana2023
2024-04-30 13:14:37 +02:00
parent 474dec272c
commit 882d9bede0
20 changed files with 187 additions and 80 deletions

View File

@@ -6,14 +6,16 @@ public class Enchere {
private int id;
private int noUtilisateur;
private String pseudoUtilisateur;
private int noArticle;
private Date dateEnchere;
private float montantEnchere;
public Enchere(){}
public Enchere(int noUtilisateur, int noArticle, Date dateEnchere, float montantEnchere) {
public Enchere(int noUtilisateur, int noArticle, String pseudoUtilisateur, Date dateEnchere, float montantEnchere ) {
setNoUtilisateur(noUtilisateur);
setPseudoUtilisateur(pseudoUtilisateur);
setNoArticle(noArticle);
setDateEnchere(dateEnchere);
setMontantEnchere(montantEnchere);
@@ -58,4 +60,12 @@ public class Enchere {
public void setNoUtilisateur(int noUtilisateur) {
this.noUtilisateur = noUtilisateur;
}
public String getPseudoUtilisateur() {
return pseudoUtilisateur;
}
public void setPseudoUtilisateur(String pseudoUtilisateur) {
this.pseudoUtilisateur = pseudoUtilisateur;
}
}

View File

@@ -45,8 +45,12 @@ public class AccueilController {
this.userService = userService;
}
@GetMapping({"/", "/accueil"})
public String viewAccueil(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6") int size, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {
@GetMapping({"/", "/enchere"})
public String viewAccueil(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails,
@RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory,
@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "6") int size, Model model,
@RequestParam(value = "venteOption", required = false) String[] venteOptions,
@RequestParam(value = "achatOption", required = false) String[] achatOptions) {
model.addAttribute("categories", categorieService.findAllCategories());
model.addAttribute("requestURI", request.getRequestURI());
SearchArticleCritere critere = new SearchArticleCritere();
@@ -58,16 +62,25 @@ public class AccueilController {
critere.setVenteOptions(venteOptions);
critere.setAchatOptions(achatOptions);
// Si la page est 1 ou plus, décrémentez la valeur de la page de 1
if (page > 0) {
page -= 1;
}
// Pagination
Page<Article> articlePage = articleService.searchArticlePageable(critere, PageRequest.of(page, size));
model.addAttribute("articles", articlePage.getContent());
model.addAttribute("currentPage", page);
// Ajoutez 1 à la valeur de la page actuelle pour l'affichage dans la vue
int currentPage = page + 1;
model.addAttribute("currentPage", currentPage);
model.addAttribute("totalPages", articlePage.getTotalPages());
return "accueil";
}
@PostMapping("/accueil")
@PostMapping("/enchere")
public String handleSearch(HttpServletRequest request, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "6") int size, Model model, @RequestParam(value = "venteOption", required = false) String[] venteOptions, @RequestParam(value = "achatOption", required = false) String[] achatOptions) {

View File

@@ -21,11 +21,9 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Controller()
@RequestMapping("/article")
@@ -50,7 +48,7 @@ public class ArticleController {
@GetMapping
public String viewArticle(Model model) {
return "redirect:/accueil";
return "redirect:/enchere";
}
//Affichage d'un article
@@ -65,14 +63,20 @@ public class ArticleController {
Retrait retrait = retraitService.retraitByNumarticle(article.getId());
article.setPseudoUtilisateur(user.getPseudo());
List<Enchere> lastEnchere = this.enchereService.enchereByArticle(article.getId());
Optional<Float> maxMontantEnchere = lastEnchere.stream()
.map(Enchere::getMontantEnchere) // Récupère seulement les montants d'enchère
.map(Enchere::getMontantEnchere)
.max(Float::compareTo);
UserProfil currentUser = userService.utilisateurByName(authentication.getName());
boolean isArticleCurrentUser = false;
if (currentUser.getId() == user.getId()) {
isArticleCurrentUser = true;
}
lastEnchere = lastEnchere.stream()
.sorted(Comparator.comparing(Enchere::getMontantEnchere).reversed())
.collect(Collectors.toList());
model.addAttribute("encheres", lastEnchere);
model.addAttribute("isArticleCurrentUser", isArticleCurrentUser);
model.addAttribute("article", article);
model.addAttribute("username", user);
@@ -89,7 +93,7 @@ public class ArticleController {
}
return "article";
} else {
return "redirect:/accueil";
return "redirect:/enchere";
}
}
@@ -229,7 +233,7 @@ public class ArticleController {
//Validation du formulaire
retrait.setNumArticle(articleService.saveArticle(article));
retraitService.setRetrait(retrait);
return "redirect:/accueil";
return "redirect:/enchere";
}
//Update d'un article

View File

@@ -27,7 +27,7 @@ public class BankController {
public String homeCredit(Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
String username = authentication.getName();
UserProfil userProfile = userService.utilisateurByName(username);

View File

@@ -38,7 +38,7 @@ public class ForgotPasswordController {
// Vérifier si l'utilisateur est déjà authentifié
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
if (link != null) {
ForgotPassword forgotPassword = forgotPasswordService.getForgotPassword(link);
@@ -79,7 +79,7 @@ public class ForgotPasswordController {
// Vérifier si l'utilisateur est déjà authentifié
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
ForgotPassword forgotPassword = forgotPasswordService.getForgotPassword(link);
if (forgotPassword != null) {
@@ -135,7 +135,7 @@ public class ForgotPasswordController {
// Vérifier si l'utilisateur est déjà authentifié
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
ForgotPassword forgotPassword = forgotPasswordService.getForgotPassword(link);
if (forgotPassword != null) {

View File

@@ -52,7 +52,7 @@ public class InscriptionController {
// Vérifier si l'utilisateur est déjà authentifié
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
model.addAttribute("userProfile", new UserProfil());
return "inscription";

View File

@@ -24,10 +24,9 @@ public class LoginController {
@GetMapping("/login")
public String login(Model modele) {
// Vérifier si l'utilisateur est déjà authentifié
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")){
return "redirect:/accueil";
return "redirect:/enchere";
}
return "security/login";
}
@@ -36,7 +35,7 @@ public class LoginController {
public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
UserProfil user = userService.utilisateurByName(username);
if (user != null && user.getPassword().equals(password)) {
return "redirect:/accueil";
return "redirect:/enchere";
} else {
return "redirect:/security/login?error";
}

View File

@@ -6,10 +6,7 @@ import org.springframework.ui.Model;
import fr.eni.enchere.bll.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -87,7 +84,7 @@ public class ProfilController {
//Supprimer le compte
userService.deleteUtilisateur(userProfile.getId());
//ATTENTION AJOUTER LA DECONNEXION
return "redirect:/accueil";
return "redirect:/enchere";
}
@PostMapping("/updateUser")
@@ -127,4 +124,15 @@ public class ProfilController {
return "accueil";
}
}
@GetMapping("/showProfil")
public String showOtherProfil(@RequestParam("userPseudo") String userPseudo, Model model) {
model.addAttribute("userProfil", userService.utilisateurByName(userPseudo));
return "showProfil";
}
}

View File

@@ -1,6 +1,10 @@
package fr.eni.enchere.dal;
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 org.apache.catalina.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
@@ -14,9 +18,11 @@ import java.util.List;
public class EnchereRepositoryImpl implements EnchereRepository {
private final JdbcTemplate jdbcTemplate;
private UserService userService;
public EnchereRepositoryImpl(JdbcTemplate jdbcTemplate) {
public EnchereRepositoryImpl(JdbcTemplate jdbcTemplate, UserService userService) {
this.jdbcTemplate = jdbcTemplate;
this.userService = userService;
}
public class EnchereRowMapper implements RowMapper<Enchere> {
@@ -27,6 +33,8 @@ public class EnchereRepositoryImpl implements EnchereRepository {
enchere.setNoArticle(rs.getInt("no_article"));
enchere.setMontantEnchere(rs.getInt("montant_enchere"));
enchere.setDateEnchere(rs.getDate("date_enchere"));
UserProfil user = userService.utilisateurById(rs.getInt("no_utilisateur"));
enchere.setPseudoUtilisateur(user.getPseudo());
return enchere;
}
}

View File

@@ -19,17 +19,17 @@ public class WebSecurityConfig{
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/","/accueil", "/login", "/forgotPassword/**", "/inscription/**", "/searchArticle", "/article/show", "/change-language").permitAll()
.requestMatchers("/","/enchere", "/login", "/forgotPassword/**", "/inscription/**", "/searchArticle", "/article/show", "/change-language").permitAll()
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**", "/assets/**", "/i18n/**").permitAll()
.requestMatchers("/profil/**", "/article/new/**", "/article/update", "/article/delete").authenticated()
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated())
.formLogin((form) -> form
.loginPage("/login")
.defaultSuccessUrl("/", true))
.defaultSuccessUrl("/enchere", true))
.logout((logout) -> logout
.clearAuthentication(true).invalidateHttpSession(true)
.deleteCookies("JSESSIONID").logoutSuccessUrl("/login?logout")
.deleteCookies("JSESSIONID").logoutSuccessUrl("/enchere")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll());
return http.build();