Skip to content
Snippets Groups Projects
Commit 1ec643c2 authored by Joakim Falch's avatar Joakim Falch
Browse files

Implemented Swagger documentation

parent 88e480ef
No related branches found
No related tags found
3 merge requests!45Added some functionality to User.,!44Added some functionality to User.,!42Implemented Swagger documentation
Pipeline #223516 passed
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
2. Installation 2. Installation
3. Running the Application 3. Running the Application
4. Running the tests 4. Running the tests
5. Docker Commands 5. Swagger Documentation
6. Docker Commands
--- ---
...@@ -45,7 +46,13 @@ Open a terminal and navigate to the root directory of the project, from here you ...@@ -45,7 +46,13 @@ Open a terminal and navigate to the root directory of the project, from here you
--- ---
### 5. Docker Commands ### 5. Swagger Documentation
To get an overview of the API endpoints, you can use the swagger documentation.
To access this, make sure the application is running and open a browser and navigate to `http://localhost:8080/swagger-ui/index.html`
---
### 6. Docker Commands
Here is a list of commands you can use in docker to manage the containers. These commands should be run from the root directory of the project. Here is a list of commands you can use in docker to manage the containers. These commands should be run from the root directory of the project.
......
...@@ -95,6 +95,11 @@ ...@@ -95,6 +95,11 @@
<version>1.17.6</version> <version>1.17.6</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -5,6 +5,10 @@ import edu.ntnu.idatt2106.backend.model.user.SubUserRequest; ...@@ -5,6 +5,10 @@ import edu.ntnu.idatt2106.backend.model.user.SubUserRequest;
import edu.ntnu.idatt2106.backend.model.user.User; import edu.ntnu.idatt2106.backend.model.user.User;
import edu.ntnu.idatt2106.backend.model.user.UserRequest; import edu.ntnu.idatt2106.backend.model.user.UserRequest;
import edu.ntnu.idatt2106.backend.service.UserService; import edu.ntnu.idatt2106.backend.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -37,11 +41,20 @@ public class UserController { ...@@ -37,11 +41,20 @@ public class UserController {
* @param userRequest email, phone number, address, role and password for the user being saved * @param userRequest email, phone number, address, role and password for the user being saved
* @return the saved user * @return the saved user
*/ */
@ApiResponse(responseCode = "200", description = "User created successfully",
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) })
@ApiResponse(responseCode = "409", description = "User with given email already exists",
content = { @Content(mediaType = "application/json") })
@PostMapping("/create") @PostMapping("/create")
public ResponseEntity<String> createUserWithoutChild(@RequestBody UserRequest userRequest) { public ResponseEntity<String> createUserWithoutChild(@RequestBody UserRequest userRequest) {
return userService.createUserWithoutChild(userRequest); return userService.createUserWithoutChild(userRequest);
} }
@ApiResponse(responseCode = "200", description = "User created successfully",
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) })
@ApiResponse(responseCode = "409", description = "User with given email already exists",
content = { @Content(mediaType = "application/json") })
@PostMapping("/create/child") @PostMapping("/create/child")
public ResponseEntity<String> createUserWithChild(@RequestBody UserRequest userRequest) { public ResponseEntity<String> createUserWithChild(@RequestBody UserRequest userRequest) {
return userService.createUserWithChild(userRequest); return userService.createUserWithChild(userRequest);
...@@ -57,6 +70,10 @@ public class UserController { ...@@ -57,6 +70,10 @@ public class UserController {
* @return a {@link UserRequest} object containing the user's details and JWT token if the authentication succeeds, * @return a {@link UserRequest} object containing the user's details and JWT token if the authentication succeeds,
* or null if the authentication fails * or null if the authentication fails
*/ */
@ApiResponse(responseCode = "200", description = "Login successful",
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) })
@ApiResponse(responseCode = "400", description = "User with given email does not exist",
content = { @Content(mediaType = "application/json") })
@PostMapping("/login") @PostMapping("/login")
public ResponseEntity<Map<String, Object>> loginUser( public ResponseEntity<Map<String, Object>> loginUser(
@RequestBody UserRequest user, @RequestBody UserRequest user,
...@@ -75,6 +92,8 @@ public class UserController { ...@@ -75,6 +92,8 @@ public class UserController {
* @param phoneNumber The new phone number for the user. * @param phoneNumber The new phone number for the user.
* @return A ResponseEntity containing a success or error message. * @return A ResponseEntity containing a success or error message.
*/ */
@ApiResponse(responseCode = "200", description = "Phone number changed",
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) })
@PutMapping("/edit/phone") @PutMapping("/edit/phone")
public ResponseEntity<String> editPhoneNumber(@RequestParam String phoneNumber, @AuthenticationPrincipal User user){ public ResponseEntity<String> editPhoneNumber(@RequestParam String phoneNumber, @AuthenticationPrincipal User user){
return userService.editPhoneNumber(user, phoneNumber); return userService.editPhoneNumber(user, phoneNumber);
......
...@@ -40,6 +40,10 @@ public class JWTRequestFilter extends OncePerRequestFilter { ...@@ -40,6 +40,10 @@ public class JWTRequestFilter extends OncePerRequestFilter {
new AntPathRequestMatcher("/user/auth/refreshToken"), new AntPathRequestMatcher("/user/auth/refreshToken"),
new AntPathRequestMatcher("/swagger-ui/**"), new AntPathRequestMatcher("/swagger-ui/**"),
new AntPathRequestMatcher("/v3/api-docs/**"), new AntPathRequestMatcher("/v3/api-docs/**"),
new AntPathRequestMatcher("/swagger-ui.html"),
new AntPathRequestMatcher("/swagger.json"),
new AntPathRequestMatcher("/swagger-resources/**"),
new AntPathRequestMatcher("/webjars/**"),
new AntPathRequestMatcher("/addProduct") new AntPathRequestMatcher("/addProduct")
); );
private final RequestMatcher allowedUrls = new OrRequestMatcher(PUBLIC_URLS); private final RequestMatcher allowedUrls = new OrRequestMatcher(PUBLIC_URLS);
......
...@@ -12,3 +12,4 @@ jwt.algorithm.key=VerySecureAlgorithmKey ...@@ -12,3 +12,4 @@ jwt.algorithm.key=VerySecureAlgorithmKey
jwt.issuer=smartMat jwt.issuer=smartMat
jwt.accessToken.expiryInSeconds=600 jwt.accessToken.expiryInSeconds=600
jwt.refreshToken.expiryInSeconds=86400 jwt.refreshToken.expiryInSeconds=86400
springdoc.swagger-ui.url=/v3/api-docs
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment