Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: Refactored to use ResponseEntity in controllers (Close #7) #22

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
));
}

}