From 041be7e1492ca881125f222d36ae50f4a35a8c6c Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Mon, 6 Nov 2023 07:26:24 -0500 Subject: [PATCH 1/6] Refactored Book Details --- .../Controller/BooksController.java | 61 ++++----- .../getdata/restcall_test1/Entity/Author.java | 2 +- .../getdata/restcall_test1/Entity/Books.java | 117 ++++++------------ .../restcall_test1/Repository/BookRepo.java | 8 ++ .../restcall_test1/Service/BooksService.java | 41 +++--- 5 files changed, 93 insertions(+), 136 deletions(-) diff --git a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java index dc5812c..d2a0abb 100644 --- a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java +++ b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java @@ -15,55 +15,42 @@ public class BooksController { @Autowired private BooksService booksService; - @GetMapping("/{genre}") + @GetMapping("/genre/{genre}") public ResponseEntity> getBooksByGenre(@PathVariable String genre) { List books = booksService.getBooksByGenre(genre); return ResponseEntity.ok(books); } - @GetMapping("/Best Sellers") + @GetMapping("/best-sellers") public ResponseEntity> getBestSellingBooks() { List books = booksService.getBestSellingBooks(); return ResponseEntity.ok(books); } - //http://localhost:8080/books/Fantasy for genre - //http://localhost:8080/books/Best Seller for best-seller + @PutMapping("/update-prices/{publisher}/{discountPercent}") + public ResponseEntity updateBookPrices( + @PathVariable String publisher, + @PathVariable double discountPercent + ) { + booksService.updateBookPrices(publisher, discountPercent); + return ResponseEntity.ok().build(); + } + @GetMapping("/isbn/{isbn}") + public ResponseEntity getBookByISBN(@PathVariable String isbn) { + Books book = booksService.getBookByISBN(isbn); + if (book != null) { + return ResponseEntity.ok(book); + } else { + return ResponseEntity.notFound().build(); + } + } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +//http://localhost:8080/books/Fantasy for genre +//http://localhost:8080/books/Best Sellers for best-seller +//http://localhost:8080/books/publisher/discount?publisher=Bantam&discountPercent=5 discount for publisher +//http://localhost:8080/books/Vintage/5 a more simple way to update price +//GET http://localhost:8080/books/isbn/9780743210898 for isbn diff --git a/src/main/java/com/getdata/restcall_test1/Entity/Author.java b/src/main/java/com/getdata/restcall_test1/Entity/Author.java index 84c22d3..3ff6481 100644 --- a/src/main/java/com/getdata/restcall_test1/Entity/Author.java +++ b/src/main/java/com/getdata/restcall_test1/Entity/Author.java @@ -36,4 +36,4 @@ public String getFirstName(){ public String getLastName(){ return this.LastName; } -} \ No newline at end of file +} diff --git a/src/main/java/com/getdata/restcall_test1/Entity/Books.java b/src/main/java/com/getdata/restcall_test1/Entity/Books.java index 062c1a4..5930e38 100644 --- a/src/main/java/com/getdata/restcall_test1/Entity/Books.java +++ b/src/main/java/com/getdata/restcall_test1/Entity/Books.java @@ -1,5 +1,8 @@ package com.getdata.restcall_test1.Entity; +import com.fasterxml.jackson.annotation.*; import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; @Entity @Table(name = "books") @@ -12,96 +15,50 @@ public Books(String bookName, String genre) { public Books() {} - @Id + @Getter + @Setter @Column(name = "BookName") private String bookName; + @Getter + @Setter + @Column(name = "BookDesc") + private String bookDesc; + + @Getter + @Setter + @Column(name = "Price") + private double price; + + @Getter + @Setter @Column(name = "Genre") private String genre; + @Getter + @Setter + @Column(name = "Publisher") + private String publisher; + + @Getter + @Setter @Column(name = "CopiesSold") private int copiesSold; + @Getter + @Setter + @Column(name = "YearPublished") + private int yearPublished; + + @Getter + @Setter + @Id @Column(name = "ISBN") private String ISBN; - public String getBookName() { - return this.bookName; - } - - public void setBookName(String bookName) { - this.bookName = bookName; - } - - public String getGenre() { - return this.genre; - } - - public void setGenre(String genre) { - this.genre = genre; - } - - public int getCopiesSold() { - return copiesSold; - } - - public void setCopiesSold(int copiesSold) { - this.copiesSold = copiesSold; - } - - public String getISBN() { - return ISBN; - } - - public void setISBN(String ISBN) { - this.ISBN = ISBN; - } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @Setter + @ManyToOne + @JoinColumn(name = "AuthorID") + private Author author; +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java index 3ece6f7..fdf35d0 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java @@ -4,8 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; public interface BookRepo extends JpaRepository{ List findByGenre(String genre); @@ -15,5 +17,11 @@ public interface BookRepo extends JpaRepository{ @Query("SELECT b FROM Books b JOIN Rating r ON b.ISBN = r.ISBN WHERE r.rating = :rating") List findBooksByRating(@Param("rating") int rating); + // Optional findById(Long id); + + List findByPublisher(String publisher); + + // Find a book by ISBN + Optional findById(String isbn); } diff --git a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java index 24c19a9..590d4ed 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java @@ -2,10 +2,12 @@ import com.getdata.restcall_test1.Repository.BookRepo; import com.getdata.restcall_test1.Entity.Books; +import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; @Service public class BooksService { @@ -13,7 +15,7 @@ public class BooksService { @Autowired private BookRepo bookRepo; - public List getBooksByGenre(String genre) { // camelCase parameter + public List getBooksByGenre(String genre) { return bookRepo.findByGenre(genre); } @@ -26,25 +28,28 @@ public List getBooksByRating(int rating) { } -} - - - - - - - - - - - - - - - - + public void createBook(Books book) { + bookRepo.save(book); + } + public void updateBook(Books book) { + bookRepo.save(book); + } + public Books getBookByISBN(String isbn) { + Optional bookOptional = bookRepo.findById(isbn); + return bookOptional.orElse(null); + } + @Transactional + public void updateBookPrices(String publisher, double discountPercent) { + List books = bookRepo.findByPublisher(publisher); + for (Books book : books) { + double newPrice = book.getPrice() * (1 - discountPercent / 100); + book.setPrice(newPrice); + bookRepo.save(book); + } + } +} \ No newline at end of file From 5462166f6fdb73eafe1b5a6abacd732ed1bdcba5 Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Mon, 6 Nov 2023 07:39:26 -0500 Subject: [PATCH 2/6] Refactored Book Details --- .../getdata/restcall_test1/Controller/BooksController.java | 2 +- src/main/java/com/getdata/restcall_test1/Entity/Author.java | 2 +- .../java/com/getdata/restcall_test1/Repository/BookRepo.java | 2 +- src/main/resources/application.properties | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java index d2a0abb..2c3da0e 100644 --- a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java +++ b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java @@ -53,4 +53,4 @@ public ResponseEntity getBookByISBN(@PathVariable String isbn) { //http://localhost:8080/books/Best Sellers for best-seller //http://localhost:8080/books/publisher/discount?publisher=Bantam&discountPercent=5 discount for publisher //http://localhost:8080/books/Vintage/5 a more simple way to update price -//GET http://localhost:8080/books/isbn/9780743210898 for isbn +//GET http://localhost:8080/books/isbn/9780743210898 for isbn \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Entity/Author.java b/src/main/java/com/getdata/restcall_test1/Entity/Author.java index 3ff6481..84c22d3 100644 --- a/src/main/java/com/getdata/restcall_test1/Entity/Author.java +++ b/src/main/java/com/getdata/restcall_test1/Entity/Author.java @@ -36,4 +36,4 @@ public String getFirstName(){ public String getLastName(){ return this.LastName; } -} +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java index fdf35d0..5beee4d 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java @@ -24,4 +24,4 @@ public interface BookRepo extends JpaRepository{ // Find a book by ISBN Optional findById(String isbn); -} +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6503ed3..c338bbd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/test_data1 +spring.datasource.url=jdbc:mysql://localhost:8080/geektext spring.datasource.username=root -spring.datasource.password=112101Ma@28 +spring.datasource.password=ealexan2 spring.jpa.show-sql=true From 3bd242abe8984bfa128ca7a4f7456d74e2a11699 Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Mon, 6 Nov 2023 07:51:46 -0500 Subject: [PATCH 3/6] Refactored Book Details --- .../java/com/getdata/restcall_test1/Repository/BookRepo.java | 2 +- .../java/com/getdata/restcall_test1/Service/BooksService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java index 5beee4d..fdf35d0 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java @@ -24,4 +24,4 @@ public interface BookRepo extends JpaRepository{ // Find a book by ISBN Optional findById(String isbn); -} \ No newline at end of file +} diff --git a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java index 590d4ed..6dd4c0b 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java @@ -52,4 +52,4 @@ public void updateBookPrices(String publisher, double discountPercent) { } -} \ No newline at end of file +} From 9cc17158d21ad9b56e947123353a8d487f55d850 Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Mon, 6 Nov 2023 07:54:28 -0500 Subject: [PATCH 4/6] Refactored Book Details --- .../java/com/getdata/restcall_test1/Service/BooksService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java index 6dd4c0b..224d5b0 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java @@ -51,5 +51,4 @@ public void updateBookPrices(String publisher, double discountPercent) { } } - } From c51e3bf994700fcd972719dc30145e407b6369f6 Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Sun, 26 Nov 2023 09:02:03 -0500 Subject: [PATCH 5/6] Rebased branch --- build.gradle | 2 +- .../Controller/BooksController.java | 12 +++- .../Controller/CreditCardController.java | 25 --------- .../Controller/UserController.java | 34 ------------ .../getdata/restcall_test1/Entity/Books.java | 55 ++----------------- .../restcall_test1/Entity/CreditCard.java | 48 ---------------- .../getdata/restcall_test1/Entity/User.java | 49 ----------------- .../restcall_test1/Repository/BookRepo.java | 4 +- .../Repository/CreditCardRepo.java | 7 --- .../restcall_test1/Repository/RatingRepo.java | 4 +- .../restcall_test1/Repository/UserRepo.java | 11 ---- .../restcall_test1/Service/BooksService.java | 3 +- .../Service/CreditCardService.java | 21 ------- .../restcall_test1/Service/RatingService.java | 4 +- .../restcall_test1/Service/UserService.java | 26 --------- 15 files changed, 23 insertions(+), 282 deletions(-) delete mode 100644 src/main/java/com/getdata/restcall_test1/Controller/CreditCardController.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Controller/UserController.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Entity/CreditCard.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Entity/User.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Repository/CreditCardRepo.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Repository/UserRepo.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Service/CreditCardService.java delete mode 100644 src/main/java/com/getdata/restcall_test1/Service/UserService.java diff --git a/build.gradle b/build.gradle index d6cf871..7fc7020 100644 --- a/build.gradle +++ b/build.gradle @@ -32,4 +32,4 @@ dependencies { tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java index 14d6f3c..88023f1 100644 --- a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java +++ b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java @@ -49,8 +49,16 @@ public ResponseEntity getBookByISBN(@PathVariable String isbn) { - //http://localhost:8080/books/Fantasy for genre - //http://localhost:8080/books/Best Sellers for best-seller +//http://localhost:8080/books/Fantasy for genre +//http://localhost:8080/books/Best Sellers for best-seller +//http://localhost:8080/books/publisher/discount?publisher=Bantam&discountPercent=5 discount for publisher +//http://localhost:8080/books/Vintage/5 a more simple way to update price +//GET http://localhost:8080/books/isbn/9780743210898 for isbn + + + + //http://localhost:8080/books/genre/Fantasy for genre + //http://localhost:8080/books/best-sellers for best-seller //http://localhost:8080/books/publisher/discount?publisher=Bantam&discountPercent=5 discount for publisher //http://localhost:8080/books/Vintage/5 a more simple way to update price //GET http://localhost:8080/books/isbn/9780743210898 for isbn diff --git a/src/main/java/com/getdata/restcall_test1/Controller/CreditCardController.java b/src/main/java/com/getdata/restcall_test1/Controller/CreditCardController.java deleted file mode 100644 index e9d735c..0000000 --- a/src/main/java/com/getdata/restcall_test1/Controller/CreditCardController.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.getdata.restcall_test1.Controller; - -import com.getdata.restcall_test1.Entity.CreditCard; -import com.getdata.restcall_test1.Service.CreditCardService; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/cards") -public class CreditCardController { - - private CreditCardService cardService; - - public CreditCardController(CreditCardService cardService){ - this.cardService = cardService; - } - - @PostMapping("/postCard") - public CreditCard postCard(CreditCard card){ - cardService.postCard(card); - return card; - } -} diff --git a/src/main/java/com/getdata/restcall_test1/Controller/UserController.java b/src/main/java/com/getdata/restcall_test1/Controller/UserController.java deleted file mode 100644 index d7fa730..0000000 --- a/src/main/java/com/getdata/restcall_test1/Controller/UserController.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.getdata.restcall_test1.Controller; - -import com.getdata.restcall_test1.Entity.Books; -import com.getdata.restcall_test1.Entity.User; -import com.getdata.restcall_test1.Service.UserService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping("/users") -public class UserController { - - private UserService userService; - - public UserController(UserService userService){ - this.userService = userService; - } - - @GetMapping("/getUser/{Username}") - public ResponseEntity> getUsersByUsername(@PathVariable String Username) { - List users = userService.getUsersByUsername(Username); - return ResponseEntity.ok(users); - } - - @PostMapping("/postUser") - public User postUser(User user){ - userService.postUser(user); - return user; - } - -} diff --git a/src/main/java/com/getdata/restcall_test1/Entity/Books.java b/src/main/java/com/getdata/restcall_test1/Entity/Books.java index 4f3d711..fd79a75 100644 --- a/src/main/java/com/getdata/restcall_test1/Entity/Books.java +++ b/src/main/java/com/getdata/restcall_test1/Entity/Books.java @@ -1,5 +1,6 @@ package com.getdata.restcall_test1.Entity; -import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -56,57 +57,11 @@ public Books() {} @Column(name = "ISBN") private String ISBN; + @Getter @Setter + @JsonBackReference @ManyToOne @JoinColumn(name = "AuthorID") private Author author; -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Entity/CreditCard.java b/src/main/java/com/getdata/restcall_test1/Entity/CreditCard.java deleted file mode 100644 index c1f3717..0000000 --- a/src/main/java/com/getdata/restcall_test1/Entity/CreditCard.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.getdata.restcall_test1.Entity; - -import com.fasterxml.jackson.annotation.JsonBackReference; -import com.fasterxml.jackson.annotation.JsonIgnore; -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import java.sql.Date; - -@Entity -@Table(name = "CreditCards") -@NoArgsConstructor -@AllArgsConstructor -public class CreditCard { - - @Getter - @Setter - @Id - @Column(name = "CardNum") - private String CardNum; - - @Getter - @Setter - @Column(name = "CVV") - private int CVV; - - @Getter - @Setter - @Column(name = "ExpDate") - private Date ExpDate; - - @Getter - @Setter - @Column - private String UserID; - - @Getter - @Setter - @JsonIgnore - @ManyToOne - @JoinColumn(name = "UserID", insertable = false, updatable = false) - private User user; - - -} diff --git a/src/main/java/com/getdata/restcall_test1/Entity/User.java b/src/main/java/com/getdata/restcall_test1/Entity/User.java deleted file mode 100644 index 31eb804..0000000 --- a/src/main/java/com/getdata/restcall_test1/Entity/User.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.getdata.restcall_test1.Entity; - -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import java.util.ArrayList; -import java.util.List; - -@Entity -@Table(name = "Users") -@NoArgsConstructor -@AllArgsConstructor -public class User { - - - @Getter - @Setter - @Id - @Column(name = "UserID") - private String UserID; - - @Getter - @Setter - @Column(name = "Username", unique = true) - private String username; - - @Getter - @Setter - @Column(name = "PW") - private String PW; - - @Getter - @Setter - @Column(name = "FullName") - private String FullName; - - @Getter - @Setter - @Column(name = "EmailAddress") - private String EmailAddress; - - @Getter - @Setter - @Column(name = "HomeAddress") - private String HomeAddress; -} diff --git a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java index 19c1477..5beee4d 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/BookRepo.java @@ -17,11 +17,11 @@ public interface BookRepo extends JpaRepository{ @Query("SELECT b FROM Books b JOIN Rating r ON b.ISBN = r.ISBN WHERE r.rating = :rating") List findBooksByRating(@Param("rating") int rating); - // Optional findById(Long id); + // Optional findById(Long id); List findByPublisher(String publisher); // Find a book by ISBN Optional findById(String isbn); -} +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Repository/CreditCardRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/CreditCardRepo.java deleted file mode 100644 index 786fb48..0000000 --- a/src/main/java/com/getdata/restcall_test1/Repository/CreditCardRepo.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.getdata.restcall_test1.Repository; - -import com.getdata.restcall_test1.Entity.*; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface CreditCardRepo extends JpaRepository{ -} diff --git a/src/main/java/com/getdata/restcall_test1/Repository/RatingRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/RatingRepo.java index 9520e64..08ba6af 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/RatingRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/RatingRepo.java @@ -12,9 +12,9 @@ public interface RatingRepo extends JpaRepository { List findByRating(Integer ratingValue); - // List findByBookISBN(String ISBN); + // List findByBookISBN(String ISBN); @Query("SELECT b from Books b JOIN Rating r ON b.ISBN = r.ISBN WHERE r.rating >= :ratingValue") List findBooksByRatingOrHigher(@Param("ratingValue") Integer ratingValue); -} +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Repository/UserRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/UserRepo.java deleted file mode 100644 index 03bb856..0000000 --- a/src/main/java/com/getdata/restcall_test1/Repository/UserRepo.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.getdata.restcall_test1.Repository; - -import com.getdata.restcall_test1.Entity.*; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.List; - -public interface UserRepo extends JpaRepository{ - - List findByUsername(String Username); -} diff --git a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java index 224d5b0..590d4ed 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java @@ -51,4 +51,5 @@ public void updateBookPrices(String publisher, double discountPercent) { } } -} + +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Service/CreditCardService.java b/src/main/java/com/getdata/restcall_test1/Service/CreditCardService.java deleted file mode 100644 index fe393ac..0000000 --- a/src/main/java/com/getdata/restcall_test1/Service/CreditCardService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.getdata.restcall_test1.Service; - -import com.getdata.restcall_test1.Entity.CreditCard; -import com.getdata.restcall_test1.Entity.User; -import com.getdata.restcall_test1.Repository.CreditCardRepo; -import com.getdata.restcall_test1.Repository.UserRepo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class CreditCardService { - - @Autowired - private CreditCardRepo cardRepo; - - - public CreditCard postCard(CreditCard card){ - cardRepo.save(card); - return card; - } -} diff --git a/src/main/java/com/getdata/restcall_test1/Service/RatingService.java b/src/main/java/com/getdata/restcall_test1/Service/RatingService.java index 814620b..bbc0bf8 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/RatingService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/RatingService.java @@ -24,6 +24,4 @@ public List getBooksByRatingOrHigher(Integer ratingValue) { } //test -} - - +} \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Service/UserService.java b/src/main/java/com/getdata/restcall_test1/Service/UserService.java deleted file mode 100644 index c5aa76f..0000000 --- a/src/main/java/com/getdata/restcall_test1/Service/UserService.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.getdata.restcall_test1.Service; - -import com.getdata.restcall_test1.Entity.*; -import com.getdata.restcall_test1.Repository.UserRepo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -public class UserService { - - @Autowired - private UserRepo userRepo; - - public List getUsersByUsername(String Username){ - return userRepo.findByUsername(Username); - } - - public User postUser(User user){ - userRepo.save(user); - return user; - } - - -} From c0f4800c0a7bc0b0607f914ad82fcdc7112f7a4e Mon Sep 17 00:00:00 2001 From: Evon Troy Alexander Date: Mon, 27 Nov 2023 20:42:45 -0500 Subject: [PATCH 6/6] Finished Feature --- .../Controller/AuthorController.java | 29 +++++++++++++++---- .../Controller/BooksController.java | 7 ++++- .../getdata/restcall_test1/Entity/Author.java | 19 +++++------- .../restcall_test1/Repository/AuthorRepo.java | 8 ++++- .../restcall_test1/Service/AuthorService.java | 10 +++++++ .../restcall_test1/Service/BooksService.java | 1 + 6 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/getdata/restcall_test1/Controller/AuthorController.java b/src/main/java/com/getdata/restcall_test1/Controller/AuthorController.java index 5b3725c..8595cc5 100644 --- a/src/main/java/com/getdata/restcall_test1/Controller/AuthorController.java +++ b/src/main/java/com/getdata/restcall_test1/Controller/AuthorController.java @@ -1,12 +1,14 @@ package com.getdata.restcall_test1.Controller; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; import com.getdata.restcall_test1.Service.AuthorService; import com.getdata.restcall_test1.Entity.Author; +import com.getdata.restcall_test1.Entity.Books; +import java.util.List; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; @RestController public class AuthorController { @@ -21,4 +23,21 @@ public AuthorController(AuthorService authorService) { public Author getDetails(@PathVariable String AuthorID){ return authorService.getAuthorsByID(AuthorID); } -} \ No newline at end of file + @GetMapping("/getAuthorBooks/{AuthorID}") + public ResponseEntity> getBooksByAuthor(@PathVariable String AuthorID){ + List books = authorService.getBooksByAuthorID(AuthorID); + if (books != null && !books.isEmpty()) { + return ResponseEntity.ok(books); + } else { + return ResponseEntity.notFound().build(); + } + } + @PostMapping("/createAuthor") + public ResponseEntity createAuthor(@RequestBody Author author) { + authorService.createAuthor(author); + return new ResponseEntity<>(HttpStatus.CREATED); + } +} + +//http://localhost:8080/getAuthor/B0000000 +//http://localhost:8080/books/isbn/9780743210898 \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java index 88023f1..6d30754 100644 --- a/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java +++ b/src/main/java/com/getdata/restcall_test1/Controller/BooksController.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - +import org.springframework.http.HttpStatus; import java.util.List; @RestController @@ -45,6 +45,11 @@ public ResponseEntity getBookByISBN(@PathVariable String isbn) { return ResponseEntity.notFound().build(); } } + @PostMapping("/createBook") + public ResponseEntity createBook(@RequestBody Books book) { + booksService.createBook(book); + return new ResponseEntity<>(HttpStatus.CREATED); + } } diff --git a/src/main/java/com/getdata/restcall_test1/Entity/Author.java b/src/main/java/com/getdata/restcall_test1/Entity/Author.java index 84c22d3..fcc4073 100644 --- a/src/main/java/com/getdata/restcall_test1/Entity/Author.java +++ b/src/main/java/com/getdata/restcall_test1/Entity/Author.java @@ -1,5 +1,7 @@ package com.getdata.restcall_test1.Entity; import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; @Entity @@ -16,24 +18,19 @@ public Author(){ } @Id + @Getter + @Setter @Column(name = "AuthorID") private String AuthorID; + @Getter + @Setter @Column(name = "FirstName") private String FirstName; + @Getter + @Setter @Column(name = "LastName") private String LastName; - public String getAuthorID(){ - return this.AuthorID; - } - - public String getFirstName(){ - return this.FirstName; - } - - public String getLastName(){ - return this.LastName; - } } \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Repository/AuthorRepo.java b/src/main/java/com/getdata/restcall_test1/Repository/AuthorRepo.java index abf24dd..b87d95d 100644 --- a/src/main/java/com/getdata/restcall_test1/Repository/AuthorRepo.java +++ b/src/main/java/com/getdata/restcall_test1/Repository/AuthorRepo.java @@ -1,8 +1,14 @@ package com.getdata.restcall_test1.Repository; - +import com.getdata.restcall_test1.Entity.Books; +import java.util.List; import com.getdata.restcall_test1.Entity.Author; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface AuthorRepo extends JpaRepository { + + @Query("SELECT b FROM Books b WHERE b.author.AuthorID = :authorId") + List findBooksByAuthorID(@Param("authorId") String authorId); } \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Service/AuthorService.java b/src/main/java/com/getdata/restcall_test1/Service/AuthorService.java index f7b5672..c7d466c 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/AuthorService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/AuthorService.java @@ -4,6 +4,8 @@ import com.getdata.restcall_test1.Entity.Author; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; +import com.getdata.restcall_test1.Entity.Books; +import java.util.List; @Service public class AuthorService { @@ -12,6 +14,14 @@ public class AuthorService { private AuthorRepo authorRepo; public Author getAuthorsByID(String AuthorID){ + return authorRepo.findById(AuthorID).orElse(null); } + public List getBooksByAuthorID(String AuthorID){ + return authorRepo.findBooksByAuthorID(AuthorID); + } + + public void createAuthor(Author author) { + authorRepo.save(author); + } } \ No newline at end of file diff --git a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java index 590d4ed..a57c268 100644 --- a/src/main/java/com/getdata/restcall_test1/Service/BooksService.java +++ b/src/main/java/com/getdata/restcall_test1/Service/BooksService.java @@ -29,6 +29,7 @@ public List getBooksByRating(int rating) { public void createBook(Books book) { + bookRepo.save(book); }