Skip to content

Commit

Permalink
Add javadoc Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaynegi45 committed Sep 5, 2024
1 parent 4aecfe7 commit 7e217bf
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 89 deletions.
38 changes: 37 additions & 1 deletion src/main/java/com/libraryman_api/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,73 @@

import java.util.List;

/**
* REST controller for managing books in the LibraryMan application.
* This controller provides endpoints for performing CRUD operations on books,
* including retrieving all books, getting a book by its ID, adding a new book,
* updating an existing book, and deleting a book.
*/
@RestController
@RequestMapping("/api/books")
public class BookController {

@Autowired
private BookService bookService;

/**
* Retrieves a list of all books in the library.
*
* @return a list of {@link Books} objects representing all the books in the library.
*/
@GetMapping
public List<Books> getAllBooks() {
return bookService.getAllBooks();
}

/**
* Retrieves a book by its ID.
*
* @param id the ID of the book to retrieve.
* @return a {@link ResponseEntity} containing the {@link Books} object, if found.
* @throws ResourceNotFoundException if the book with the specified ID is not found.
*/
@GetMapping("/{id}")
public ResponseEntity<Books> getBookById(@PathVariable int id) {
return bookService.getBookById(id)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
}

/**
* Adds a new book to the library.
*
* @param book the {@link Books} object representing the new book to add.
* @return the added {@link Books} object.
*/
@PostMapping
public Books addBook(@RequestBody Books book) {
return bookService.addBook(book);
}

/**
* Updates an existing book in the library.
*
* @param id the ID of the book to update.
* @param bookDetails the {@link Books} object containing the updated book details.
* @return the updated {@link Books} object.
*/
@PutMapping("/{id}")
public Books updateBook(@PathVariable int id, @RequestBody Books bookDetails) {
return bookService.updateBook(id, bookDetails);
}

/**
* Deletes a book from the library by its ID.
*
* @param id the ID of the book to delete.
*/
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable int id) {
bookService.deleteBook(id);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,93 @@
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.service.BorrowingService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* REST controller for managing borrowings in the LibraryMan application.
* This controller provides endpoints for performing operations related to borrowing and returning books,
* paying fines, and retrieving borrowing records.
*/
@RestController
@RequestMapping("/api/borrowings")
public class BorrowingController {

private final BorrowingService borrowingService;

/**
* Constructs a new {@code BorrowingController} with the specified {@link BorrowingService}.
*
* @param borrowingService the service used to handle borrowing-related operations.
*/
public BorrowingController(BorrowingService borrowingService) {
this.borrowingService = borrowingService;
}

/**
* Retrieves a list of all borrowing records in the library.
*
* @return a list of {@link Borrowings} objects representing all borrowings.
*/
@GetMapping
public List<Borrowings> getAllBorrowings() {
return borrowingService.getAllBorrowings();
}

/**
* Records a new book borrowing.
*
* @param borrowing the {@link Borrowings} object containing borrowing details.
* @return the saved {@link Borrowings} object representing the borrowing record.
*/
@PostMapping
public Borrowings borrowBook(@RequestBody Borrowings borrowing) {
return borrowingService.borrowBook(borrowing);
}

/**
* Marks a borrowed book as returned.
*
* @param id the ID of the borrowing record to update.
*/
@PutMapping("/{id}/return")
public void returnBook(@PathVariable int id) {
borrowingService.returnBook(id);
}

/**
* Pays the fine for an overdue book.
*
* @param id the ID of the borrowing record for which the fine is being paid.
* @return a message indicating the payment status.
*/
@PutMapping("/{id}/pay")
public String payFine(@PathVariable int id) {
System.out.println("Pay Fine Id: " + id);
return borrowingService.payFine(id);
}

/**
* Retrieves all borrowing records for a specific member.
*
* @param memberId the ID of the member whose borrowing records are to be retrieved.
* @return a list of {@link Borrowings} objects representing the member's borrowings.
*/
@GetMapping("member/{memberId}")
public List<Borrowings> getAllBorrowingsOfAMember(@PathVariable int memberId) {
return borrowingService.getAllBorrowingsOfMember(memberId);
}


/**
* Retrieves a borrowing record by its ID.
*
* @param borrowingId the ID of the borrowing record to retrieve.
* @return the {@link Borrowings} object representing the borrowing record.
* @throws ResourceNotFoundException if the borrowing record with the specified ID is not found.
*/
@GetMapping("{borrowingId}")
public Borrowings getBorrowingById(@PathVariable int borrowingId) {
return borrowingService.getBorrowingById(borrowingId).orElseThrow(() -> new ResourceNotFoundException("Borrowing not found"));
return borrowingService.getBorrowingById(borrowingId)
.orElseThrow(() -> new ResourceNotFoundException("Borrowing not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,24 @@

import java.util.Date;

/**
* Global exception handler for the LibraryMan API.
* This class provides centralized exception handling across all controllers in the application.
* It handles specific exceptions and returns appropriate HTTP responses.
*/
@ControllerAdvice
public class GlobalExceptionHandler {

/**
* Handles {@link ResourceNotFoundException} exceptions.
* This method is triggered when a {@code ResourceNotFoundException} is thrown in the application.
* It constructs an {@link ErrorDetails} object containing the exception details and returns
* a {@link ResponseEntity} with an HTTP status of {@code 404 Not Found}.
*
* @param ex the exception that was thrown.
* @param request the current web request in which the exception was thrown.
* @return a {@link ResponseEntity} containing the {@link ErrorDetails} and an HTTP status of {@code 404 Not Found}.
*/
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/com/libraryman_api/controller/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,88 @@
package com.libraryman_api.controller;

import com.libraryman_api.entity.Borrowings;
import com.libraryman_api.entity.Members;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.service.BorrowingService;
import com.libraryman_api.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* REST controller for managing library members.
* This controller provides endpoints for performing CRUD operations on members.
*/
@RestController
@RequestMapping("/api/members")
public class MemberController {

private final MemberService memberService;

/**
* Constructs a new {@code MemberController} with the specified {@link MemberService}.
*
* @param memberService the service to handle member-related operations
*/
public MemberController(MemberService memberService) {
this.memberService = memberService;
}



/**
* Retrieves a list of all library members.
*
* @return a list of {@link Members} representing all members in the library
*/
@GetMapping
public List<Members> getAllMembers() {
return memberService.getAllMembers();
}

/**
* Retrieves a library member by their ID.
* If the member is not found, a {@link ResourceNotFoundException} is thrown.
*
* @param id the ID of the member to retrieve
* @return a {@link ResponseEntity} containing the found {@link Members} object
*/
@GetMapping("/{id}")
public ResponseEntity<Members> getMemberById(@PathVariable int id) {
return memberService.getMemberById(id)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ResourceNotFoundException("Member not found"));
}

/**
* Adds a new library member.
*
* @param member the {@link Members} object representing the new member
* @return the added {@link Members} object
*/
@PostMapping
public Members addMember(@RequestBody Members member) {
return memberService.addMember(member);
}

/**
* Updates an existing library member.
* If the member is not found, a {@link ResourceNotFoundException} is thrown.
*
* @param id the ID of the member to update
* @param memberDetails the {@link Members} object containing the updated details
* @return the updated {@link Members} object
*/
@PutMapping("/{id}")
public Members updateMember(@PathVariable int id, @RequestBody Members memberDetails) {
return memberService.updateMember(id, memberDetails);
}

/**
* Deletes a library member by their ID.
* If the member is not found, a {@link ResourceNotFoundException} is thrown.
*
* @param id the ID of the member to delete
*/
@DeleteMapping("/{id}")
public void deleteMember(@PathVariable int id) {
memberService.deleteMember(id);
}
}

18 changes: 17 additions & 1 deletion src/main/java/com/libraryman_api/email/EmailSender.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package com.libraryman_api.email;

import com.libraryman_api.entity.Notifications;

/**
* Interface representing an email sending service for the Library Management System.
* Classes implementing this interface are responsible for sending emails
* and updating the status of notifications in the system.
*/
public interface EmailSender {
void send (String to, String body, String subject, Notifications notification);

/**
* Sends an email to the specified recipient with the given body, subject, and notification details.
*
* @param to the email address of the recipient
* @param body the body of the email, typically in HTML or plain text format
* @param subject the subject of the email
* @param notification the notification entity associated with the email being sent,
* used to track the status of the notification
*/
void send(String to, String body, String subject, Notifications notification);
}
48 changes: 35 additions & 13 deletions src/main/java/com/libraryman_api/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,65 @@
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
* Service class for sending emails asynchronously.
* This class handles the construction and sending of MIME email messages.
*/
@Service
public class EmailService implements EmailSender{
public class EmailService implements EmailSender {

private final NotificationRepository notificationRepository;
private final JavaMailSender mailSender;
public EmailService(NotificationRepository notificationRepository, JavaMailSender mailSender) {
this.notificationRepository = notificationRepository;
this.mailSender = mailSender;
}
private final static Logger LOGGER = LoggerFactory
.getLogger(EmailService.class);

private static final Logger LOGGER = LoggerFactory.getLogger(EmailService.class);

@Value("${spring.mail.properties.domain_name}")
private String domainName;

/**
* Constructs a new {@code EmailService} with the specified {@link NotificationRepository} and {@link JavaMailSender}.
*
* @param notificationRepository the repository for managing notification entities
* @param mailSender the mail sender for sending email messages
*/
public EmailService(NotificationRepository notificationRepository, JavaMailSender mailSender) {
this.notificationRepository = notificationRepository;
this.mailSender = mailSender;
}

/**
* Sends an email asynchronously to the specified recipient.
* If the email is successfully sent, the notification status is updated to SENT.
* If the email fails to send, the notification status is updated to FAILED and an exception is thrown.
*
* @param to the recipient's email address
* @param email the content of the email to send
* @param subject the subject of the email
* @param notification the {@link Notifications} object representing the email notification
*/
@Override
@Async
public void send(String to, String email, String subject, Notifications notification) {
public void send(String to, String email, String subject, Notifications notification) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, "utf-8");
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
helper.setText(email, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setFrom(domainName);
mailSender.send(mimeMessage);

// Update notification status to SENT
notification.setNotificationStatus(NotificationStatus.SENT);
notificationRepository.save(notification);
} catch (MessagingException e) {
LOGGER.error("failed to send email", e);
LOGGER.error("Failed to send email", e);

// Update notification status to FAILED
notification.setNotificationStatus(NotificationStatus.FAILED);
notificationRepository.save(notification);
throw new IllegalStateException("failed to send email");

throw new IllegalStateException("Failed to send email", e);
}
}
}
Loading

0 comments on commit 7e217bf

Please sign in to comment.