Commit ac38eaea by Bright Sukumpanumet

Add sample data

parent fefd6e45
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
package edu.uchicago.mpcs.topics; package edu.uchicago.mpcs.topics;
import edu.uchicago.mpcs.topics.auction.Item; import edu.uchicago.mpcs.topics.auction.Item;
import edu.uchicago.mpcs.topics.auction.ItemCategory;
import edu.uchicago.mpcs.topics.controller.ItemController; import edu.uchicago.mpcs.topics.controller.ItemController;
import edu.uchicago.mpcs.topics.db.ItemCategoryRepository;
import edu.uchicago.mpcs.topics.db.ItemRepository;
import edu.uchicago.mpcs.topics.db.UserDB; import edu.uchicago.mpcs.topics.db.UserDB;
import edu.uchicago.mpcs.topics.user.User; import edu.uchicago.mpcs.topics.user.User;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
...@@ -35,16 +38,43 @@ public class Application { ...@@ -35,16 +38,43 @@ public class Application {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
// @Bean
// CommandLineRunner initData(UserDB userDB) {
// return args -> {
// userDB.save(new User("wawa", "rex", "wawa", "rex@gmail.com", true));
// userDB.save(new User("wawa", "haoh", "wawa", "hao@gmail.com", true));
// userDB.save(new User("wawa", "nella", "wawa", "nella@gmail.com", true));
// userDB.save(new User("wawa", "bright", "wawa", "bright@gmail.com", true));
// };
// }
@Bean @Bean
CommandLineRunner initData(UserDB userDB) { CommandLineRunner initData(ItemRepository itemRepository,
ItemCategoryRepository itemCategoryRepository)
{
return args -> { return args -> {
userDB.save(new User("wawa", "rex", "wawa", "rex@gmail.com", true));
userDB.save(new User("wawa", "haoh", "wawa", "hao@gmail.com", true)); // item
userDB.save(new User("wawa", "nella", "wawa", "nella@gmail.com", true)); itemRepository.save(new Item("iphone", "it's an iphone", 1L,
userDB.save(new User("wawa", "bright", "wawa", "bright@gmail.com", true)); 333.33, 55555.55, "bit.ly/dd.png", 2L,
true, null, null ));
itemRepository.save(new Item("iphone x", "it's an iphone x", 1L,
333.33, 55555.55, "bit.ly/dd.png", 2L,
true, null, null ));
itemRepository.save(new Item("iphone c", "it's an iphone c", 1L,
333.33, 55555.55, "bit.ly/dd.png", 2L,
true, null, null ));
// category
itemCategoryRepository.save(new ItemCategory("Electronics"));
itemCategoryRepository.save(new ItemCategory("Clothes"));
}; };
} }
// @Bean // @Bean
// public WebMvcConfigurer corsConfigurer() { // public WebMvcConfigurer corsConfigurer() {
// return new WebMvcConfigurer() { // return new WebMvcConfigurer() {
......
...@@ -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.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bidId; private Long bidId;
@NotNull @NotNull
......
...@@ -15,7 +15,7 @@ public class Item ...@@ -15,7 +15,7 @@ public class Item
{ {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId; private Long itemId;
@Column(length = 2000) @Column(length = 2000)
...@@ -64,6 +64,7 @@ public class Item ...@@ -64,6 +64,7 @@ public class Item
this.canBuyNow = canBuyNow; this.canBuyNow = canBuyNow;
this.startTime = startTime; this.startTime = startTime;
this.endTime = endTime; this.endTime = endTime;
this.status = Status.ACTIVE;
} }
public Long getItemId() public Long getItemId()
......
...@@ -11,15 +11,14 @@ import javax.validation.constraints.NotNull; ...@@ -11,15 +11,14 @@ import javax.validation.constraints.NotNull;
public class ItemCategory public class ItemCategory
{ {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemCategoryId; private Long itemCategoryId;
@NotNull
private String itemCategoryName; private String itemCategoryName;
public ItemCategory(String itemCategoryName) public ItemCategory(String itemCategoryName)
{ {
itemCategoryName = itemCategoryName; this.itemCategoryName = itemCategoryName;
} }
public Long getItemCategoryId() public Long getItemCategoryId()
......
...@@ -29,41 +29,56 @@ public class TimeTracker ...@@ -29,41 +29,56 @@ public class TimeTracker
{ {
this.itemId = item.getItemId(); this.itemId = item.getItemId();
LocalDateTime startTime = LocalDateTime.parse(item.getStartTime()); // System.out.println(this.itemId);
LocalDateTime endTime = LocalDateTime.parse(item.getEndTime());
LocalDateTime currentTime = LocalDateTime.now(); LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime startTime = LocalDateTime.now();
if (item.getStartTime() != null)
{
setStatus(Status.INACTIVE);
startTime = LocalDateTime.parse(item.getStartTime());
Duration durationStart = Duration.between(startTime, currentTime); 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);
}
Duration durationEnd = Duration.between(startTime, endTime); if (item.getEndTime() != null)
ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor(); {
endTimer.schedule(this::timeEnd, durationEnd.getSeconds(), TimeUnit.SECONDS); LocalDateTime endTime = LocalDateTime.parse(item.getEndTime());
Duration durationEnd = Duration.between(startTime, endTime);
ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor();
endTimer.schedule(this::timeEnd, durationEnd.getSeconds(), TimeUnit.SECONDS);
}
} }
private void timeStart() private Item setStatus(Status status)
{ {
// Can't just use this.item since it may be edited in the future.
Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null); Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null);
itemToUpdate.setStatus(Status.ACTIVE); itemToUpdate.setStatus(status);
itemRepository.save(itemToUpdate); return itemRepository.save(itemToUpdate);
} }
private void timeStart()
{
this.setStatus(Status.ACTIVE);
}
private void timeEnd() private void timeEnd()
{ {
//set status to ENDED //set status to ENDED
Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null); Item itemToAdd = this.setStatus(Status.ENDED);
itemToUpdate.setStatus(Status.ENDED);
itemRepository.save(itemToUpdate);
//add the item to user shopping cart //add the item to user shopping cart
ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToUpdate.getCurrentBidderID()).orElse(null); ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToAdd.getCurrentBidderID()).orElse(null);
shoppingCartRepository.save(shoppingCart.addItem(itemToUpdate)); shoppingCartRepository.save(shoppingCart.addItem(itemToAdd));
} }
} }
...@@ -3,6 +3,7 @@ package edu.uchicago.mpcs.topics.controller; ...@@ -3,6 +3,7 @@ package edu.uchicago.mpcs.topics.controller;
import edu.uchicago.mpcs.topics.auction.Bid; import edu.uchicago.mpcs.topics.auction.Bid;
import edu.uchicago.mpcs.topics.auction.ItemCategory; import edu.uchicago.mpcs.topics.auction.ItemCategory;
import edu.uchicago.mpcs.topics.db.ItemCategoryRepository; import edu.uchicago.mpcs.topics.db.ItemCategoryRepository;
import edu.uchicago.mpcs.topics.service.ItemCategoryService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -19,10 +20,12 @@ public class itemCategoryController ...@@ -19,10 +20,12 @@ public class itemCategoryController
//repository //repository
private ItemCategoryRepository itemCategoryRepository; private ItemCategoryRepository itemCategoryRepository;
private ItemCategoryService itemCategoryService;
public itemCategoryController(ItemCategoryRepository itemCategoryRepository) public itemCategoryController(ItemCategoryRepository itemCategoryRepository, ItemCategoryService itemCategoryService)
{ {
this.itemCategoryRepository = itemCategoryRepository; this.itemCategoryRepository = itemCategoryRepository;
this.itemCategoryService = itemCategoryService;
} }
...@@ -30,10 +33,8 @@ public class itemCategoryController ...@@ -30,10 +33,8 @@ public class itemCategoryController
@GetMapping @GetMapping
public List<ItemCategory> getAllCategories() public List<ItemCategory> getAllCategories()
{ {
List<ItemCategory> categories = new ArrayList<>();
itemCategoryRepository.findAll().forEach(categories::add);
return categories; return itemCategoryService.getAllCategories();
} }
//add category //add category
......
package edu.uchicago.mpcs.topics.service;
import edu.uchicago.mpcs.topics.auction.ItemCategory;
import edu.uchicago.mpcs.topics.db.ItemCategoryRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bright on 2018-11-17.
*/
@Service
public class ItemCategoryService
{
private final ItemCategoryRepository itemCategoryRepository;
public ItemCategoryService(ItemCategoryRepository itemCategoryRepository)
{
this.itemCategoryRepository = itemCategoryRepository;
}
public List<ItemCategory> getAllCategories()
{
List<ItemCategory> categories = new ArrayList<>();
itemCategoryRepository.findAll().forEach(categories::add);
return categories;
}
}
\ No newline at end of file
...@@ -35,7 +35,7 @@ public class ItemService ...@@ -35,7 +35,7 @@ public class ItemService
//start timer //start timer
TimeTracker timeTracker = new TimeTracker(savedItem); new TimeTracker(savedItem);
return savedItem; return savedItem;
} }
......
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