Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e0ae08e

Browse files
committed
Updated Code with Fetch Requests
1 parent d88d9e7 commit e0ae08e

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

‎src/main/java/com/reuben/store/controller/MainController.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
1010
import org.springframework.http.ResponseEntity;
11-
import org.springframework.web.bind.annotation.PathVariable;
12-
import org.springframework.web.bind.annotation.PostMapping;
13-
import org.springframework.web.bind.annotation.RequestBody;
14-
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.bind.annotation.*;
1512

1613
import java.time.LocalDate;
14+
import java.time.Month;
15+
import java.util.List;
1716
import java.util.Optional;
1817

1918

@@ -56,13 +55,15 @@ public Customer addCustomer(
5655
}
5756

5857
@PostMapping("/customer/{customer_id}")
59-
public Profile updateProfile(@RequestBody Profile profile,
58+
public Customer updateProfile(@RequestBody Profile profile,
6059
@PathVariable("customer_id") Long customer_id) {
6160
// Saving the Vendor Details
61+
// Customer c1 = new Customer();
6262
Customer c = customerRepository.getOne(customer_id);
63-
profile.setCustomer(c);
6463
Profile p = profileRepository.save(profile);
65-
return p;
64+
c.setProfile(p);
65+
Customer c2 = customerRepository.save(c);
66+
return c2;
6667
// System.out.println(c.getId());
6768
// return c;
6869

@@ -111,15 +112,51 @@ public ResponseEntity<?> enrollStudentInCourse(
111112
Purchase purchase = new Purchase();
112113
purchase.setCustomer(c);
113114
purchase.setProduct(p);
114-
purchase.setDate(LocalDate.now());
115+
116+
LocalDate date = LocalDate.of(2020, Month.DECEMBER, 23);
117+
118+
purchase.setDate(date);
115119

116120
//save enroll object in DB
117121
Purchase purchase1 = purchaseRepository.save(purchase);
118122
return new ResponseEntity<>(purchase1, HttpStatus.OK);
119123

120124
}
121125

126+
// Fetch API
127+
128+
@GetMapping("/product/purchase/{vendor_id}")
129+
public List<Product> FetchProductsBeforeCertainDate(@PathVariable("vendor_id") Long vendor_id
130+
){
131+
// Vendor v = vendorRepository.getOne(vendor_id); //existing record
132+
133+
List<Product> productList = purchaseRepository.fetchAllProductsByVendorId(vendor_id);
134+
// System.out.println(productList);
135+
return productList;
136+
137+
}
138+
139+
@GetMapping("/customer/purchase/{vendor_id}")
140+
public List<Customer> FetchCustomerFromCertainVendor(@PathVariable("vendor_id") Long vendor_id
141+
){
142+
// Vendor v = vendorRepository.getOne(vendor_id); //existing record
122143

144+
List<Customer> productList = purchaseRepository.fetchAllCustomerByVendorId(vendor_id);
145+
// System.out.println(productList);
146+
return productList;
147+
148+
}
149+
150+
@GetMapping("/product/customer/{city}")
151+
public List<Product> FetchProductsInCity(@PathVariable("city") String city
152+
){
153+
// Vendor v = vendorRepository.getOne(vendor_id); //existing record
154+
155+
List<Product> productList = purchaseRepository.fetchAllProductsByCity(city);
156+
// System.out.println(productList);
157+
return productList;
158+
159+
}
123160

124161
}
125162

‎src/main/java/com/reuben/store/model/Customer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ public class Customer {
1515
@Column(length = 100)
1616
private String password;
1717

18+
@OneToOne
19+
private Profile profile;
20+
21+
public Profile getProfile() {
22+
return profile;
23+
}
24+
25+
public void setProfile(Profile profile) {
26+
this.profile = profile;
27+
}
1828

1929
public Long getId() {
2030
return id;

‎src/main/java/com/reuben/store/model/Profile.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public class Profile {
1818
@Column(length = 100)
1919
private String city;
2020

21-
@OneToOne
22-
private Customer customer;
23-
24-
public Customer getCustomer() {
25-
return customer;
26-
}
27-
28-
public void setCustomer(Customer customer) {
29-
this.customer = customer;
30-
}
21+
// @OneToOne
22+
// private Customer customer;
23+
//
24+
// public Customer getCustomer() {
25+
// return customer;
26+
// }
27+
//
28+
// public void setCustomer(Customer customer) {
29+
// this.customer = customer;
30+
// }
3131

3232
public Long getId() {
3333
return id;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package com.reuben.store.repository;
22

33

4+
import com.reuben.store.model.Customer;
5+
import com.reuben.store.model.Product;
46
import com.reuben.store.model.Purchase;
57
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
10+
import java.util.List;
611

712
public interface PurchaseRepository extends JpaRepository<Purchase, Long> {
813

14+
@Query("select distinct(pd) from Purchase ph JOIN ph.product pd JOIN pd.vendor v where ph.date < '2020年12月25日' and v.id = ?1")
15+
List<Product> fetchAllProductsByVendorId(Long vendor_id);
16+
17+
@Query("select distinct(C) from Purchase ph JOIN ph.product pd JOIN ph.customer C JOIN pd.vendor v where v.id = ?1")
18+
List<Customer> fetchAllCustomerByVendorId(Long vendor_id);
19+
20+
@Query("select distinct(pd) from Purchase ph JOIN ph.product pd JOIN ph.customer C JOIN C.profile pr where ph.date < '2020年12月25日' and pr.city=?1")
21+
List<Product> fetchAllProductsByCity(String city);
922
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /