Skip to content

Commit

Permalink
Sending user data to pages that need it
Browse files Browse the repository at this point in the history
get-best-bid-user endpoint
  • Loading branch information
xzippyzachx committed Mar 25, 2023
1 parent 6d6ccb1 commit c757a6b
Show file tree
Hide file tree
Showing 23 changed files with 311 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,22 @@ public Bid getAuctionBestBid(@RequestBody GetBestBidRequest request) {
return auctionServiceFactory.getAuctionService().getAuctionBestBid(request.auc_id);
}

@RequestMapping(value="get-best-bid-user", produces=MediaType.APPLICATION_JSON_VALUE, method={RequestMethod.GET, RequestMethod.POST})
public String getAuctionBestBidUser(@RequestBody GetBestBidRequest request) {
return auctionServiceFactory.getAuctionService().getAuctionBestBidUser(request.auc_id);
}

static record NewBidRequest(
Integer auc_id,
Double bid_amount
Double bid_amount,
Integer usr_id
) {}
@PostMapping("new-bid")
public String newBid(@RequestBody NewBidRequest request) {

Auction auction = auctionServiceFactory.getAuctionService().getAuction(request.auc_id);

return auctionServiceFactory.getAuctionService(auction.getAuc_type()).createNewBid(auction, request.bid_amount);
return auctionServiceFactory.getAuctionService(auction.getAuc_type()).createNewBid(auction, request.bid_amount, request.usr_id);
}

static record NewAuctionPaidRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected AbstractService(Environment env, AuctionRepository auctionRepo, BidRep
this.restTemplate = restTemplateBuilder.build();
}

public abstract String createNewBid(Auction auc_id, Double bid_amount);
public abstract String createNewBid(Auction auc_id, Double bid_amount, Integer usr_id);

