test_parser.java

mail@pastecode.io avatar
unknown
java
2 years ago
6.3 kB
1
Indexable
Never
package com.teethcare.controller;​

​
​
​
​

import javax.validation.Valid;​

​
​

import static org.springframework.http.HttpHeaders.AUTHORIZATION;​

​
​

@RestController​

@RequiredArgsConstructor​

@RequestMapping(path = EndpointConstant.Report.REPORT_ENDPOINT)​

public class ReportController {​

    private final ReportService reportService;​

    private final FeedbackMapper feedbackMapper;​

    private final FeedbackService feedbackService;​

    private final JwtTokenUtil jwtTokenUtil;​

    private final AccountService accountService;​

​
​

    /**​

    * Gets all reports for a given report filter request.​

    * ​

    * @param request - The request to retrieve all the reports for.​

    * @param token - The JWT token of the report filter.​

    * @param page - The page number of the report to return.​

    * @param size - The number of reports to return.​

    * @param field - The field to sort report by.​

    * @param direction - The direction of the report.​

    */​

    @GetMapping​

    @PreAuthorize("hasAnyAuthority(T(com.teethcare.common.Role).ADMIN, T(com.teethcare.common.Role).CUSTOMER_SERVICE)")​

    public ResponseEntity<Page<ReportResponse>> getAll(ReportFilterRequest request,​

            @RequestHeader(value = AUTHORIZATION) String token,​

            @RequestParam(name = "page", required = false, defaultValue = Constant.PAGINATION.DEFAULT_PAGE_NUMBER) int page,​

            @RequestParam(name = "size", required = false, defaultValue = Constant.PAGINATION.DEFAULT_PAGE_SIZE) int size,​

            @RequestParam(name = "sortBy", required = false, defaultValue = Constant.SORT.DEFAULT_SORT_BY) String field,​

            @RequestParam(name = "sortDir", required = false, defaultValue = Constant.SORT.DEFAULT_SORT_DIRECTION) String direction) {​

        token = token.substring("Bearer ".length());​

        String username = jwtTokenUtil.getUsernameFromJwt(token);​

        Account account = accountService.getAccountByUsername(username);​

​
​

        Pageable pageable = PaginationAndSortFactory.getPagable(size, page, field, direction);​

        Page<Report> list = reportService.findByStatus(pageable, request, account);​

        Page<ReportResponse> responses = list.map(feedbackMapper::mapReportToReportResponse);​

        return new ResponseEntity<>(responses, HttpStatus.OK);​

    }​

​
​

    /**​

    * GET id get a report by id.​

    * ​

    * @param id - the id of the report to retrieve​

    * @param token - The JWT token of the report.​

    */​

    @GetMapping("/{id}")​

    @PreAuthorize("hasAnyAuthority(T(com.teethcare.common.Role).ADMIN, T(com.teethcare.common.Role).CUSTOMER_SERVICE)")​

    public ResponseEntity<ReportResponse> getById(@PathVariable("id") int id,​

            @RequestHeader(value = AUTHORIZATION) String token) {​

        token = token.substring("Bearer ".length());​

        String username = jwtTokenUtil.getUsernameFromJwt(token);​

        Account account = accountService.getAccountByUsername(username);​

​
​

        Report report = reportService.findById(id, account);​

​
​

        ReportResponse response = feedbackMapper.mapReportToReportResponse(report);​

        return new ResponseEntity<>(response, HttpStatus.OK);​

    }​

​
​

    /**​

    * Adds a new report to the report.​

    * ​

    * @param token - The token of the user to add the report to the report.​

    * @param reportRequest - the report request to add.​

    */​

    @PostMapping​

    @PreAuthorize("hasAuthority(T(com.teethcare.common.Role).CUSTOMER_SERVICE)")​

    public ResponseEntity<ReportResponse> add(@RequestHeader(value = AUTHORIZATION) String token,​

            @Valid @RequestBody ReportRequest reportRequest) {​

        Account account = null;​

        /**​

        *Get the account by username from the token.​

        */​

        if (token != null) {​

            token = token.substring("Bearer ".length());​

            String username = jwtTokenUtil.getUsernameFromJwt(token);​

            account = accountService.getAccountByUsername(username);​

        }​

        Feedback feedback = feedbackService.findById(reportRequest.getFeedbackId());​

        Report report = reportService.add(reportRequest, account, feedback);​

​
​

        ReportResponse response = feedbackMapper.mapReportToReportResponse(report);​

        return new ResponseEntity<>(response, HttpStatus.OK);​

    } ​

​
​

    /**​

    * Updates an existing report.​

    * ​

    * @param request - The request to update the report with the given id.​

    * @param id - the id of the report to update44 B​

    */​

    @PutMapping("/{id}")​

    @PreAuthorize("hasAuthority(T(com.teethcare.common.Role).ADMIN)")​

    public ResponseEntity<ReportResponse> update(@RequestBody EvaluateRequest request,​

            @PathVariable("id") int id) {​

        Report report = reportService.evaluate(id, request.getStatus());​

        /**FFFFFFFFFFFFFFFFFF                                                                                                                 ​

        *Delete the feedback from the feedback service.​

        */​

        if (request.getStatus().equalsIgnoreCase(Status.Report.APPROVED.name())) {​

            feedbackService.delete(report.getFeedback().getId());​

        }​

        ReportResponse response = feedbackMapper.mapReportToReportResponse(report);​

        return new ResponseEntity<>(response, HttpStatus.OK);​

    }​

​
​

    public add(int a, int b) {​

        /**​

        *Returns the sum of the two numbers.​

        */​

        if (a > b) {​

            return a +b;​

        }​

        else {​

            return 0;​

        }​

    }​

}​

​
​

​