Commit 4951df1d by Bright Sukumpanumet

Add Bid ItemCategory and related. Use JPA and Apache Derby for now.

parent 720f5987
......@@ -65,6 +65,15 @@
<artifactId>DynamoDBLocal</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
<repository>
......
package edu.uchicago.mpcs.topics;
/**
* Created by Bright on 2018-11-17.
*/
import java.util.Arrays;
//import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
public class MoviesCreateTable {
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);
String tableName = "Movies";
try {
System.out.println("Attempting to create table; please wait...");
Table table = dynamoDB.createTable(tableName,
Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition
// key
new KeySchemaElement("title", KeyType.RANGE)), // Sort key
Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
new AttributeDefinition("title", ScalarAttributeType.S)),
new ProvisionedThroughput(10L, 10L));
table.waitForActive();
System.out.println("Success. Table status: " + table.getDescription().getTableStatus());
}
catch (Exception e) {
System.err.println("Unable to create table: ");
System.err.println(e.getMessage());
}
}
}
\ No newline at end of file
package edu.uchicago.mpcs.topics;
/**
* Created by Bright on 2018-11-17.
*/
import java.io.File;
import java.util.Iterator;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class MoviesLoadData {
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Movies");
JsonParser parser = new JsonFactory().createParser(new File("moviedata.json"));
JsonNode rootNode = new ObjectMapper().readTree(parser);
Iterator<JsonNode> iter = rootNode.iterator();
ObjectNode currentNode;
while (iter.hasNext()) {
currentNode = (ObjectNode) iter.next();
int year = currentNode.path("year").asInt();
String title = currentNode.path("title").asText();
try {
table.putItem(new Item().withPrimaryKey("year", year, "title", title).withJSON("info",
currentNode.path("info").toString()));
System.out.println("PutItem succeeded: " + year + " " + title);
}
catch (Exception e) {
System.err.println("Unable to add movie: " + year + " " + title);
System.err.println(e.getMessage());
break;
}
}
parser.close();
}
}
\ No newline at end of file
package edu.uchicago.mpcs.topics.auction;
import edu.uchicago.mpcs.topics.user.Buyer;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
public class Bid {
private String id;
private Listing listing;
private double price;
private Buyer buyer;
@Entity
public class Bid
{
@Id
private String userId;
private long amount;
private String itemId;
private Date time;
public String getId() {
return id;
public Bid(String userId, long amount, String itemId, Date time)
{
this.userId = userId;
this.amount = amount;
this.itemId = itemId;
this.time = time;
}
public void setId(String id) {
this.id = id;
public String getUserId()
{
return userId;
}
public Listing getListing() {
return listing;
public void setUserId(String userId)
{
this.userId = userId;
}
public void setListing(Listing listing) {
this.listing = listing;
public long getAmount()
{
return amount;
}
public double getPrice() {
return price;
public void setAmount(long amount)
{
this.amount = amount;
}
public void setPrice(double price) {
this.price = price;
public String getItemId()
{
return itemId;
}
public Buyer getBuyer() {
return buyer;
public void setItemId(String itemId)
{
this.itemId = itemId;
}
public void setBuyer(Buyer buyer) {
this.buyer = buyer;
public Date getTime()
{
return time;
}
public void setTime(Date time)
{
this.time = time;
}
}
package edu.uchicago.mpcs.topics.auction;
import javax.persistence.Entity;
import javax.persistence.Id;
public class Item {
@Entity
public class Item
{
@Id
private String itemId;
private String title;
private String description;
......
package edu.uchicago.mpcs.topics.auction;
public class ItemCategory {
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class ItemCategory
{
@Id
private String ItemCategoryId;
public ItemCategory(String itemCategoryId)
{
ItemCategoryId = itemCategoryId;
}
public String getItemCategoryId()
{
return ItemCategoryId;
}
public void setItemCategoryId(String itemCategoryId)
{
ItemCategoryId = itemCategoryId;
}
}
package edu.uchicago.mpcs.topics.controller;
import edu.uchicago.mpcs.topics.auction.Bid;
import edu.uchicago.mpcs.topics.service.BidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Created by Bright on 2018-11-18.
*/
@RestController
@RequestMapping("/bid")
public class BidController
{
@Autowired
private BidService bidService;
@PostMapping
public void addItem(@RequestBody Bid bid)
{
bidService.addBid(bid);
}
}
\ No newline at end of file
......@@ -30,9 +30,15 @@ public class ItemController
itemService.addItem(item);
}
// @DeleteMapping("/remove")
// public void removeItem(@RequestBody )
//
// @RequestMapping("/update")
@DeleteMapping("/remove")
public void removeItem(@RequestBody Item item) //or change to item ID
{
itemService.deleteItem(item.getItemId());
}
}
@PutMapping("/update")
public void updateItem(@RequestBody Item item)
{
itemService.updateItem(item);
}
}
\ No newline at end of file
package edu.uchicago.mpcs.topics.db;
import edu.uchicago.mpcs.topics.auction.Bid;
import org.springframework.data.repository.CrudRepository;
/**
* Created by Bright on 2018-11-18.
*/
public interface BidRepository extends CrudRepository<Bid, String>
{
}
package edu.uchicago.mpcs.topics.db;
import edu.uchicago.mpcs.topics.auction.Item;
import org.springframework.data.repository.CrudRepository;
/**
* Created by Bright on 2018-11-17.
*/
public interface ItemRepository extends CrudRepository<Item, String>
{
}
package edu.uchicago.mpcs.topics.service;
import edu.uchicago.mpcs.topics.auction.Bid;
import edu.uchicago.mpcs.topics.db.BidRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Bright on 2018-11-18.
*/
@Service
public class BidService
{
@Autowired
private BidRepository bidRepository;
public void addBid(Bid bid)
{
bidRepository.save(bid);
}
}
package edu.uchicago.mpcs.topics.service;
import edu.uchicago.mpcs.topics.auction.Item;
import edu.uchicago.mpcs.topics.db.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
......@@ -14,29 +16,34 @@ import java.util.List;
@Service
public class ItemService
{
private List<Item> items = new ArrayList<>(Arrays.asList(
new Item("1", "Sun glasses", "Red sun glasses", "1", 500, 1000, "",""),
new Item("2", "Mouse", "clicky", "1", 100, 1300, "","")
));
@Autowired
private ItemRepository itemRepository;
//adding example items
// private List<Item> items = new ArrayList<>(Arrays.asList(
// new Item("1", "Sun glasses", "Red sun glasses", "1", 500, 1000, "",""),
// new Item("2", "Mouse", "clickkkkz", "1", 100, 1300, "","")
// ));
public List<Item> getAllItems()
{
List<Item> items = new ArrayList<>();
itemRepository.findAll().forEach(items::add);
return items;
}
public void addItem(Item item)
{
items.add(item);
itemRepository.save(item);
}
public void deleteItem(String id)
{
itemRepository.deleteById(id);
}
public void updateItem(String id)
public void updateItem(Item item)
{
itemRepository.save(item);
}
}
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