Skip to content

Commit

Permalink
edit services, edit controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
a-oselkov committed Feb 21, 2024
1 parent 1976565 commit 6e865b6
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 121 deletions.
11 changes: 6 additions & 5 deletions src/main/java/hexlet/code/controller/LabelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -20,7 +21,7 @@ public interface LabelController {
@ApiResponse(responseCode = "200", description = "List of tasks",
content = @Content(schema = @Schema(implementation = Label.class))
)
List<Label> getLabels();
ResponseEntity<List<Label>> getLabels();

@Operation(summary = "Get a label by its id")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -29,7 +30,7 @@ public interface LabelController {
content = @Content(schema = @Schema(implementation = Label.class))),
@ApiResponse(responseCode = "404", description = "Task not found")
})
Label getLabel(@PathVariable Long id);
ResponseEntity<Label> getLabel(@PathVariable Long id);

@Operation(summary = "Create a new label")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -38,7 +39,7 @@ public interface LabelController {
content = @Content(schema = @Schema(implementation = Label.class))),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
Label createLabel(@RequestBody @Valid LabelDto labelDto);
ResponseEntity<Label> createLabel(@RequestBody @Valid LabelDto labelDto);

@Operation(summary = "Update the label")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -49,7 +50,7 @@ public interface LabelController {
@ApiResponse(responseCode = "404", description = "Label with that id not found"),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
Label updateUser(@PathVariable Long id,
ResponseEntity<Label> updateUser(@PathVariable Long id,
@RequestBody @Valid LabelDto labelDto);

@Operation(summary = "Delete the label")
Expand All @@ -59,5 +60,5 @@ Label updateUser(@PathVariable Long id,
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Label with that id not found")
})
void deleteUser(@PathVariable Long id);
ResponseEntity<Void> deleteUser(@PathVariable Long id);
}
11 changes: 6 additions & 5 deletions src/main/java/hexlet/code/controller/TaskController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -20,7 +21,7 @@ public interface TaskController {
@ApiResponse(responseCode = "200", description = "List of tasks",
content = @Content(schema = @Schema(implementation = Task.class))
)
Iterable<Task> getAllTasks(@QuerydslPredicate(root = Task.class) Predicate predicate);
ResponseEntity<Iterable<Task>> getAllTasks(@QuerydslPredicate(root = Task.class) Predicate predicate);

@Operation(summary = "Get a task by its id")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -29,7 +30,7 @@ public interface TaskController {
content = @Content(schema = @Schema(implementation = Task.class))),
@ApiResponse(responseCode = "404", description = "Task not found")
})
Task getTask(@PathVariable Long id);
ResponseEntity<Task> getTask(@PathVariable Long id);

@Operation(summary = "Create a new task")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -38,7 +39,7 @@ public interface TaskController {
content = @Content(schema = @Schema(implementation = Task.class))),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
Task createTask(@RequestBody @Valid TaskDto taskDto);
ResponseEntity<Task> createTask(@RequestBody @Valid TaskDto taskDto);

@Operation(summary = "Update the task")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -49,7 +50,7 @@ public interface TaskController {
@ApiResponse(responseCode = "404", description = "Task with that id not found"),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
Task updateTask(@PathVariable Long id, @RequestBody @Valid TaskDto taskDto);
ResponseEntity<Task> updateTask(@PathVariable Long id, @RequestBody @Valid TaskDto taskDto);

@Operation(summary = "Delete the task")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -58,5 +59,5 @@ public interface TaskController {
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Task with that id not found")
})
void deleteTask(@PathVariable Long id);
ResponseEntity<Void> deleteTask(@PathVariable Long id);
}
11 changes: 6 additions & 5 deletions src/main/java/hexlet/code/controller/TaskStatusController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -20,7 +21,7 @@ public interface TaskStatusController {
@ApiResponse(responseCode = "200", description = "List of task statuses",
content = @Content(schema = @Schema(implementation = TaskStatus.class))
)
List<TaskStatus> getTaskStatuses();
ResponseEntity<List<TaskStatus>> getTaskStatuses();

@Operation(summary = "Get a task status by its id")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -29,7 +30,7 @@ public interface TaskStatusController {
content = @Content(schema = @Schema(implementation = TaskStatus.class))),
@ApiResponse(responseCode = "404", description = "Task status not found")
})
TaskStatus getTaskStatus(@PathVariable Long id);
ResponseEntity<TaskStatus> getTaskStatus(@PathVariable Long id);

