Commit 38d576ff authored by Khalid Ali's avatar Khalid Ali
Browse files

Big update - add all OneToMany relationships and update unit tests

parent d6f3221f
......@@ -48,7 +48,7 @@ dependencies {
implementation('com.fasterxml.jackson.dataformat:jackson-dataformat-xml')
implementation('com.fasterxml.jackson.datatype:jackson-datatype-joda')
implementation('org.modelmapper:modelmapper:2.3.1')
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
compile('org.postgresql:postgresql:42.2.5')
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.assertj:assertj-core')
......
package com.gmu.bookshare.entity;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.Date;
......@@ -9,10 +11,8 @@ import java.util.Date;
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "bid")
@Getter
@Setter
public class Bid {
@Table(name = "Bid")
public class BidEntity {
@Id
@GeneratedValue
......@@ -30,4 +30,32 @@ public class Bid {
@Column(name = "userId")
private Long userId;
// @ManyToMany(fetch = FetchType.LAZY,
// cascade = {
// CascadeType.PERSIST,
// CascadeType.MERGE
// },
// mappedBy = "posts")
// private Set<ListingEntity> posts = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "listingId")
private ListingEntity listing;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shareUserId")
private ShareUser shareUserOwner;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BidEntity)) return false;
return id != null && id.equals(((BidEntity) o).id);
}
@Override
public int hashCode() {
return 31;
}
}
......@@ -4,16 +4,15 @@ import lombok.*;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
@Entity
@Getter
@Setter
@Table(name = "listing")
@Table(name = "Listing")
public class ListingEntity {
@Id
......@@ -49,18 +48,48 @@ public class ListingEntity {
@Column(name = "createDate")
private Date createDate;
@Column(name = "bidList")
@OneToMany
@JoinTable(name = "Listing2Bids",
joinColumns = {@JoinColumn(name = "Listing_FK")},
inverseJoinColumns = {@JoinColumn(name = "Bids_FK")})
private List<Bid> bidList;
@NonNull
@Column(name = "owner")
private Long owner;
@NonNull
@Column(name = "title")
private String title;
// @ManyToMany(fetch = FetchType.LAZY,
// cascade = {
// CascadeType.PERSIST,
// CascadeType.MERGE
// })
// @JoinTable(name = "listing2bids",
// joinColumns = { @JoinColumn(name = "listingId") },
// inverseJoinColumns = { @JoinColumn(name = "bidId") })
@OneToMany(
mappedBy = "listing",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<BidEntity> bids = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shareUserId")
private ShareUser shareUser;
public void addBid(BidEntity bid) {
bids.add(bid);
bid.setListing(this);
}
public void removeBid(BidEntity bid) {
bids.remove(bid);
bid.setListing(null);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ListingEntity)) return false;
return id != null && id.equals(((ListingEntity) o).id);
}
@Override
public int hashCode() {
return 41;
}
}
package com.gmu.bookshare.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import javax.persistence.*;
import java.util.Set;
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "user")
@Getter
@Table(name = "ShareUser")
public class ShareUser {
@Id
......@@ -20,9 +18,39 @@ public class ShareUser {
@Column(name = "id", updatable = false)
private Long id;
@NonNull
@Column(name = "email")
private String email;
@NonNull
@Column(name = "name")
private String name;
@NonNull
@OneToMany(mappedBy = "shareUser")
private Set<ListingEntity> listingsOwned;
@NonNull
@OneToMany(mappedBy = "shareUserOwner")
private Set<BidEntity> bidsOwned;
public void addBid(BidEntity bid) {
bidsOwned.add(bid);
bid.setShareUserOwner(this);
}
public void removeBid(BidEntity bid) {
bidsOwned.remove(bid);
bid.setShareUserOwner(null);
}
public void addListing(ListingEntity listing) {
listingsOwned.add(listing);
listing.setShareUser(this);
}
public void removeListing(ListingEntity listing) {
listingsOwned.remove(listing);
listing.setShareUser(null);
}
}
package com.gmu.bookshare.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BidDto {
@Id
@GeneratedValue
@JsonProperty("id")
private Long id;
@JsonProperty("createDate")
private Date createDate;
@JsonProperty("price")
private double price;
@JsonProperty("flag")
private int flagged;
@JsonProperty("userId")
private Long userId;
}
package com.gmu.bookshare.persistence;
import com.gmu.bookshare.entity.Bid;
import org.springframework.data.repository.CrudRepository;
import com.gmu.bookshare.entity.BidEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface BidRepository extends CrudRepository<Bid, Long> {
public interface BidRepository extends JpaRepository<BidEntity, Long> {
List<Bid> findByUserId(Long l);
List<BidEntity> findByUserId(Long l);
}
package com.gmu.bookshare.service;
import com.gmu.bookshare.entity.BidEntity;
import com.gmu.bookshare.persistence.BidRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -13,4 +14,8 @@ public class BidService {
public BidService(BidRepository bidRepository) {
this.bidRepository = bidRepository;
}
public BidEntity addListing(BidEntity bidEntity) {
return bidRepository.save(bidEntity);
}
}
package com.gmu.bookshare.service;
import com.gmu.bookshare.entity.BidEntity;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.error.ListingNotFoundException;
import com.gmu.bookshare.persistence.ListingRepository;
......@@ -47,4 +48,8 @@ public class ListingService {
public void deleteListing(Long id) {
listingRepository.deleteById(id);
}
public void addBid(Long id, BidEntity bid) {
getById(id).addBid(bid);
}
}
package com.gmu.bookshare.web;
import com.gmu.bookshare.entity.BidEntity;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.model.BidDto;
import com.gmu.bookshare.model.ListingDto;
import com.gmu.bookshare.service.BidService;
import com.gmu.bookshare.service.ListingService;
......@@ -70,6 +72,23 @@ public class BookshareApiController {
listingService.deleteListing(id);
}
@GetMapping(value = "/listing/{id}/bid/")
List<BidDto> getBidsAssociatedWithListing(@PathVariable Long id) {
ListingEntity listingEntity = listingService.getById(id);
return listingEntity.getBids().stream()
.map(this::convertBidToDto)
.collect(Collectors.toList());
}
@PostMapping(value = "/listing/{id}/bid/")
@ResponseStatus(HttpStatus.CREATED)
public BidDto addBid(@PathVariable Long id, @RequestBody BidDto bidDto) {
BidEntity bid = convertBidToEntity(bidDto);
BidEntity bidCreated = bidService.addListing(bid);
listingService.addBid(id, bidCreated);
return convertBidToDto(bidCreated);
}
private ListingDto convertToDto(ListingEntity listingEntity) {
return modelMapper.map(listingEntity, ListingDto.class);
}
......@@ -77,4 +96,12 @@ public class BookshareApiController {
private ListingEntity convertToEntity(ListingDto listingDto) {
return modelMapper.map(listingDto, ListingEntity.class);
}
private BidDto convertBidToDto(BidEntity bidEntity) {
return modelMapper.map(bidEntity, BidDto.class);
}
private BidEntity convertBidToEntity(BidDto bidDto) {
return modelMapper.map(bidDto, BidEntity.class);
}
}
spring.jpa:
hibernate.ddl-auto: update
properties.hibernate.dialect: org.hibernate.dialect.PostgreSQL94Dialect
hibernate:
ddl-auto: none
jdbc.lob.non_contextual_creation: true
properties.hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
spring.datasource:
......
......@@ -9,15 +9,6 @@ CREATE TABLE ShareUser (
name varchar(255)
);
DROP TABLE IF EXISTS Bid;
CREATE TABLE Bid (
id SERIAL PRIMARY KEY,
createDate DATE,
price DECIMAL(5,2),
flagged INTEGER,
userId bigint REFERENCES ShareUser(id)
);
DROP TABLE IF EXISTS Listing;
CREATE TABLE Listing (
id SERIAL PRIMARY KEY,
......@@ -29,10 +20,28 @@ CREATE TABLE Listing (
image bytea,
description varchar(1000),
createDate DATE,
bid_id bigint REFERENCES Bid(id),
userId bigint REFERENCES ShareUser(id),
shareUserId bigint NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (shareUserId) REFERENCES ShareUser (id),
title varchar(255)
);
CREATE INDEX ON Bid (id);
ALTER TABLE Bid ADD CONSTRAINT listingId FOREIGN KEY (id) REFERENCES Listing
DROP TABLE IF EXISTS Bid;
CREATE TABLE Bid (
id SERIAL PRIMARY KEY,
createDate DATE,
price DECIMAL(5,2),
flagged INTEGER,
listingId bigint NOT NULL,
CONSTRAINT listing_fk FOREIGN KEY (listingId) REFERENCES Listing (id),
shareUserId bigint NOT NULL,
CONSTRAINT shareUser_fk FOREIGN KEY (shareUserId) REFERENCES ShareUser (id)
);
--DROP TABLE IF EXISTS listing2bids;
--CREATE TABLE Listing2Bids (
-- listingId bigint NOT NULL,
-- bidId bigint NOT NULL,
-- CONSTRAINT ListingBid_pk PRIMARY KEY (listingId, bidId),
-- CONSTRAINT a_r_fk FOREIGN KEY (listingId) REFERENCES Listing(id),
-- CONSTRAINT b_r_fk FOREIGN KEY (bidId) REFERENCES Bid(id)
--);
......@@ -53,7 +53,7 @@ public class BookshareRestControllerIntegrationTest {
throws Exception {
ListingEntity alex = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
List<ListingEntity> allListingEntities = Collections.singletonList(alex);
......@@ -71,9 +71,9 @@ public class BookshareRestControllerIntegrationTest {
throws Exception {
ListingEntity listingInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
ListingEntity listingNotInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
listingInDB.setId(123456L);
listingNotInDB.setId(123457L);
......@@ -91,7 +91,7 @@ public class BookshareRestControllerIntegrationTest {
@Test
public void givenListing_whenPutInvalidListing_thenReturnHttpNotFound() throws Exception {
ListingEntity listingNotInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
listingNotInDB.setId(123457L);
ListingDto listingDtoNotInDB = modelMapper.map(listingNotInDB, ListingDto.class);
......@@ -107,7 +107,7 @@ public class BookshareRestControllerIntegrationTest {
@Test
public void givenListing_whenDeleteListing_thenReturnHttpOk() throws Exception {
ListingEntity listingInDB = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
listingInDB.setId(1324L);
ListingDto listingDtoInDB = modelMapper.map(listingInDB, ListingDto.class);
......
package com.gmu.bookshare.repository;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.entity.ShareUser;
import com.gmu.bookshare.persistence.ListingRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -11,6 +12,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -29,8 +31,7 @@ public class ListingEntityRepositoryIntegrationTest {
public void whenFindByIsbn_thenReturnListing() {
// given
ListingEntity alex = new ListingEntity();
testEntityManager.persist(alex);
testEntityManager.flush();
testEntityManager.persistAndFlush(alex);
// when
ArrayList<ListingEntity> found = (ArrayList<ListingEntity>) listingRepository.findByIsbn(alex.getIsbn());
......@@ -43,15 +44,23 @@ public class ListingEntityRepositoryIntegrationTest {
@Test
public void whenFindById_thenReturnListing() {
//given
ShareUser shareUser = new ShareUser("Bobby Jones", "bobbyjones@yahoo.com",
new HashSet<>(), new HashSet<>());
ListingEntity book1 = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
ListingEntity book2 = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
ListingEntity book3 = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
shareUser.addListing(book1);
shareUser.addListing(book2);
shareUser.addListing(book3);
testEntityManager.persist(book1);
testEntityManager.persist(book2);
testEntityManager.persist(book3);
testEntityManager.persistAndFlush(shareUser);
// when
Optional<ListingEntity> found2 = listingRepository.findById(book2.getId());
......@@ -72,9 +81,14 @@ public class ListingEntityRepositoryIntegrationTest {
@Test
public void whenFindByInvalidId_thenReturnEmpty() {
//given
ShareUser shareUser = new ShareUser("John Doe", "jdoe@outlook.com",
new HashSet<>(), new HashSet<>());
ListingEntity book1 = new ListingEntity(123456, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
testEntityManager.persistAndFlush(book1);
new Date(), "Title Calc 3");
shareUser.addListing(book1);
testEntityManager.persistAndFlush(shareUser);
// when
Optional<ListingEntity> error = listingRepository.findById(82341792L);
......
package com.gmu.bookshare.service;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.entity.ShareUser;
import com.gmu.bookshare.persistence.ListingRepository;
import org.junit.Before;
import org.junit.Test;
......@@ -14,6 +15,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -42,12 +44,18 @@ public class ListingEntityServiceIntegrationTest {
@Before
public void setUp() {
ShareUser shareUser = new ShareUser("Bob Jones", "bobjones@gmail.com",
new HashSet<>(), new HashSet<>());
ListingEntity listingEntity1 = new ListingEntity(1234567, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
ListingEntity listingEntity2 = new ListingEntity(1234568, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
ListingEntity listingEntity3 = new ListingEntity(1234569, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
shareUser.addListing(listingEntity1);
shareUser.addListing(listingEntity2);
shareUser.addListing(listingEntity3);
ArrayList<ListingEntity> l = new ArrayList<>();
l.add(listingEntity1);
......@@ -88,7 +96,7 @@ public class ListingEntityServiceIntegrationTest {
@Test
public void whenAddListingByEntity_thenListingShouldBeReturned() {
ListingEntity listingEntity4 = new ListingEntity(1234560, 3, 14.99,
new Date(), 192838079872L, 2879878394L, "Title Calc 3");
new Date(), "Title Calc 3");
Mockito.when(EmployeeServiceImplTestContextConfiguration.listingRepository.save(listingEntity4))
.thenReturn(listingEntity4);
......
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 to comment