Skip to content

Commit

Permalink
Reformated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaynegi45 committed Nov 13, 2024
1 parent 6880ceb commit eaf4315
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/libraryman_api/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.libraryman_api.exception.ResourceNotFoundException;
import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -102,21 +101,22 @@ public BookDto updateBook(@PathVariable int id, @Valid @RequestBody BookDto book
public void deleteBook(@PathVariable int id) {
bookService.deleteBook(id);
}

/**
* Searches book based on title, author, genre, etc.
* It uses a keyword parameter to filter the books, and pagination is applied to the search results.
* If no book is found it will return 204(No content found) http response.
* If keyword is null then it will return all books.
* @param keyword the Keyword to search Book
*
* @param keyword the Keyword to search Book
* @param pageable
* @return
*/
@GetMapping("/search")
public ResponseEntity<Page<Book>> searchBook(@RequestParam String keyword, @PageableDefault(page = 0, size = 5, sort = "title") Pageable pageable){
Page<Book> books=bookService.searchBook(keyword,pageable);
if(!books.isEmpty())
return ResponseEntity.ok(books);
return ResponseEntity.noContent().build();
public ResponseEntity<Page<Book>> searchBook(@RequestParam String keyword, @PageableDefault(page = 0, size = 5, sort = "title") Pageable pageable) {
Page<Book> books = bookService.searchBook(keyword, pageable);
if (!books.isEmpty())
return ResponseEntity.ok(books);
return ResponseEntity.noContent().build();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/libraryman_api/book/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class BookDto {

private int bookId;
@NotBlank(message = "Title is required ")
@Size(min = 1,max = 255,message = "Title must be between 1 and 255 characters")
@Size(min = 1, max = 255, message = "Title must be between 1 and 255 characters")
private String title;

@NotBlank(message = "Author is required")
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/libraryman_api/book/BookRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@

@Repository
public interface BookRepository extends JpaRepository<Book, Integer> {
/**
* This method use SQL Query for finding book based on
* title, author, genre, publishedYear, etc. By using LIKE operator
* it search from database based on keyword entered.
*
* @param keyword
* @param pageable
* @return
*/
@Query("SELECT b FROM Book b WHERE "
+ "(LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.publisher) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.genre) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "CAST(b.publishedYear AS string) LIKE %:keyword% OR "
+ "CAST(b.copiesAvailable AS string) LIKE %:keyword%)")
Page<Book> searchBook(@Param("keyword") String keyword,Pageable pageable);

/**
* This method use SQL Query for finding book based on
* title, author, genre, publishedYear, etc. By using LIKE operator
* it search from database based on keyword entered.
*
* @param keyword
* @param pageable
* @return
*/

@Query("SELECT b FROM Book b WHERE "
+ "(LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.publisher) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "LOWER(b.genre) LIKE LOWER(CONCAT('%', :keyword, '%')) OR "
+ "CAST(b.publishedYear AS string) LIKE %:keyword% OR "
+ "CAST(b.copiesAvailable AS string) LIKE %:keyword%)")
Page<Book> searchBook(@Param("keyword") String keyword, Pageable pageable);
}


Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,16 @@ public Book DtoToEntity(BookDto bookDto) {
book.setIsbn(bookDto.getIsbn());
return book;
}

/**
* <p>This method takes String keyword and search book based on
* <p>This method takes String keyword and search book based on
* title, author, genre, publishedYear, etc. from Book Entity. </p>
*
*
* @param keyword
* @param pageable
* @return
*/
public Page<Book> searchBook(String keyword,Pageable pageable ){
return bookRepository.searchBook(keyword,pageable);
public Page<Book> searchBook(String keyword, Pageable pageable) {
return bookRepository.searchBook(keyword, pageable);
}
}
28 changes: 15 additions & 13 deletions src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
@Repository
public interface BorrowingRepository extends JpaRepository<Borrowings, Integer> {

Page<Borrowings> findByMember_memberId(int memberId, Pageable pageable); {
try {
Page<Borrowings> borrowings = borrowingRepository.findByMember_memberId(memberId, pageable);

if (borrowings.isEmpty()) {
throw new ResourceNotFoundException("Member didn't borrow any book");
}
return borrowings.map(this::EntityToDto);
} catch (PropertyReferenceException ex) {
throw new InvalidSortFieldException("The specified 'sortBy' value is invalid.");
}
}

{
try {
Page<Borrowings> borrowings = borrowingRepository.findByMember_memberId(memberId, pageable);

if (borrowings.isEmpty()) {
throw new ResourceNotFoundException("Member didn't borrow any book");
}
return borrowings.map(this::EntityToDto);
} catch (PropertyReferenceException ex) {
throw new InvalidSortFieldException("The specified 'sortBy' value is invalid.");
}
}

Page<Borrowings> findByMember_memberId(int memberId, Pageable pageable);

@Query(value = "SELECT b.title AS bookTitle, COUNT(*) AS borrowCount " +
"FROM borrowings br JOIN books b ON br.book_id = b.book_id " +
"GROUP BY b.book_id ORDER BY borrowCount DESC LIMIT :limit", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class BorrowingsDto {
private Date returnDate;



public BorrowingsDto(int borrowingId, BookDto book, Fines fine, MembersDto member, Date borrowDate, Date dueDate, Date returnDate) {
this.borrowingId = borrowingId;
this.book = book;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/libraryman_api/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.libraryman_api.notification.NotificationRepository;
import com.libraryman_api.notification.NotificationStatus;
import com.libraryman_api.notification.Notifications;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.springframework.web.context.request.WebRequest;

import java.util.Date;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Global exception handler for the LibraryMan API. This class provides
Expand Down Expand Up @@ -75,16 +77,16 @@ public ResponseEntity<?> invalidPasswordException(InvalidPasswordException ex, W
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String,Object>> MethodArgumentNotValidException(MethodArgumentNotValidException ex){
public ResponseEntity<Map<String, Object>> MethodArgumentNotValidException(MethodArgumentNotValidException ex) {
List<ObjectError> allErrors = ex.getBindingResult().getAllErrors();
HashMap<String,Object> map = new HashMap<>();
HashMap<String, Object> map = new HashMap<>();
allErrors.forEach(objectError -> {
String message=objectError.getDefaultMessage();
String field=((FieldError) objectError).getField();
map.put(field,message);
String message = objectError.getDefaultMessage();
String field = ((FieldError) objectError).getField();
map.put(field, message);
});

return new ResponseEntity<>(map,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/libraryman_api/member/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ResponseEntity<MembersDto> getMemberById(@PathVariable int id) {
*/
@PutMapping("/{id}")
@PreAuthorize("hasRole('LIBRARIAN') or hasRole('ADMIN') or (hasRole('USER') and #id == authentication.principal.memberId)")
public MembersDto updateMember(@PathVariable int id,@Valid @RequestBody UpdateMembersDto membersDtoDetails) {
public MembersDto updateMember(@PathVariable int id, @Valid @RequestBody UpdateMembersDto membersDtoDetails) {
return memberService.updateMember(id, membersDtoDetails);
}

Expand All @@ -114,7 +114,7 @@ public void deleteMember(@PathVariable int id) {
@PutMapping("/{id}/password")
@PreAuthorize("#id == authentication.principal.memberId")
public ResponseEntity<?> updatePassword(@PathVariable int id,
@Valid @RequestBody UpdatePasswordDto updatePasswordDto) {
@Valid @RequestBody UpdatePasswordDto updatePasswordDto) {
memberService.updatePassword(id, updatePasswordDto);
return ResponseEntity.ok("Password updated successfully.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.libraryman_api.email.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.regex.Pattern;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.libraryman_api.newsletter;

import jakarta.persistence.*;

import java.util.UUID;

@Entity
Expand Down Expand Up @@ -32,11 +33,31 @@ public NewsletterSubscriber(String email) {
}

// Getters and Setters
public Long getId() { return id; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public boolean isActive() { return active; }
public void setActive(boolean active) { this.active = active; }
public String getUnsubscribeToken() { return unsubscribeToken; }
public void regenerateToken() { this.unsubscribeToken = UUID.randomUUID().toString(); }
public Long getId() {
return id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public String getUnsubscribeToken() {
return unsubscribeToken;
}

public void regenerateToken() {
this.unsubscribeToken = UUID.randomUUID().toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.libraryman_api.newsletter;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface NewsletterSubscriberRepository extends JpaRepository<NewsletterSubscriber, Long> {
Optional<NewsletterSubscriber> findByEmail(String email);

Optional<NewsletterSubscriber> findByUnsubscribeToken(String unsubscribeToken);
}
11 changes: 0 additions & 11 deletions src/main/resources/application-development.properties
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
## Database Setup First

## Create a Database and add database name where "Add_Your_Database_Name" is below
## Make this file as it was earlier before commiting code

# Change this connection string to this format: jdbc:mysql://{ip_address}:{port}/{database_name}
spring.datasource.url=jdbc:mysql://localhost:3306/Add_Your_Database_Name

## Add your Database Username and Password
spring.datasource.username=Add_Your_UserName
spring.datasource.password=Add_Your_Password

# Hibernate Dialect for MySQL 8
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

# Prevent early database interaction
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=never

# Format SQL
spring.jpa.properties.hibernate.format_sql=true

# Error Handling
server.error.include-binding-errors=always
server.error.include-message=always
server.error.include-stacktrace=never
server.error.include-exception=true

# Logging for Spring Security
logging.level.org.springframework.security=TRACE

# --- Mail Service Setup ---

# I use docker mail service https://hub.docker.com/r/maildev/maildev
# Or Web based Mail service https://mailtrap.io/
spring.mail.host=Add_Your_Mail_Service_Host
Expand All @@ -41,7 +31,6 @@ spring.mail.password=Add_Your_Mail_Service_Password
spring.mail.properties.mail.smtp.auth=Add_Your_Mail_Service_SMTP
spring.mail.properties.mail.starttls.enable=Add_Your_Mail_Service_Start_TLS
spring.mail.properties.domain_name=Add_Your_Mail_Service_Domain_Name

# --- Oauth 2.0 Configurations ---
spring.security.oauth2.client.registration.google.client-name=google
spring.security.oauth2.client.registration.google.client-id=ADD_YOUR_CLIENT_ID
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/application-production.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ spring.datasource.password=${DATABASE_PASSWORD}
spring.datasource.driver-class-name=${DATABASE_DRIVER_CLASS_NAME}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false

# --- Mail Service Setup ---
spring.mail.host=${MAIL_SERVICE_HOST}
spring.mail.port=${MAIL_SERVICE_PORT}
Expand All @@ -14,7 +13,6 @@ spring.mail.password=${MAIL_SERVICE_PASSWORD}
spring.mail.properties.mail.smtp.auth=${MAIL_SERVICE_SMTP}
spring.mail.properties.mail.starttls.enable=${MAIL_SERVICE_STARTTLS}
spring.mail.properties.domain_name=${MAIL_SERVICE_DOMAIN_NAME}

# --- Oauth 2.0 Configurations ---
spring.security.oauth2.client.registration.google.client-name=google
spring.security.oauth2.client.registration.google.client-id=${YOUR_CLIENT_ID}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
@SpringBootTest
class LibrarymanApiApplicationTests {

@Test
void contextLoads() {
@Test
void contextLoads() {

}
}

}

0 comments on commit eaf4315

Please sign in to comment.