Skip to content

Commit

Permalink
Merge pull request #90 from dianarrmiranda/89-reformular-documentação…
Browse files Browse the repository at this point in the history
…-da-api

89 reformular documentação da api
  • Loading branch information
jnluis authored Dec 16, 2023
2 parents 0d41b3f + b5429e7 commit f7f4619
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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())) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@
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.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 jakarta.validation.Valid;

import pt.ua.ies.vineTrack.entity.Grape;
import pt.ua.ies.vineTrack.service.GrapeService;

@CrossOrigin("*")
@RestController
@RequestMapping(path = "/api/grapes")
@Tag(name = "Grapes", description = "Operations for grapes")
public class GrapeController {

@Autowired
private GrapeService grapeService;

@GetMapping()
@Operation(summary = "Get all types of grapes")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved"),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content),
})
public ResponseEntity<List<Grape>> getAllGrapes(){
try {
return ResponseEntity.ok(grapeService.getAllGrapes());
Expand All @@ -34,6 +48,16 @@ public ResponseEntity<List<Grape>> getAllGrapes(){
}

@PostMapping()
@Operation(summary = "Add a type of grape")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully added"),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content),
})
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "Grape object to be added",
required = true,
content = @Content(schema = @Schema(example = "{\"name\": \"Example Grape\", \"type\": \"Example Type\"}"))
)
public ResponseEntity<Grape> addGrape(@Valid @RequestBody Grape grape){
return ResponseEntity.ok(grapeService.save(grape));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@
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.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 pt.ua.ies.vineTrack.entity.Nutrient;
import pt.ua.ies.vineTrack.service.NutrientService;

@CrossOrigin("*")
@RestController
@RequestMapping(path = "api/nutrients")
@Tag(name = "Nutrients", description = "Operations for nutrients")
public class NutrientController {

@Autowired
NutrientService nutrientService;

@GetMapping()
@Operation(summary = "Get all nutrients")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved"),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content),
})
public ResponseEntity<List<Nutrient>> getAllNutrients(){
try {
return ResponseEntity.ok(nutrientService.getAllNutrients());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,36 @@
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.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 pt.ua.ies.vineTrack.entity.Track;
import pt.ua.ies.vineTrack.service.TrackService;

@CrossOrigin("*")
@RestController
@RequestMapping(path = "/api/tracks")
@Tag(name = "Tracks", description = "Operations for tracks")
public class TrackController {

@Autowired
TrackService trackService;

@GetMapping()
@Operation(summary = "Get all tracks")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved"),
@ApiResponse(responseCode = "404", description = "Not found", content = @Content),
})
public ResponseEntity<List<Track>> getAllVines(){
try {
return ResponseEntity.ok(trackService.getAllTracks());
} catch (Exception e) {
return ResponseEntity.notFound().build();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit f7f4619

Please sign in to comment.