public abstract void broadcastCurrentAuction(Auction auction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -84,11 +85,13 @@ public JSONObject getAuctionJSON(Integer auc_id) {
Bid bestBid = bidRepo.findBestByAuction(auc_id);

JSONObject auctionJSON = new JSONObject(auction);

ItemBean itemBean = getItem(auction.getAuc_itm_id());
auctionJSON.put("auc_itm_id", new JSONObject(itemBean));
if(bestBid != null) {
auctionJSON.put("highest_bidder_usr_full_name", "Zach Ross"); //ToDo: Pass actual user full name
JSONObject userJSON = getUser(bestBid.getBid_usr_id());
String fullName = userJSON.getString("usr_first_name") + " " + userJSON.getString("usr_last_name");
auctionJSON.put("highest_bidder_usr_full_name", fullName);
auctionJSON.put("highest_bidder_usr_id", bestBid.getBid_usr_id());
}

return auctionJSON;
Expand Down Expand Up @@ -117,6 +120,19 @@ public Bid getAuctionBestBid(Integer auc_id) {
return bidRepo.findBestByAuction(auc_id);
}

public String getAuctionBestBidUser(Integer auc_id) {
Bid bestBid = bidRepo.findBestByAuction(auc_id);

if(bestBid == null) {
return "This auction has no bids";
}

Integer usr_id = bestBid.getBid_usr_id();

JSONObject bestBidUser = getUser(usr_id);
return bestBidUser.toString();
}

public static record PostItem(
Integer itm_id
) {}
Expand Down Expand Up @@ -177,4 +193,20 @@ public void resetAuctionData() {
auctionRepo.resetAuctionData();
}

public static record GetUser(
Integer usr_id
) {}
private JSONObject getUser(Integer usr_id) {
String url = "http://localhost:" + env.getProperty("userServer.port") + "/api/users/get-user";

GetUser userPayload = new GetUser(usr_id);
HttpHeaders headers = new HttpHeaders();
headers.add("x-api-key", env.getProperty("userServer.apiKey"));
HttpEntity<GetUser> request = new HttpEntity<>(userPayload, headers);

ResponseEntity<String> response = this.restTemplate.postForEntity(url, request, String.class); //ToDO: Try catch

return new JSONObject(response.getBody());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;
Expand All @@ -26,7 +27,7 @@ protected DutchService(AuctionRepository auctionRepo, BidRepository bidRepo, Res
}

@Override
public String createNewBid(Auction auction, Double bid_amount) {
public String createNewBid(Auction auction, Double bid_amount, Integer usr_id) {
Date now = new Date();

if(!auction.getAuc_state().equals("running"))
Expand All @@ -39,7 +40,7 @@ public String createNewBid(Auction auction, Double bid_amount) {

newBid.setBid_auc_id(auction);
newBid.setBid_amount(bid_amount);
newBid.setBid_usr_id(1); //TODO: Pass actual usr_id
newBid.setBid_usr_id(usr_id);
newBid.setBid_time(now);

bidRepo.save(newBid);
Expand All @@ -60,7 +61,10 @@ public void broadcastCurrentAuction(Auction auction) {

Bid bestBid = bidRepo.findBestByAuction(auction.getAuc_id());
if(bestBid != null) {
payload.put("highest_bidder_usr_full_name", "Zach Ross"); //ToDo: Pass actual user full name
JSONObject userJSON = getUser(bestBid.getBid_usr_id());
String fullName = userJSON.getString("usr_first_name") + " " + userJSON.getString("usr_last_name");
payload.put("highest_bidder_usr_full_name", fullName);
payload.put("highest_bidder_usr_id", bestBid.getBid_usr_id());
}

HttpHeaders headers = new HttpHeaders();
Expand Down Expand Up @@ -137,7 +141,7 @@ public void run() {
TimerTask tt = new TimerTask() {
@Override
public void run() {
CompleteAuction(auction.getAuc_id());
ExpireAuction(auction.getAuc_id());
};
};
Date nextInterval = new Date(now.getTime() + (1000L * ((DutchAuction) auction).getDch_end_delay()));
Expand All @@ -149,15 +153,31 @@ public void run() {
broadcastCurrentAuction(auction);
}

private void CompleteAuction(Integer auc_id) {
private void ExpireAuction(Integer auc_id) {
System.out.println("Ending dutch auction AuctionId: " + auc_id + " [" + new Date().toInstant().toString() + "]");

Auction auction = auctionRepo.findById(auc_id).get();

auction.setAuc_state("complete");
auction.setAuc_state("expired");

auctionRepo.save(auction);

broadcastCurrentAuction(auction);
}

public static record GetUser(
Integer usr_id
) {}
private JSONObject getUser(Integer usr_id) {
String url = "http://localhost:" + env.getProperty("userServer.port") + "/api/users/get-user";

AuctionService.GetUser userPayload = new AuctionService.GetUser(usr_id);
HttpHeaders headers = new HttpHeaders();
headers.add("x-api-key", env.getProperty("userServer.apiKey"));
HttpEntity<AuctionService.GetUser> request = new HttpEntity<>(userPayload, headers);

ResponseEntity<String> response = this.restTemplate.postForEntity(url, request, String.class); //ToDO: Try catch

return new JSONObject(response.getBody());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;
Expand All @@ -27,7 +28,7 @@ public ForwardService(AuctionRepository auctionRepo, BidRepository bidRepo, Rest
}

@Override
public String createNewBid(Auction auction, Double bid_amount) {
public String createNewBid(Auction auction, Double bid_amount, Integer usr_id) {
Date now = new Date();

if(!auction.getAuc_state().equals("running"))
Expand All @@ -40,7 +41,7 @@ public String createNewBid(Auction auction, Double bid_amount) {

newBid.setBid_auc_id(auction);
newBid.setBid_amount(bid_amount);
newBid.setBid_usr_id(1); //TODO: Pass actual usr_id
newBid.setBid_usr_id(usr_id);
newBid.setBid_time(now);

bidRepo.save(newBid);
Expand All @@ -61,7 +62,10 @@ public void broadcastCurrentAuction(Auction auction) {

Bid bestBid = bidRepo.findBestByAuction(auction.getAuc_id());
if(bestBid != null) {
payload.put("highest_bidder_usr_full_name", "Zach Ross"); //ToDo: Pass actual user full name
JSONObject userJSON = getUser(bestBid.getBid_usr_id());
String fullName = userJSON.getString("usr_first_name") + " " + userJSON.getString("usr_last_name");
payload.put("highest_bidder_usr_full_name", fullName);
payload.put("highest_bidder_usr_id", bestBid.getBid_usr_id());
}

HttpHeaders headers = new HttpHeaders();
Expand Down Expand Up @@ -95,7 +99,7 @@ private void SetupAuctionEndings() {
TimerTask tt = new TimerTask() {
@Override
public void run() {
CompleteAuction(auction.getAuc_id());
ExpireAuction(auction.getAuc_id());
};
};
timer.schedule(tt,((ForwardAuction)auction).getFwd_end_time());
Expand All @@ -104,21 +108,37 @@ public void run() {

} else if (((ForwardAuction) auction).getAuc_state().equals("running")) {
System.out.println("AuctionId: " + auction.getAuc_id() + " End Time: " + ((ForwardAuction) auction).getFwd_end_time().toInstant().toString());
CompleteAuction(auction.getAuc_id());
ExpireAuction(auction.getAuc_id());
}
}
}

private void CompleteAuction(Integer auc_id) {
private void ExpireAuction(Integer auc_id) {
System.out.println("Ending forward auction AuctionId: " + auc_id + " [" + new Date().toInstant().toString() + "]");

Auction auction = auctionRepo.findById(auc_id).get();

auction.setAuc_state("complete");
auction.setAuc_state("expired");

auctionRepo.save(auction);

broadcastCurrentAuction(auction);
}

public static record GetUser(
Integer usr_id
) {}
private JSONObject getUser(Integer usr_id) {
String url = "http://localhost:" + env.getProperty("userServer.port") + "/api/users/get-user";

AuctionService.GetUser userPayload = new AuctionService.GetUser(usr_id);
HttpHeaders headers = new HttpHeaders();
headers.add("x-api-key", env.getProperty("userServer.apiKey"));
HttpEntity<AuctionService.GetUser> request = new HttpEntity<>(userPayload, headers);

ResponseEntity<String> response = this.restTemplate.postForEntity(url, request, String.class); //ToDO: Try catch

return new JSONObject(response.getBody());
}

}
6 changes: 5 additions & 1 deletion auction/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ controllerServer:
port: 8080

itemServer:
port: 8082
port: 8082

userServer:
port: 8081
apiKey: "iwSo3cLAwWlXzfCBsyRs9tQVU9LMPyy1ZNWceHWd1UoaNVqA65nPC0bK6nrQByC7BWDMOQrJb3ShQQxD4rTaypyJlkAJeTFFJwoTlepq4AcJtaZgBabPORrqRAIFdM9y"
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.group15.controller.api;

import com.group15.controller.service.AuthenicationService;
import com.group15.controller.service.ControllerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
Expand All @@ -12,20 +14,23 @@
public class ProxyController {

private final ControllerService controllerService;
private final AuthenicationService authenicationService;

@Autowired
public ProxyController(ControllerService controllerService) {
public ProxyController(ControllerService controllerService, AuthenicationService authenicationService) {
this.controllerService = controllerService;
this.authenicationService = authenicationService;
}

static record NewBidRequest(
Integer auc_id,
Double bid_amount
) {}
@PostMapping("new-bid")
public String newBid(@RequestBody NewBidRequest request) {
public String newBid(Authentication authentication, @RequestBody NewBidRequest request) {
Integer usr_id = authenicationService.getUserId(authentication.getName());

return controllerService.newBid(request.auc_id, request.bid_amount);
return controllerService.newBid(request.auc_id, request.bid_amount, usr_id);
}

static record GetAuctionsByKeyRequest(
Expand Down
Loading

0 comments on commit c757a6b

Please sign in to comment.