-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from dianarrmiranda/89-reformular-documentação…
…-da-api 89 reformular documentação da api
- Loading branch information
Showing
6 changed files
with
266 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,12 @@ | |
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
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 io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import pt.ua.ies.vineTrack.dao.JwtResponse; | ||
import pt.ua.ies.vineTrack.dao.request.LoginRequest; | ||
import pt.ua.ies.vineTrack.dao.response.MessageResponse; | ||
|
@@ -26,6 +32,7 @@ | |
@CrossOrigin(origins = "*", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/api/authentication") | ||
@Tag(name = "Authentication", description = "Operations for authentication") | ||
public class AuthController { | ||
@Autowired | ||
AuthenticationManager authenticationManager; | ||
|
@@ -40,6 +47,11 @@ public class AuthController { | |
JwtUtils jwtUtils; | ||
|
||
@PostMapping("/login") | ||
@Operation(summary = "Authenticate a client with an email and password") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully authenticated", content = @Content), | ||
@ApiResponse(responseCode = "401", description = "Invalid credentials", content = @Content), | ||
}) | ||
public ResponseEntity<?> authenticateClient(@Valid @RequestBody LoginRequest loginRequest) { | ||
User user = userRepo.findByEmail(loginRequest.getEmail()).orElse(null); | ||
|
||
|
@@ -61,6 +73,16 @@ public ResponseEntity<?> authenticateClient(@Valid @RequestBody LoginRequest log | |
} | ||
|
||
@PostMapping("/register") | ||
@Operation(summary = "Register a new user") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully registered", content = @Content), | ||
@ApiResponse(responseCode = "400", description = "E-mail is already in use", content = @Content), | ||
}) | ||
@io.swagger.v3.oas.annotations.parameters.RequestBody( | ||
description = "User registration details", | ||
required = true, | ||
content = @Content(schema = @Schema(example = "{\"name\": \"Exemplo Ex\", \"email\": \"[email protected]\", \"password\": \"examplePassword\"}")) | ||
) | ||
public ResponseEntity<?> registerUser(@Valid @RequestBody User user) { | ||
if (userRepo.existsByEmail(user.getEmail())) { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,19 @@ | |
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jakarta.validation.Valid; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import pt.ua.ies.vineTrack.service.UserService; | ||
import pt.ua.ies.vineTrack.entity.User; | ||
import pt.ua.ies.vineTrack.entity.Notification; | ||
|
@@ -28,6 +34,7 @@ | |
@RestController | ||
@RequestMapping(path = "/api/users") | ||
@ResponseStatus(HttpStatus.OK) | ||
@Tag(name = "Users", description = "Operations for users") | ||
public class UserController { | ||
@Autowired | ||
private UserService userService; | ||
|
@@ -36,30 +43,41 @@ public class UserController { | |
private NotificationService notificationService; | ||
|
||
@GetMapping() | ||
@Operation(summary = "Get all users") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved users"), | ||
@ApiResponse(responseCode = "404", description = "No users found", content = @Content), | ||
}) | ||
public ResponseEntity<List<User>> getAllUsers(){ | ||
try { | ||
return ResponseEntity.ok(userService.getAllUsers()); | ||
return ResponseEntity.ok(userService.getAllUsers()); | ||
} catch (Exception e) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} | ||
|
||
@PostMapping() | ||
public ResponseEntity<User> addUser(@Valid @RequestBody User user){ | ||
return ResponseEntity.ok(userService.save(user)); | ||
} | ||
|
||
@GetMapping(path = "/{id}") | ||
@Operation(summary = "Get a user by id") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved user"), | ||
@ApiResponse(responseCode = "404", description = "User not found", content = @Content), | ||
}) | ||
@Parameter(name = "id", description = "User id", required = true, example = "1") | ||
public ResponseEntity<User> viewUser(@PathVariable Integer id){ | ||
try { | ||
return ResponseEntity.ok(userService.getUserById(id)); | ||
} catch (Exception e) { | ||
} catch (Exception e) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} | ||
|
||
|
||
@GetMapping(path = "/email/{email}") | ||
@Operation(summary = "Get a user by email") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved user"), | ||
@ApiResponse(responseCode = "404", description = "User not found", content = @Content), | ||
}) | ||
@Parameter(name = "email", description = "User email", required = true, example = "[email protected]") | ||
public ResponseEntity<User> viewUser(@PathVariable String email){ | ||
try { | ||
return ResponseEntity.ok(userService.getUserByEmail(email)); | ||
|
@@ -69,6 +87,12 @@ public ResponseEntity<User> viewUser(@PathVariable String email){ | |
} | ||
|
||
@GetMapping(path = "/vines/{userId}") | ||
@Operation(summary = "Get all vines from a user") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved vines"), | ||
@ApiResponse(responseCode = "404", description = "No vines found", content = @Content), | ||
}) | ||
@Parameter(name = "userId", description = "User id", required = true, example = "1") | ||
public ResponseEntity<List<Integer>> getVinesByUserId(@PathVariable Integer userId){ | ||
try { | ||
return ResponseEntity.ok(userService.getVinesIds(userId)); | ||
|
@@ -78,6 +102,12 @@ public ResponseEntity<List<Integer>> getVinesByUserId(@PathVariable Integer user | |
} | ||
|
||
@GetMapping(path = "/notifications/{userId}") | ||
@Operation(summary = "Get all notifications from a user") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully retrieved notifications"), | ||
@ApiResponse(responseCode = "404", description = "No notifications found", content = @Content), | ||
}) | ||
@Parameter(name = "userId", description = "User id", required = true, example = "1") | ||
// get all notifications from a vine | ||
public List<Notification> getNotificationsByUserId(@PathVariable Integer userId){ | ||
User user = userService.getUserById(userId); | ||
|
@@ -121,6 +151,12 @@ public List<Notification> getNotificationsByUserId(@PathVariable Integer userId) | |
|
||
|
||
@PutMapping("/markAsRead/{notificationId}") | ||
@Operation(summary = "Mark a notification as read") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully marked notification as read"), | ||
@ApiResponse(responseCode = "404", description = "Notification not found", content = @Content), | ||
}) | ||
@Parameter(name = "notificationId", description = "Notification id", required = true, example = "1") | ||
public ResponseEntity<Notification> markNotificationAsRead(@PathVariable int notificationId) { | ||
try { | ||
Notification notification = notificationService.getNotificationById(notificationId); | ||
|
@@ -137,14 +173,28 @@ public ResponseEntity<Notification> markNotificationAsRead(@PathVariable int not | |
} | ||
} | ||
|
||
|
||
|
||
@PutMapping() | ||
@Operation(summary = "Update a user") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully updated user"), | ||
@ApiResponse(responseCode = "404", description = "User not found", content = @Content), | ||
}) | ||
@io.swagger.v3.oas.annotations.parameters.RequestBody( | ||
description = "User object to be updated", | ||
required = true, | ||
content = @Content(schema = @Schema(example = "{\"id\": 1, \"name\": \"Example User\", \"email\": \"[email protected]\"}")) | ||
) | ||
public User updateUser(@RequestBody User user){ | ||
return userService.updateUser(user); | ||
} | ||
|
||
@DeleteMapping(path = "/{id}") | ||
@Operation(summary = "Delete a user") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Successfully deleted user", content = @Content(schema = @Schema(example = "User deleted successfully!"))), | ||
@ApiResponse(responseCode = "404", description = "User not found", content = @Content), | ||
}) | ||
@Parameter(name = "id", description = "User id", required = true, example = "1") | ||
public String deleteUser(@PathVariable Integer id){ | ||
return userService.deleteUserById(id); | ||
} | ||
|
Oops, something went wrong.