|
|
@@ -0,0 +1,111 @@
|
|
|
+package bidflow.bid.client.controller;
|
|
|
+
|
|
|
+import bidflow.bid.client.client.AuctionClient;
|
|
|
+import bidflow.bid.client.client.CatalogClient;
|
|
|
+import bidflow.bid.client.client.PersistenceClient;
|
|
|
+import bidflow.bid.client.client.UsersClient;
|
|
|
+import bidflow.bid.client.dto.CreateAuctionRequest;
|
|
|
+import bidflow.bid.client.dto.ItemDTO;
|
|
|
+import bidflow.bid.client.dto.User;
|
|
|
+import bidflow.bid.client.dto.UserResponse;
|
|
|
+import bidflow.bid.client.dto.Auction;
|
|
|
+import bidflow.bid.client.dto.AuctionView;
|
|
|
+import bidflow.bid.client.dto.BidRequest;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.ui.Model;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class AuctionController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuctionClient auctionClient;
|
|
|
+ @Autowired
|
|
|
+ private CatalogClient catalogClient;
|
|
|
+ @Autowired
|
|
|
+ private PersistenceClient persistenceClient;
|
|
|
+ @Autowired
|
|
|
+ private UsersClient usersClient;
|
|
|
+
|
|
|
+ @GetMapping("/")
|
|
|
+ public String index(Model model) {
|
|
|
+
|
|
|
+ List<ItemDTO> items = auctionClient.getItems();
|
|
|
+ UserResponse usersResponse = usersClient.getUsers();
|
|
|
+ List<Auction> historyRaw = persistenceClient.getHistory();
|
|
|
+
|
|
|
+ List<User> users = new ArrayList<>();
|
|
|
+ if (usersResponse != null && usersResponse.getEmbedded() != null) {
|
|
|
+ users = usersResponse.getEmbedded().getUsers();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Enriquecer histórico
|
|
|
+ List<AuctionView> history = historyRaw.stream()
|
|
|
+ .map(auction -> {
|
|
|
+
|
|
|
+ ItemDTO item = catalogClient.getItemById(auction.getItemId());
|
|
|
+
|
|
|
+ User winner = null;
|
|
|
+ if (auction.getWinnerUserId() != null && !auction.getWinnerUserId().isEmpty()) {
|
|
|
+ winner = usersClient.getUserById(auction.getWinnerUserId());
|
|
|
+ }
|
|
|
+
|
|
|
+ User seller = null;
|
|
|
+ if (auction.getSellerId() != null && !auction.getSellerId().isEmpty()) {
|
|
|
+ seller = usersClient.getUserById(auction.getSellerId());
|
|
|
+ }
|
|
|
+
|
|
|
+ String itemName = item != null ? item.getName() : "";
|
|
|
+ String sellerUsername = seller != null ? seller.getUsername() : "";
|
|
|
+ String winnerUsername = winner != null ? winner.getUsername() : "";
|
|
|
+
|
|
|
+ return new AuctionView(
|
|
|
+ itemName,
|
|
|
+ sellerUsername,
|
|
|
+ winnerUsername,
|
|
|
+ auction.getFinalPrice()
|
|
|
+ );
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ model.addAttribute("items", items);
|
|
|
+ model.addAttribute("users", users);
|
|
|
+ model.addAttribute("history", history);
|
|
|
+
|
|
|
+ return "index";
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/create")
|
|
|
+ public String createAuction(@RequestParam String itemId,
|
|
|
+ @RequestParam Double startingPrice) {
|
|
|
+
|
|
|
+ CreateAuctionRequest request = new CreateAuctionRequest();
|
|
|
+ request.setItemId(itemId);
|
|
|
+ request.setStartingPrice(startingPrice);
|
|
|
+
|
|
|
+ auctionClient.createAuction(request);
|
|
|
+
|
|
|
+ return "redirect:/";
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/bid")
|
|
|
+ public String placeBid(@RequestParam String itemId,
|
|
|
+ @RequestParam String userId,
|
|
|
+ @RequestParam Double amount) {
|
|
|
+
|
|
|
+ BidRequest request = new BidRequest();
|
|
|
+ request.setItemId(itemId);
|
|
|
+ request.setUserId(userId);
|
|
|
+ request.setAmount(amount);
|
|
|
+
|
|
|
+ auctionClient.placeBid(request);
|
|
|
+
|
|
|
+ return "redirect:/";
|
|
|
+ }
|
|
|
+}
|