Commit ac38eaea by Bright Sukumpanumet

Add sample data

parent fefd6e45
......@@ -16,7 +16,10 @@
package edu.uchicago.mpcs.topics;
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.db.ItemCategoryRepository;
import edu.uchicago.mpcs.topics.db.ItemRepository;
import edu.uchicago.mpcs.topics.db.UserDB;
import edu.uchicago.mpcs.topics.user.User;
import org.springframework.boot.CommandLineRunner;
......@@ -35,16 +38,43 @@ public class Application {
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
CommandLineRunner initData(UserDB userDB) {
CommandLineRunner initData(ItemRepository itemRepository,
ItemCategoryRepository itemCategoryRepository)
{
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));
// item
itemRepository.save(new Item("iphone", "it's an iphone", 1L,
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
// public WebMvcConfigurer corsConfigurer() {
// return new WebMvcConfigurer() {
......
......@@ -11,7 +11,7 @@ import java.util.Date;
public class Bid
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bidId;
@NotNull
......
......@@ -15,7 +15,7 @@ public class Item
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId;
@Column(length = 2000)
......@@ -64,6 +64,7 @@ public class Item
this.canBuyNow = canBuyNow;
this.startTime = startTime;
this.endTime = endTime;
this.status = Status.ACTIVE;
}
public Long getItemId()
......
......@@ -11,15 +11,14 @@ import javax.validation.constraints.NotNull;
public class ItemCategory
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemCategoryId;
@NotNull
private String itemCategoryName;
public ItemCategory(String itemCategoryName)
{
itemCategoryName = itemCategoryName;
this.itemCategoryName = itemCategoryName;
}
public Long getItemCategoryId()
......
......@@ -29,41 +29,56 @@ public class TimeTracker
{
this.itemId = item.getItemId();
LocalDateTime startTime = LocalDateTime.parse(item.getStartTime());
LocalDateTime endTime = LocalDateTime.parse(item.getEndTime());
// System.out.println(this.itemId);
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);
ScheduledExecutorService startTimer = Executors.newSingleThreadScheduledExecutor();
startTimer.schedule(this::timeStart, durationStart.getSeconds(), TimeUnit.SECONDS);
Duration durationStart = Duration.between(startTime, currentTime);
ScheduledExecutorService startTimer = Executors.newSingleThreadScheduledExecutor();
startTimer.schedule(this::timeStart, durationStart.getSeconds(), TimeUnit.SECONDS);
}
Duration durationEnd = Duration.between(startTime, endTime);
ScheduledExecutorService endTimer = Executors.newSingleThreadScheduledExecutor();
endTimer.schedule(this::timeEnd, durationEnd.getSeconds(), TimeUnit.SECONDS);
if (item.getEndTime() != null)
{
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);
itemToUpdate.setStatus(Status.ACTIVE);
itemToUpdate.setStatus(status);
itemRepository.save(itemToUpdate);
return itemRepository.save(itemToUpdate);
}
private void timeStart()
{
this.setStatus(Status.ACTIVE);
}
private void timeEnd()
{
//set status to ENDED
Item itemToUpdate = itemRepository.findById(this.itemId).orElse(null);
itemToUpdate.setStatus(Status.ENDED);
itemRepository.save(itemToUpdate);
Item itemToAdd = this.setStatus(Status.ENDED);
//add the item to user shopping cart
ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToUpdate.getCurrentBidderID()).orElse(null);
shoppingCartRepository.save(shoppingCart.addItem(itemToUpdate));
ShoppingCart shoppingCart = shoppingCartRepository.findById(itemToAdd.getCurrentBidderID()).orElse(null);
shoppingCartRepository.save(shoppingCart.addItem(itemToAdd));
}
}
......@@ -3,6 +3,7 @@ package edu.uchicago.mpcs.topics.controller;
import edu.uchicago.mpcs.topics.auction.Bid;
import edu.uchicago.mpcs.topics.auction.ItemCategory;
import edu.uchicago.mpcs.topics.db.ItemCategoryRepository;
import edu.uchicago.mpcs.topics.service.ItemCategoryService;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
......@@ -19,10 +20,12 @@ public class itemCategoryController
//repository
private ItemCategoryRepository itemCategoryRepository;
private ItemCategoryService itemCategoryService;
public itemCategoryController(ItemCategoryRepository itemCategoryRepository)
public itemCategoryController(ItemCategoryRepository itemCategoryRepository, ItemCategoryService itemCategoryService)
{
this.itemCategoryRepository = itemCategoryRepository;
this.itemCategoryService = itemCategoryService;
}
......@@ -30,10 +33,8 @@ public class itemCategoryController
@GetMapping
public List<ItemCategory> getAllCategories()
{
List<ItemCategory> categories = new ArrayList<>();
itemCategoryRepository.findAll().forEach(categories::add);
return categories;
return itemCategoryService.getAllCategories();
}
//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
//start timer
TimeTracker timeTracker = new TimeTracker(savedItem);
new TimeTracker(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