Skip to content

Commit

Permalink
Merge pull request #22 from sayedxali/hotfix/response-entity-usage
Browse files Browse the repository at this point in the history
hotfix: Refactored to use `ResponseEntity` in controllers (Close #7)
  • Loading branch information
seyedali-dev committed May 15, 2024
2 parents f316cdc + e07ba8f commit 61ed5e7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.HttpStatus.*;
Expand All @@ -31,56 +32,53 @@ public class TimeEntryController {
content = @Content(array = @ArraySchema(schema = @Schema(implementation = TimeEntryResponse.class)))
)
})
public Result getTimeEntries() {
return new Result(
public ResponseEntity<Result> getTimeEntries() {
return ResponseEntity.ok(new Result(
true,
OK,
"List of time entries.",
this.timeEntryService.getTimeEntries()
);
));
}

@GetMapping("/{userId}")
public Result getUsersTimeEntry(@PathVariable String userId) {
return new Result(
public ResponseEntity<Result> getUsersTimeEntry(@PathVariable String userId) {
return ResponseEntity.ok(new Result(
true,
OK,
"Time entry for user: '" + userId + "' :",
this.timeEntryService.getUsersTimeEntry(userId)
);
));
}

@PostMapping
@ResponseStatus(CREATED)
public Result addTimeEntryManually(@RequestBody TimeEntryDTO timeEntryDTO) {
return new Result(
public ResponseEntity<Result> addTimeEntryManually(@RequestBody TimeEntryDTO timeEntryDTO) {
return ResponseEntity.status(CREATED).body(new Result(
true,
CREATED,
"Time entry created successfully.",
this.timeEntryService.addTimeEntryManually(timeEntryDTO)
);
));
}

@PutMapping("/{timeEntryId}")
@ResponseStatus(OK)
public Result updateTimeEntryManually(@PathVariable String timeEntryId, @RequestBody TimeEntryDTO timeEntryDTO) {
return new Result(
public ResponseEntity<Result> updateTimeEntryManually(@PathVariable String timeEntryId, @RequestBody TimeEntryDTO timeEntryDTO) {
return ResponseEntity.ok(new Result(
true,
OK,
"Time entry for user: -> " + timeEntryId + " <- updated successfully.",
this.timeEntryService.updateTimeEntryManually(timeEntryId, timeEntryDTO)
);
));
}

@DeleteMapping("/{timeEntryId}")
@ResponseStatus(NO_CONTENT)
public Result deleteTimeEntry(@PathVariable String timeEntryId) {
public ResponseEntity<Result> deleteTimeEntry(@PathVariable String timeEntryId) {
this.timeEntryService.deleteTimeEntry(timeEntryId);
return new Result(
return ResponseEntity.status(NO_CONTENT).body(new Result(
true,
NO_CONTENT,
"Time entry deleted successfully."
);
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.seyed.ali.timeentryservice.service.interfaces.TimeEntryTrackingService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
Expand All @@ -21,38 +22,35 @@ public class TimeEntryTrackingController {
private final TimeEntryTrackingService timeEntryService;

@PostMapping("/start")
@ResponseStatus(CREATED)
public Result startTrackingTimeEntry(@RequestBody TimeBillingDTO timeBillingDTO) {
public ResponseEntity<Result> startTrackingTimeEntry(@RequestBody TimeBillingDTO timeBillingDTO) {
boolean billable = timeBillingDTO.isBillable();
BigDecimal hourlyRate = timeBillingDTO.getHourlyRate();
return new Result(
return ResponseEntity.status(CREATED).body(new Result(
true,
CREATED,
"Time tracking started...",
this.timeEntryService.startTrackingTimeEntry(billable, hourlyRate)
);
));
}

@PutMapping("/stop/{timeEntryId}")
@ResponseStatus(OK)
public Result stopTrackingTimeEntry(@PathVariable String timeEntryId) {
return new Result(
public ResponseEntity<Result> stopTrackingTimeEntry(@PathVariable String timeEntryId) {
return ResponseEntity.ok(new Result(
true,
OK,
"Time tracking stopped.",
this.timeEntryService.stopTrackingTimeEntry(timeEntryId)
);
));
}

@PutMapping("/continue/{timeEntryId}")
@ResponseStatus(OK)
public Result continueTrackingTimeEntry(@PathVariable String timeEntryId) {
return new Result(
public ResponseEntity<Result> continueTrackingTimeEntry(@PathVariable String timeEntryId) {
return ResponseEntity.ok(new Result(
true,
OK,
"Ok 👌🏻. Time tracking continued...",
this.timeEntryService.continueTrackingTimeEntry(timeEntryId)
);
));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.seyed.ali.timeentryservice.exceptions;

import com.seyed.ali.timeentryservice.model.dto.response.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@RestControllerAdvice
public class TimeEntryServiceHandlerAdvice {

@ExceptionHandler({AccessDeniedException.class})
public ResponseEntity<Result> handleAccessDeniedException(AccessDeniedException e) {
return ResponseEntity.status(FORBIDDEN).body(new Result(
false,
FORBIDDEN,
"No permission.",
"ServerMessage - " + e.getMessage()
));
}

@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<Result> handleResourceNotFoundException(ResourceNotFoundException e) {
return ResponseEntity.status(NOT_FOUND).body(new Result(
false,
NOT_FOUND,
"The requested resource was not found.",
"ServerMessage - " + e.getMessage()
));
}

}

0 comments on commit 61ed5e7

Please sign in to comment.