@Operation(summary = "Create a new task status")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -38,7 +39,7 @@ public interface TaskStatusController {
content = @Content(schema = @Schema(implementation = TaskStatus.class))),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
TaskStatus createTaskStatus(@RequestBody @Valid TaskStatusDto taskStatusDto);
ResponseEntity<TaskStatus> createTaskStatus(@RequestBody @Valid TaskStatusDto taskStatusDto);

@Operation(summary = "Update the task status")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -49,7 +50,7 @@ public interface TaskStatusController {
@ApiResponse(responseCode = "404", description = "Task status with that id not found"),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
TaskStatus updateTaskStatus(@PathVariable Long id,
ResponseEntity<TaskStatus> updateTaskStatus(@PathVariable Long id,
@RequestBody @Valid TaskStatusDto taskStatusDto);

@Operation(summary = "Delete the task status")
Expand All @@ -59,5 +60,5 @@ TaskStatus updateTaskStatus(@PathVariable Long id,
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Task status with that id not found")
})
void deleteTaskStatus(@PathVariable Long id);
ResponseEntity<Void> deleteTaskStatus(@PathVariable Long id);
}
11 changes: 6 additions & 5 deletions src/main/java/hexlet/code/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -20,7 +21,7 @@ public interface UserController {
@ApiResponse(responseCode = "200", description = "List of users",
content = @Content(schema = @Schema(implementation = User.class))
)
List<User> getUsers();
ResponseEntity<List<User>> getUsers();

@Operation(summary = "Get a user by its id")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -29,7 +30,7 @@ public interface UserController {
content = @Content(schema = @Schema(implementation = User.class))),
@ApiResponse(responseCode = "404", description = "User not found")
})
User getUser(@Parameter(description = "Id of user to be searched")
ResponseEntity<User> getUser(@Parameter(description = "Id of user to be searched")
@PathVariable Long id);

@Operation(summary = "Create a new user")
Expand All @@ -38,7 +39,7 @@ User getUser(@Parameter(description = "Id of user to be searched")
content = @Content(schema = @Schema(implementation = User.class))),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
User createUser(@RequestBody @Valid UserDto userDto);
ResponseEntity<User> createUser(@RequestBody @Valid UserDto userDto);

@Operation(summary = "Update the user")
@SecurityRequirement(name = "Bearer Authentication")
Expand All @@ -49,7 +50,7 @@ User getUser(@Parameter(description = "Id of user to be searched")
@ApiResponse(responseCode = "404", description = "User with that id not found"),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
User updateUser(@Parameter(description = "Id of user to be updated")
ResponseEntity<User> updateUser(@Parameter(description = "Id of user to be updated")
@PathVariable Long id,
@RequestBody @Valid UserDto userDto);

Expand All @@ -60,6 +61,6 @@ User updateUser(@Parameter(description = "Id of user to be updated")
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "User with that id not found")
})
void deleteUser(@Parameter(description = "Id of user to be deleted")
ResponseEntity<Void> deleteUser(@Parameter(description = "Id of user to be deleted")
@PathVariable Long id);
}
32 changes: 17 additions & 15 deletions src/main/java/hexlet/code/controller/impl/LabelControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hexlet.code.service.LabelService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,13 +16,14 @@
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static hexlet.code.controller.impl.LabelControllerImpl.LABEL_CONTROLLER_PATH;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
@AllArgsConstructor
Expand All @@ -33,33 +35,33 @@ public class LabelControllerImpl implements LabelController {
private final LabelService labelService;
private final LabelRepository labelRepository;

@GetMapping
public List<Label> getLabels() {
return labelRepository.findAll();
@GetMapping(produces = APPLICATION_JSON_VALUE)
public ResponseEntity<List<Label>> getLabels() {
return ResponseEntity.status(OK).body(labelService.getAllLabels());
}

@GetMapping(LABEL_ID)
public Label getLabel(@PathVariable final Long id) {
return labelService.getLabel(id);
@GetMapping(value = LABEL_ID, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Label> getLabel(@PathVariable final Long id) {
return ResponseEntity.status(OK).body(labelService.getLabel(id));
}

@PostMapping
@ResponseStatus(CREATED)
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('USER')")
public Label createLabel(@RequestBody @Valid final LabelDto labelDto) {
return labelService.createLabel(labelDto);
public ResponseEntity<Label> createLabel(@RequestBody @Valid final LabelDto labelDto) {
return ResponseEntity.status(CREATED).body(labelService.createLabel(labelDto));
}

@PutMapping(LABEL_ID)
@PutMapping(value = LABEL_ID, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('USER')")
public Label updateUser(@PathVariable final Long id,
public ResponseEntity<Label> updateUser(@PathVariable final Long id,
@RequestBody @Valid final LabelDto labelDto) {
return labelService.updateLabel(id, labelDto);
return ResponseEntity.status(OK).body(labelService.updateLabel(id, labelDto));
}

@DeleteMapping(LABEL_ID)
@PreAuthorize("hasAuthority('USER')")
public void deleteUser(@PathVariable final Long id) {
public ResponseEntity<Void> deleteUser(@PathVariable final Long id) {
labelService.deleteLabel(id);
return ResponseEntity.status(OK).build();
}
}
47 changes: 17 additions & 30 deletions src/main/java/hexlet/code/controller/impl/TaskControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
import hexlet.code.model.Task;
import hexlet.code.repository.TaskRepository;
import hexlet.code.service.TaskService;
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.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -23,11 +18,12 @@
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import static hexlet.code.controller.impl.TaskControllerImpl.TASK_CONTROLLER_PATH;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
@AllArgsConstructor
Expand All @@ -42,42 +38,33 @@ public class TaskControllerImpl implements TaskController {
private final TaskService taskService;
private final TaskRepository taskRepository;

@GetMapping
public Iterable<Task> getAllTasks(@QuerydslPredicate(root = Task.class) final Predicate predicate) {
return taskRepository.findAll(predicate);
@GetMapping(produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Iterable<Task>> getAllTasks(@QuerydslPredicate(root = Task.class) final Predicate predicate) {
return ResponseEntity.status(OK).body(taskService.getAllTasks(predicate));
}

@GetMapping(TASK_ID)
public Task getTask(@PathVariable final Long id) {
return taskService.getTask(id);
@GetMapping(value = TASK_ID, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Task> getTask(@PathVariable final Long id) {
return ResponseEntity.status(OK).body(taskService.getTask(id));
}

@PostMapping
@ResponseStatus(CREATED)
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('USER')")
public Task createTask(@RequestBody @Valid final TaskDto taskDto) {
return taskService.createTask(taskDto);
public ResponseEntity<Task> createTask(@RequestBody @Valid final TaskDto taskDto) {
return ResponseEntity.status(CREATED).body(taskService.createTask(taskDto));
}

@Override
@Operation(summary = "Update the task")
@SecurityRequirement(name = "Bearer Authentication")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Task updated",
content = @Content(schema = @Schema(implementation = Task.class))),
@ApiResponse(responseCode = "403", description = "Access denied"),
@ApiResponse(responseCode = "404", description = "Task with that id not found"),
@ApiResponse(responseCode = "422", description = "Incorrect input data")
})
@PutMapping(TASK_ID)
@PutMapping(value = TASK_ID, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('USER')")
public Task updateTask(@PathVariable final Long id, @RequestBody @Valid final TaskDto taskDto) {
return taskService.updateTask(id, taskDto);
public ResponseEntity<Task> updateTask(@PathVariable final Long id, @RequestBody @Valid final TaskDto taskDto) {
return ResponseEntity.status(OK).body(taskService.updateTask(id, taskDto));
}

@DeleteMapping(TASK_ID)
@PreAuthorize(TASK_OWNER)
public void deleteTask(@PathVariable final Long id) {
public ResponseEntity<Void> deleteTask(@PathVariable final Long id) {
taskService.deleteTask(id);
return ResponseEntity.status(OK).build();
}
}
Loading

0 comments on commit 6e865b6

Please sign in to comment.