Commit c9c621d4 authored by Alex Segers's avatar Alex Segers

[AFP-91] 🎨 Create custom annotations & resolvers for 'ManagerController' (@asegers)

parent 3030a1ce
package com.afp.ordermanagement.controller; package com.afp.ordermanagement.controller;
import com.afp.ordermanagement.annotation.AuthManagerController;
import com.afp.ordermanagement.annotation.ManagerPayload;
import com.afp.ordermanagement.exception.ResourceNotFoundException;
import com.afp.ordermanagement.model.Manager; import com.afp.ordermanagement.model.Manager;
import com.afp.ordermanagement.repository.ManagerRepository; import com.afp.ordermanagement.service.ManagerService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;
import javax.validation.Valid;
@Validated
@RestController @RestController
@RequestMapping("/api") @AuthManagerController
@RequestMapping("/api/managers/")
public class ManagerController { public class ManagerController {
@Autowired @Autowired
ManagerRepository managerRepository; ManagerService managerService;
@GetMapping("/manager") @PostMapping("/auth")
public Flux<Manager> getAllManagers() { public ResponseEntity<Mono<Manager>> signUpOrLogInManager(@ManagerPayload Manager managerPayload) {
System.out.println("here"); Mono<Manager> manager = managerService.getByEmail(managerPayload.getEmail())
Flux<Manager> managerFlux = managerRepository.findAll(); .switchIfEmpty(managerService.create(managerPayload));
return managerFlux; return ResponseEntity.ok(manager);
} }
@GetMapping("/account")
public ResponseEntity<Mono<Manager>> getManagerDetails(@ManagerPayload Manager managerPayload) {
Mono<Manager> existingManager = managerService.getByEmail(managerPayload.getEmail())
.switchIfEmpty(Mono.error(new ResourceNotFoundException()));
return ResponseEntity.ok(existingManager);
}
@PatchMapping("/account")
public ResponseEntity<Mono<Manager>> updateManagerDetails(@ManagerPayload Manager managerPayload, @Valid @RequestBody Manager managerBody) {
return ResponseEntity.ok(managerService.updateByEmail(managerPayload.getEmail(), managerBody));
}
@DeleteMapping("/account")
public ResponseEntity deleteManagerDetails(@ManagerPayload Manager managerPayload) {
managerService.getByEmail(managerPayload.getEmail())
.switchIfEmpty(Mono.error(new ResourceNotFoundException()))
.flatMap(managerService::delete);
return ResponseEntity.noContent().build();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment