Commit 31dd5bcd by Bright Sukumpanumet

Add timer functions

parent daa0dcbb
...@@ -2,6 +2,7 @@ package edu.uchicago.mpcs.topics.auction; ...@@ -2,6 +2,7 @@ package edu.uchicago.mpcs.topics.auction;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.uchicago.mpcs.topics.user.User;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date; import java.util.Date;
...@@ -33,11 +34,13 @@ public class Item ...@@ -33,11 +34,13 @@ public class Item
@Column(length = 2000) @Column(length = 2000)
private String photoUrl; private String photoUrl;
private Long userId; //seller private Long sellerId; //seller
private Status status; private Status status;
private Long currentBidderID;
@ManyToMany(mappedBy = "items") @ManyToMany(mappedBy = "items")
@JsonIgnore @JsonIgnore
private Set<ShoppingCart> shopping_carts = new HashSet<>(); //must have the same name as the table private Set<ShoppingCart> shopping_carts = new HashSet<>(); //must have the same name as the table
...@@ -47,7 +50,7 @@ public class Item ...@@ -47,7 +50,7 @@ public class Item
} }
public Item(String title, String description, Long categoryId, double currentPrice, public Item(String title, String description, Long categoryId, double currentPrice,
double buyNowPrice, String photoUrl, Long userId, Boolean canBuyNow) double buyNowPrice, String photoUrl, Long sellerId, Boolean canBuyNow)
{ {
this.title = title; this.title = title;
this.description = description; this.description = description;
...@@ -55,7 +58,7 @@ public class Item ...@@ -55,7 +58,7 @@ public class Item
this.currentPrice = currentPrice; this.currentPrice = currentPrice;
this.buyNowPrice = buyNowPrice; this.buyNowPrice = buyNowPrice;
this.photoUrl = photoUrl; this.photoUrl = photoUrl;
this.userId = userId; this.sellerId = sellerId;
this.canBuyNow = canBuyNow; this.canBuyNow = canBuyNow;
} }
...@@ -130,14 +133,14 @@ public class Item ...@@ -130,14 +133,14 @@ public class Item
this.photoUrl = photoUrl; this.photoUrl = photoUrl;
} }
public Long getUserId() public Long getSellerId()
{ {
return userId; return sellerId;
} }
public void setUserId(Long userId) public void setSellerId(Long sellerId)
{ {
this.userId = userId; this.sellerId = sellerId;
} }
@JsonIgnore @JsonIgnore
...@@ -212,4 +215,14 @@ public class Item ...@@ -212,4 +215,14 @@ public class Item
{ {
this.endTime = endTime; this.endTime = endTime;
} }
public Long getCurrentBidderID()
{
return currentBidderID;
}
public void setCurrentBidderID(Long currentBidderID)
{
this.currentBidderID = currentBidderID;
}
} }
...@@ -5,5 +5,5 @@ package edu.uchicago.mpcs.topics.auction; ...@@ -5,5 +5,5 @@ package edu.uchicago.mpcs.topics.auction;
*/ */
public enum Status public enum Status
{ {
ACTIVE, INACTIVE, FAILED, INAPPROPRIATE, COUNTERFEIT, SUSPENDED, DELETED, HASBID, SOLD ACTIVE, INACTIVE, FAILED, INAPPROPRIATE, COUNTERFEIT, SUSPENDED, DELETED, HASBID, SOLD, ENDED
} }
package edu.uchicago.mpcs.topics.auction; package edu.uchicago.mpcs.topics.auction;
import java.util.Date; import edu.uchicago.mpcs.topics.db.ItemRepository;
import java.util.Timer; import edu.uchicago.mpcs.topics.db.ShoppingCartRepository;
public class TimeTracker { import java.time.Duration;
private Timer timer; import java.time.LocalDateTime;
private long timeWindow; import java.util.concurrent.Executors;
private Date start; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public Timer getTimer() { public class TimeTracker
return timer; {
} private Long itemId;
public void setTimer(Timer timer) { private ItemRepository itemRepository;
this.timer = timer; private ShoppingCartRepository shoppingCartRepository;
}
public long getTimeWindow() {
return timeWindow;
}
public void setTimeWindow(long timeWindow) {
this.timeWindow = timeWindow; //Please use format 2018-11-30T01:37:17
public TimeTracker(Long itemId, LocalDateTime startTime, LocalDateTime endTime, ItemRepository itemRepository, ShoppingCartRepository shoppingCartRepository)
{
this.itemId = itemId;
this.itemRepository = itemRepository;
this.shoppingCartRepository = shoppingCartRepository;
LocalDateTime currentTime = LocalDateTime.now();
Duration durationStart = Duration.between(startTime, currentTime);
ScheduledExecutorService startTimer = Executors.newSingleThreadScheduledExecutor();
startTimer.schedule(() -> timeStart(), durationStart.getSeconds(), TimeUnit.SECONDS);
Duration durationEnd = Duration.between(startTime, endTime);
ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor();
endTimer.schedule(() -> timeEnd(), durationEnd.getSeconds(), TimeUnit.SECONDS);
} }
public Date getStart() { private void timeStart()
return start; {
Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null);
itemToUpdate.setStatus(Status.ACTIVE);
itemRepository.save(itemToUpdate);
} }
public void setStart(Date start) { private void timeEnd()
this.start = start; {
//set status to ENDED
Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null);
itemToUpdate.setStatus(Status.ENDED);
itemRepository.save(itemToUpdate);
//TODO add the item to user shopping cart
// ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToUpdate.getHighestBidder().getUserId()).orElse(null);
// shoppingCartRepository.save(shoppingCart.addItem(itemToUpdate));
} }
} }
...@@ -7,6 +7,7 @@ import edu.uchicago.mpcs.topics.db.ShoppingCartRepository; ...@@ -7,6 +7,7 @@ import edu.uchicago.mpcs.topics.db.ShoppingCartRepository;
import edu.uchicago.mpcs.topics.service.CheckoutService; import edu.uchicago.mpcs.topics.service.CheckoutService;
import edu.uchicago.mpcs.topics.service.ShoppingCartService; import edu.uchicago.mpcs.topics.service.ShoppingCartService;
import edu.uchicago.mpcs.topics.auction.ShoppingCart; import edu.uchicago.mpcs.topics.auction.ShoppingCart;
import edu.uchicago.mpcs.topics.user.User;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
/** /**
......
...@@ -32,10 +32,13 @@ public class BidService ...@@ -32,10 +32,13 @@ public class BidService
{ {
itemToUpdate.setItemId(bid.getItemId()); itemToUpdate.setItemId(bid.getItemId());
itemToUpdate.setCurrentPrice(bid.getAmount()); itemToUpdate.setCurrentPrice(bid.getAmount());
itemToUpdate.setCurrentBidderID(bid.getUserId());
itemRepository.save(itemToUpdate); itemRepository.save(itemToUpdate);
bid.setStatus(Status.ACTIVE); bid.setStatus(Status.ACTIVE);
//set highestBidder
return bidRepository.save(bid); return bidRepository.save(bid);
} }
else else
......
...@@ -5,6 +5,7 @@ import edu.uchicago.mpcs.topics.auction.Status; ...@@ -5,6 +5,7 @@ import edu.uchicago.mpcs.topics.auction.Status;
import edu.uchicago.mpcs.topics.db.ItemRepository; import edu.uchicago.mpcs.topics.db.ItemRepository;
import edu.uchicago.mpcs.topics.db.ShoppingCartRepository; import edu.uchicago.mpcs.topics.db.ShoppingCartRepository;
import edu.uchicago.mpcs.topics.auction.ShoppingCart; import edu.uchicago.mpcs.topics.auction.ShoppingCart;
import edu.uchicago.mpcs.topics.user.User;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment