Commit 1842595e by Bright Sukumpanumet

Fix bugs

parent ac38eaea
...@@ -11,7 +11,7 @@ import java.util.Date; ...@@ -11,7 +11,7 @@ import java.util.Date;
public class Bid public class Bid
{ {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.AUTO)
private Long bidId; private Long bidId;
@NotNull @NotNull
...@@ -32,6 +32,7 @@ public class Bid ...@@ -32,6 +32,7 @@ public class Bid
this.amount = amount; this.amount = amount;
this.itemId = itemId; this.itemId = itemId;
this.time = time; this.time = time;
this.status = Status.ACTIVE;
} }
public Long getBidId() public Long getBidId()
......
...@@ -16,6 +16,8 @@ public class ItemCategory ...@@ -16,6 +16,8 @@ public class ItemCategory
private String itemCategoryName; private String itemCategoryName;
public ItemCategory(){}
public ItemCategory(String itemCategoryName) public ItemCategory(String itemCategoryName)
{ {
this.itemCategoryName = itemCategoryName; this.itemCategoryName = itemCategoryName;
...@@ -33,6 +35,6 @@ public class ItemCategory ...@@ -33,6 +35,6 @@ public class ItemCategory
public void setItemCategoryName(String itemCategoryName) public void setItemCategoryName(String itemCategoryName)
{ {
itemCategoryName = itemCategoryName; this.itemCategoryName = itemCategoryName;
} }
} }
package edu.uchicago.mpcs.topics.auction;
/**
* Created by Bright on 2018-11-30.
*/
public class Watchlist
{
}
...@@ -49,11 +49,10 @@ public class ShoppingCartController ...@@ -49,11 +49,10 @@ public class ShoppingCartController
return shoppingCartService.removeItem(shoppingCartRepository.findById(id).orElse(null), item); return shoppingCartService.removeItem(shoppingCartRepository.findById(id).orElse(null), item);
} }
@PostMapping ("/{id}/checkout") @PutMapping ("/{id}/checkout")
public Checkout checkout(@PathVariable Long id, @RequestBody Checkout checkout) public ShoppingCart checkout(@PathVariable Long id)
{ {
// save checkout (checkout) to db // save checkout (checkout) to db
shoppingCartService.checkout(shoppingCartRepository.findById(id).orElse(null)); return shoppingCartService.checkout(shoppingCartRepository.findById(id).orElse(null));
return checkoutService.checkout(checkout);
} }
} }
...@@ -44,12 +44,4 @@ public class itemCategoryController ...@@ -44,12 +44,4 @@ public class itemCategoryController
return itemCategoryRepository.save(itemCategory); return itemCategoryRepository.save(itemCategory);
} }
//remove category ...disable remove category because of possible consequences
// @DeleteMapping("/{id}")
// public void removeItemCategory(@PathVariable Long id)
// {
// itemCategoryRepository.deleteById(id);
// }
} }
...@@ -17,10 +17,14 @@ import java.util.Optional; ...@@ -17,10 +17,14 @@ import java.util.Optional;
@Service @Service
public class BidService public class BidService
{ {
@Autowired private final BidRepository bidRepository;
private BidRepository bidRepository; private final ItemRepository itemRepository;
@Autowired
private ItemRepository itemRepository; public BidService(BidRepository bidRepository, ItemRepository itemRepository)
{
this.bidRepository = bidRepository;
this.itemRepository = itemRepository;
}
public Bid addBid(Bid bid) public Bid addBid(Bid bid)
{ {
...@@ -35,10 +39,9 @@ public class BidService ...@@ -35,10 +39,9 @@ public class BidService
//Assign highest bidder //Assign highest bidder
itemToUpdate.setCurrentBidderID(bid.getUserId()); itemToUpdate.setCurrentBidderID(bid.getUserId());
itemToUpdate.setStatus(Status.HASBID);
itemRepository.save(itemToUpdate); itemRepository.save(itemToUpdate);
bid.setStatus(Status.ACTIVE);
//set highestBidder //set highestBidder
return bidRepository.save(bid); return bidRepository.save(bid);
......
...@@ -2,7 +2,6 @@ package edu.uchicago.mpcs.topics.service; ...@@ -2,7 +2,6 @@ package edu.uchicago.mpcs.topics.service;
import edu.uchicago.mpcs.topics.auction.Item; import edu.uchicago.mpcs.topics.auction.Item;
import edu.uchicago.mpcs.topics.auction.Status; import edu.uchicago.mpcs.topics.auction.Status;
import edu.uchicago.mpcs.topics.auction.TimeTracker;
import edu.uchicago.mpcs.topics.db.ItemRepository; import edu.uchicago.mpcs.topics.db.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -10,7 +9,6 @@ import org.springframework.stereotype.Service; ...@@ -10,7 +9,6 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
/** /**
* Created by Bright on 2018-11-17. * Created by Bright on 2018-11-17.
...@@ -19,8 +17,15 @@ import java.util.UUID; ...@@ -19,8 +17,15 @@ import java.util.UUID;
@Service @Service
public class ItemService public class ItemService
{ {
private final ItemRepository itemRepository;
private TimerService timerService;
@Autowired @Autowired
private ItemRepository itemRepository; public ItemService(ItemRepository itemRepository, TimerService timerService)
{
this.itemRepository = itemRepository;
this.timerService = timerService;
}
public List<Item> getAllItems() public List<Item> getAllItems()
{ {
...@@ -35,9 +40,7 @@ public class ItemService ...@@ -35,9 +40,7 @@ public class ItemService
//start timer //start timer
new TimeTracker(savedItem); return timerService.trackTime(savedItem);
return savedItem;
} }
public Item deleteItem(Long id) public Item deleteItem(Long id)
......
...@@ -40,7 +40,7 @@ public class ShoppingCartService ...@@ -40,7 +40,7 @@ public class ShoppingCartService
return shoppingCartRepository.save(shoppingCart); return shoppingCartRepository.save(shoppingCart);
} }
public void checkout(ShoppingCart shoppingCart) public ShoppingCart checkout(ShoppingCart shoppingCart)
{ {
//for each item in checkout, delete item from shopping cart //for each item in checkout, delete item from shopping cart
for (Item item : shoppingCart.getItems()) for (Item item : shoppingCart.getItems())
...@@ -51,6 +51,8 @@ public class ShoppingCartService ...@@ -51,6 +51,8 @@ public class ShoppingCartService
//for each item in checkout, change Status to SOLD //for each item in checkout, change Status to SOLD
shoppingCart.removeAllItems(); shoppingCart.removeAllItems();
return shoppingCartRepository.save(shoppingCart);
} }
} }
\ No newline at end of file
package edu.uchicago.mpcs.topics.auction; package edu.uchicago.mpcs.topics.service;
import edu.uchicago.mpcs.topics.auction.Item;
import edu.uchicago.mpcs.topics.auction.ShoppingCart;
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 org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -10,14 +15,13 @@ import java.util.concurrent.Executors; ...@@ -10,14 +15,13 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class TimeTracker @Service
public class TimerService
{ {
private Long itemId; private Long itemId;
@Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
@Autowired
private ShoppingCartRepository shoppingCartRepository; private ShoppingCartRepository shoppingCartRepository;
...@@ -25,35 +29,40 @@ public class TimeTracker ...@@ -25,35 +29,40 @@ public class TimeTracker
//Please use format 2018-11-30T01:37:17 //Please use format 2018-11-30T01:37:17
public TimeTracker(Item item) public TimerService(ItemRepository itemRepository, ShoppingCartRepository shoppingCartRepository)
{
this.itemRepository = itemRepository;
this.shoppingCartRepository = shoppingCartRepository;
}
public Item trackTime(Item item)
{ {
this.itemId = item.getItemId(); this.itemId = item.getItemId();
// System.out.println(this.itemId);
LocalDateTime currentTime = LocalDateTime.now(); LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime startTime = LocalDateTime.now(); LocalDateTime startTime = LocalDateTime.parse(item.getStartTime());
if (item.getStartTime() != null) if (item.getStartTime() != null)
{ {
setStatus(Status.INACTIVE); setStatus(Status.INACTIVE);
startTime = LocalDateTime.parse(item.getStartTime()); Duration durationStart = Duration.between(currentTime, startTime);
Duration durationStart = Duration.between(startTime, currentTime);
ScheduledExecutorService startTimer = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService startTimer = Executors.newSingleThreadScheduledExecutor();
startTimer.schedule(this::timeStart, durationStart.getSeconds(), TimeUnit.SECONDS); startTimer.schedule(this::timeStart, durationStart.getSeconds(), TimeUnit.SECONDS);
// System.out.println(durationStart.getSeconds());
} }
if (item.getEndTime() != null) if (item.getEndTime() != null)
{ {
LocalDateTime endTime = LocalDateTime.parse(item.getEndTime()); LocalDateTime endTime = LocalDateTime.parse(item.getEndTime());
Duration durationEnd = Duration.between(startTime, endTime); Duration durationEnd = Duration.between(endTime, currentTime);
ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor();
endTimer.schedule(this::timeEnd, durationEnd.getSeconds(), TimeUnit.SECONDS); endTimer.schedule(this::timeEnd, durationEnd.getSeconds(), TimeUnit.SECONDS);
} }
return itemRepository.findById(this.itemId).orElse(null);
} }
private Item setStatus(Status status) private Item setStatus(Status status)
...@@ -75,10 +84,17 @@ public class TimeTracker ...@@ -75,10 +84,17 @@ public class TimeTracker
private void timeEnd() private void timeEnd()
{ {
//set status to ENDED //set status to ENDED
Item itemToAdd = this.setStatus(Status.ENDED);
//add the item to user shopping cart Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null);
ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToAdd.getCurrentBidderID()).orElse(null);
shoppingCartRepository.save(shoppingCart.addItem(itemToAdd)); if (itemToUpdate.getStatus() == Status.HASBID)
{
//add the item to user shopping cart
ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToUpdate.getCurrentBidderID()).orElse(null);
shoppingCartRepository.save(shoppingCart.addItem(itemToUpdate));
}
itemToUpdate.setStatus(Status.ENDED);
itemRepository.save(itemToUpdate);
} }
} }
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