package com.pmumali.plr.controllers; import java.net.URI; import java.util.List; import org.springframework.http.ResponseEntity; 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 com.pmumali.plr.dtos.PariDto; import com.pmumali.plr.enums.PariType; import com.pmumali.plr.models.Pari; import com.pmumali.plr.services.PariService; import io.swagger.v3.oas.annotations.Operation; import lombok.AllArgsConstructor; import lombok.Data; @RestController @RequestMapping("/api/paris") @Data @AllArgsConstructor public class PariController { private final PariService pariService; @Operation(summary = "Placer un pari") @PostMapping public ResponseEntity create(@RequestBody PariDto dto) { Pari p = pariService.create(dto.courseId(), dto.chevalId(), dto.pariType(), dto.mise(), dto.bettorRef()); PariDto response = new PariDto(p.getId(), p.getCourse().getId(), p.getCheval().getId(), p.getPariType(), p.getMise(), p.getBettorRef()); return ResponseEntity.created(URI.create("/api/paris/" + p.getId())).body(response); } @Operation(summary = "Lister tous les paris") @GetMapping public ResponseEntity> all() { List list = pariService.all().stream() .map(h -> new PariDto(h.getId(), h.getCourse().getId(), h.getCheval().getId(), h.getPariType(), h.getMise(), h.getBettorRef())) .toList(); return ResponseEntity.ok(list); } @Operation(summary = "Récupérer un pari par id") @GetMapping("/{id}") public ResponseEntity one(@PathVariable Long id) { Pari p = pariService.get(id); PariDto dto = new PariDto(p.getId(), p.getCourse().getId(), p.getCheval().getId(), p.getPariType(), p.getMise(), p.getBettorRef()); return ResponseEntity.ok(dto); } // Recherche par type (ex: SIMPLE_GAGNANT) @Operation(summary = "Lister les paris par type") @GetMapping("/type/{pariType}") public ResponseEntity> getAllByPariType(@PathVariable PariType pariType) { List list = pariService.getParisByPariType(pariType).stream() .map(h -> new PariDto(h.getId(), h.getCourse().getId(), h.getCheval().getId(), h.getPariType(), h.getMise(), h.getBettorRef())) .toList(); return ResponseEntity.ok(list); } // Recherche par course et type @Operation(summary = "Lister les paris par course + type") @GetMapping("/course/{courseId}/type/{pariType}") public ResponseEntity> getByCourseAndType(@PathVariable Long courseId, @PathVariable PariType pariType) { List list = pariService.getParisByCourseAndType(courseId, pariType).stream() .map(h -> new PariDto(h.getId(), h.getCourse().getId(), h.getCheval().getId(), h.getPariType(), h.getMise(), h.getBettorRef())) .toList(); return ResponseEntity.ok(list); } // Somme des mises d'une course / type (utile pour calculs de pools) @Operation(summary = "Total des mises pour une course et un type de pari") @GetMapping("/course/{courseId}/type/{pariType}/sum") public ResponseEntity sumMises(@PathVariable Long courseId, @PathVariable PariType pariType) { java.math.BigDecimal total = pariService.sumMiseByCourseIdAndPariType(courseId, pariType); return ResponseEntity.ok(total); } }