Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commenting to BoardService #69

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Configuration
public class ApplicationConfiguration {
private final UserRepository userRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@

import java.util.List;

// SecurityConfiguration.java
/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@

import lombok.extern.slf4j.Slf4j;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@RequestMapping("/auth")
@RestController
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResponseEntity<BoardResponse> getBoardById(@PathVariable Long id) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User currentUser = (User) authentication.getPrincipal();
log.info("User {} is searching for kanban with id {} START", currentUser.getUserTag(), id);
BoardResponse boardResponse = boardService.getBoardByIdAndUserNew(id, currentUser.getId());
BoardResponse boardResponse = boardService.getBoardByIdAndUser(id, currentUser.getId());
log.info("User {} is searching for kanban with id {} SUCCESS", currentUser.getUserTag(), id);
return ResponseEntity.ok(boardResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@RequestMapping("/user")
@RestController
@Slf4j
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/com/backend/backend/dto/LoginResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
import lombok.Data;
import lombok.experimental.Accessors;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Data
@Accessors(chain = true)
public class LoginResponse {
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/com/backend/backend/dto/LoginUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

import lombok.Data;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Data
public class LoginUserDto {
private String email;
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/com/backend/backend/dto/RegisterUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

import lombok.Data;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Data
public class RegisterUserDto {
private String email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@RestControllerAdvice
public class GlobalExceptionHandler {

Expand Down
13 changes: 12 additions & 1 deletion api/src/main/java/com/backend/backend/filter/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@

import java.io.IOException;

// JwtRequestFilter.java
/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Component
public class JwtFilter extends OncePerRequestFilter {
private final HandlerExceptionResolver handlerExceptionResolver;
Expand Down
13 changes: 13 additions & 0 deletions api/src/main/java/com/backend/backend/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
import java.util.List;
import java.util.Set;


/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Table(name="users")
@Entity
@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

import java.util.Optional;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
Optional<User> findByUserTag(String userTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

import com.backend.backend.exceptions.UserAlreadyExistsException;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Service
public class AuthenticationService {
private final UserRepository userRepository;
Expand Down
55 changes: 42 additions & 13 deletions api/src/main/java/com/backend/backend/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,86 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* A service class that handles interactions with boards in the database. This layer is called by the controller
* classes and interacts directly with the repository interfaces to create, read, update and delete in the database.
*/
@Service
public class BoardService {

/**
* Repository with CRUD methods on the Board table in the database
*/
private final BoardRepository boardRepository;
/**
* Repository with CRUD methods on the Userboard table in the database
*/
private final UserBoardRepository userBoardRepository;

public BoardService(UserRepository userRepository, BoardRepository boardRepository, UserBoardRepository userBoardRepository) {
/**
* Constructs a new Board service and injects the repositories
* @param boardRepository - Board repository
* @param userBoardRepository - Userboard repository
*/
public BoardService(BoardRepository boardRepository, UserBoardRepository userBoardRepository) {
this.boardRepository = boardRepository;
this.userBoardRepository = userBoardRepository;
}

/**
* Creates a blank board for a user
* @param user - User to create the new board for
* @return - BoardResponse with the details of the new board for the user
*/
public BoardResponse createBoardForUser(User user) {
Board board = new Board();
boardRepository.save(board);
boardRepository.save(board); // saves board
UserBoard userBoard = new UserBoard();
userBoard.setUser(user);
userBoard.setBoard(board);
userBoardRepository.save(userBoard);
userBoardRepository.save(userBoard); // links user to the board
return new BoardResponse(board);
}

/**
* Retrieves a board by its id for a specific user
* @param boardId - The ID of the board to find
* @param userId - The ID of the user who is searching for the board
* @return A BoardResponse with the details of the board found for the user
* @throws EntityNotFoundException if board id is not found for that user
*/
public BoardResponse getBoardByIdAndUser(Long boardId, Long userId) {
Board board = boardRepository.findByIdAndUserId(boardId, userId)
.orElseThrow(() -> new EntityNotFoundException("Board not found with id " + boardId.toString()));

BoardResponse response = new BoardResponse(board);
return response;

}

public BoardResponse getBoardByIdAndUserNew(Long boardId, Long userId) {
// Find board with id
Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new EntityNotFoundException("Board not found with id " + boardId.toString()));

// Check if user is a member of the board
if ( board.getUserBoards().stream().anyMatch(userBoard -> userBoard.getUser().getId() == userId)) {
return new BoardResponse(board);
}
else {
// User is not a member of the board so throw error
throw new EntityNotFoundException("Board not found with id " + boardId.toString());
}

}

/**
* Deletes a board for a user
* @param user - User who is trying to delete the board
* @param boardId - ID of the board that will be deleted
* @throws EntityNotFoundException if boared with correct id and user not found
*/
@Transactional
public void deleteBoard(User user, Long boardId) {
// Find board with board id for that user
Board board = boardRepository.findByIdAndUserId(boardId, user.getId())
.orElseThrow(() -> new EntityNotFoundException("Board not found with id " + boardId.toString()));

// Cleanup userboards linking users to the board
for (UserBoard userBoard : board.getUserBoards()) {
userBoardRepository.delete(userBoard);
}
// Delete the board
boardRepository.delete(board);
}

Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/com/backend/backend/service/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
import java.util.Map;
import java.util.function.Function;

/**
* Acknowledgements:
* This class was written by following the instructions in the "Implement JWT authentication
* in a Spring Boot 3 application" tutorial on Medium published by Eric C. Togo.
* The tutorial was published on 20/03/2024.
*
* The full APA 7th reference is:
* Tiogo, E.C. (2024) Implement JWT authentication in a Spring Boot 3
* application, Medium. Available at:
* https://medium.com/@tericcabrel/implement-jwt-authentication-in-a-spring-boot-3-application-5839e4fd8fac
* (Accessed: 22 June 2024).
*/
@Service
@AllArgsConstructor
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Acknowledgements:
* ChatGPT (https://chatgpt.com/) was used to assist in writing test
* cases. The test cases were further edited manually for correctness, brevity and
* coverage of cases in the code
*/
@SpringBootTest
class BackendApplicationTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* Acknowledgements:
* ChatGPT (https://chatgpt.com/) was used to assist in writing test
* cases. The test cases were further edited manually for correctness, brevity and
* coverage of cases in the code
*/
class AuthenticationControllerTests {

@InjectMocks
Expand Down
Loading
Loading