|
@ -2,10 +2,14 @@ package org.waterproofingdata.wpdauth.controller; |
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.waterproofingdata.wpdauth.dto.CustomMapper; |
|
|
import org.waterproofingdata.wpdauth.dto.CustomMapper; |
|
|
import org.waterproofingdata.wpdauth.dto.UsersRequestDTO; |
|
|
import org.waterproofingdata.wpdauth.dto.UsersRequestDTO; |
|
|
import org.waterproofingdata.wpdauth.dto.UsersResponseDTO; |
|
|
import org.waterproofingdata.wpdauth.dto.UsersResponseDTO; |
|
|
|
|
|
import org.waterproofingdata.wpdauth.exception.CustomException; |
|
|
|
|
|
import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; |
|
|
import org.waterproofingdata.wpdauth.model.Users; |
|
|
import org.waterproofingdata.wpdauth.model.Users; |
|
|
|
|
|
import org.waterproofingdata.wpdauth.model.UsersEducemadenOrganizations; |
|
|
import org.waterproofingdata.wpdauth.service.UsersService; |
|
|
import org.waterproofingdata.wpdauth.service.UsersService; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.http.ResponseEntity; |
|
|
import org.springframework.http.ResponseEntity; |
|
@ -26,6 +30,10 @@ import io.swagger.annotations.ApiResponse; |
|
|
import io.swagger.annotations.ApiResponses; |
|
|
import io.swagger.annotations.ApiResponses; |
|
|
import io.swagger.annotations.Authorization; |
|
|
import io.swagger.annotations.Authorization; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Optional; |
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
@RestController |
|
|
@RestController |
|
|
@RequestMapping("/users") |
|
|
@RequestMapping("/users") |
|
|
@Api(tags = "users") |
|
|
@Api(tags = "users") |
|
@ -33,6 +41,115 @@ public class UsersController { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private UsersService userService; |
|
|
private UsersService userService; |
|
|
|
|
|
|
|
|
|
|
|
private final UsersService usersService; |
|
|
|
|
|
private final UsersService.UsersEducemadenOrganizationsService usersEducemadenOrganizationsService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
public UsersController(UsersService usersService, UsersService.UsersEducemadenOrganizationsService usersEducemadenOrganizationsService) { |
|
|
|
|
|
this.usersService = usersService; |
|
|
|
|
|
this.usersEducemadenOrganizationsService = usersEducemadenOrganizationsService; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint para associar um usuário a uma instituição |
|
|
|
|
|
@PostMapping("/associate") |
|
|
|
|
|
public ResponseEntity<UsersEducemadenOrganizations> associateUserWithOrganization( |
|
|
|
|
|
@RequestBody UsersEducemadenOrganizations userOrganizationMapping) { |
|
|
|
|
|
UsersEducemadenOrganizations savedMapping = usersEducemadenOrganizationsService.saveUserOrganizationMapping(userOrganizationMapping); |
|
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(savedMapping, HttpStatus.CREATED); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint para encontrar um mapeamento de usuário por ID do usuário |
|
|
|
|
|
@GetMapping("/organization/{userId}") |
|
|
|
|
|
public ResponseEntity<UsersEducemadenOrganizations> findUserOrganizationMappingByUserId( |
|
|
|
|
|
@PathVariable Integer userId) { |
|
|
|
|
|
UsersEducemadenOrganizations mapping = usersEducemadenOrganizationsService.findByUserId(userId); |
|
|
|
|
|
|
|
|
|
|
|
return mapping != null |
|
|
|
|
|
? new ResponseEntity<>(mapping, HttpStatus.OK) |
|
|
|
|
|
: new ResponseEntity<>(HttpStatus.NOT_FOUND); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint para encontrar um mapeamento de usuário por chave de ativação |
|
|
|
|
|
@GetMapping("/organization/activation/{activationKey}") |
|
|
|
|
|
public ResponseEntity<UsersEducemadenOrganizations> findUserOrganizationMappingByActivationKey( |
|
|
|
|
|
@PathVariable UUID activationKey) { |
|
|
|
|
|
UsersEducemadenOrganizations mapping = usersEducemadenOrganizationsService.findByActivationKey(activationKey); |
|
|
|
|
|
|
|
|
|
|
|
return mapping != null |
|
|
|
|
|
? new ResponseEntity<>(mapping, HttpStatus.OK) |
|
|
|
|
|
: new ResponseEntity<>(HttpStatus.NOT_FOUND); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
|
|
@RequestMapping("/organizations") |
|
|
|
|
|
public class EduCemadenOrganizationsController { |
|
|
|
|
|
|
|
|
|
|
|
// Inject the service |
|
|
|
|
|
private final UsersService.EduCemadenOrganizationsService organizationsService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
public EduCemadenOrganizationsController(UsersService.EduCemadenOrganizationsService organizationsService) { |
|
|
|
|
|
this.organizationsService = organizationsService; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint to get all organizations |
|
|
|
|
|
@GetMapping("/all") |
|
|
|
|
|
@PreAuthorize("hasRole('ROLE_ADMIN')") |
|
|
|
|
|
public ResponseEntity<List<EduCemadenOrganizations>> getAllOrganizations() { |
|
|
|
|
|
List<EduCemadenOrganizations> organizations = organizationsService.getAllOrganizations(); |
|
|
|
|
|
return new ResponseEntity<>(organizations, HttpStatus.OK); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint to get an organization by ID |
|
|
|
|
|
@GetMapping("/{id}") |
|
|
|
|
|
@PreAuthorize("hasRole('ROLE_INSTITUTION')") |
|
|
|
|
|
public ResponseEntity<EduCemadenOrganizations> getOrganizationById(@PathVariable Integer id) { |
|
|
|
|
|
Optional<EduCemadenOrganizations> organization = organizationsService.getOrganizationById(id); |
|
|
|
|
|
return organization.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) |
|
|
|
|
|
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Endpoint para atualizar uma instituição por ID |
|
|
|
|
|
@PutMapping("/update/{id}") |
|
|
|
|
|
@PreAuthorize("hasRole('ROLE_INSTITUTION')") |
|
|
|
|
|
public ResponseEntity<EduCemadenOrganizations> updateOrganization( |
|
|
|
|
|
@PathVariable Integer id, |
|
|
|
|
|
@RequestBody EduCemadenOrganizations updatedOrganization) { |
|
|
|
|
|
Optional<EduCemadenOrganizations> updatedOrg = organizationsService.updateOrganization(id, updatedOrganization); |
|
|
|
|
|
|
|
|
|
|
|
return updatedOrg.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) |
|
|
|
|
|
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/createInstitution") |
|
|
|
|
|
@ApiOperation( |
|
|
|
|
|
value = "${UserController.createInstitution}", |
|
|
|
|
|
notes = "This is the createInstitution method to create new institutions." |
|
|
|
|
|
) |
|
|
|
|
|
@ApiResponses(value = { |
|
|
|
|
|
@ApiResponse(code = 400, message = "Something went wrong"), |
|
|
|
|
|
@ApiResponse(code = 422, message = "Required parameters should be provided") |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
public ResponseEntity<UUID> createInstitution( |
|
|
|
|
|
@ApiParam( |
|
|
|
|
|
name = "institution", |
|
|
|
|
|
value = "Institution details", |
|
|
|
|
|
required = true |
|
|
|
|
|
) |
|
|
|
|
|
@RequestBody EduCemadenOrganizations institution |
|
|
|
|
|
) { |
|
|
|
|
|
try { |
|
|
|
|
|
// Adiciona lógica para criar instituição no serviço e retorna UUID |
|
|
|
|
|
UUID activationKey = userService.createInstitution(institution); |
|
|
|
|
|
return ResponseEntity.ok(activationKey); |
|
|
|
|
|
} catch (CustomException ce) { |
|
|
|
|
|
return ResponseEntity.status(ce.getHttpStatus()).body(null); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@PutMapping("/update") |
|
|
@PutMapping("/update") |
|
|
public ResponseEntity<Users> editUser(@RequestBody Users updatedUser) { |
|
|
public ResponseEntity<Users> editUser(@RequestBody Users updatedUser) { |
|
|
Users editedUser = userService.editUser(updatedUser); |
|
|
Users editedUser = userService.editUser(updatedUser); |
|
@ -95,27 +212,27 @@ public class UsersController { |
|
|
return userService.existsByUsername(username); |
|
|
return userService.existsByUsername(username); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@PostMapping("/existsByNickname") |
|
|
|
|
|
@ApiOperation( |
|
|
|
|
|
value = "${UserController.existsByNickname}", |
|
|
|
|
|
notes = "From a nickname, this method returns if there is a nickname in db or not." |
|
|
|
|
|
) |
|
|
|
|
|
@ApiResponses(value = {// |
|
|
|
|
|
@ApiResponse(code = 400, message = "Something went wrong") |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
public boolean existsByNickname(// |
|
|
|
|
|
@ApiParam( |
|
|
|
|
|
name = "nickname", |
|
|
|
|
|
type = "String", |
|
|
|
|
|
value = "nickname of the user", |
|
|
|
|
|
example = "This is an unique field, and consumers should be aware of it.", |
|
|
|
|
|
required = true |
|
|
|
|
|
) |
|
|
|
|
|
@RequestParam String nickname |
|
|
|
|
|
) { |
|
|
|
|
|
return userService.existsByNickname(nickname); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// @PostMapping("/existsByNickname") |
|
|
|
|
|
// @ApiOperation( |
|
|
|
|
|
// value = "${UserController.existsByNickname}", |
|
|
|
|
|
// notes = "From a nickname, this method returns if there is a nickname in db or not." |
|
|
|
|
|
// ) |
|
|
|
|
|
// @ApiResponses(value = {// |
|
|
|
|
|
// @ApiResponse(code = 400, message = "Something went wrong") |
|
|
|
|
|
// } |
|
|
|
|
|
// ) |
|
|
|
|
|
// public boolean existsByNickname(// |
|
|
|
|
|
// @ApiParam( |
|
|
|
|
|
// name = "nickname", |
|
|
|
|
|
// type = "String", |
|
|
|
|
|
// value = "nickname of the user", |
|
|
|
|
|
// example = "This is an unique field, and consumers should be aware of it.", |
|
|
|
|
|
// required = true |
|
|
|
|
|
// ) |
|
|
|
|
|
// @RequestParam String nickname |
|
|
|
|
|
// ) { |
|
|
|
|
|
// return userService.existsByNickname(nickname); |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
@PostMapping("/login") |
|
|
@PostMapping("/login") |
|
|
@ApiOperation( |
|
|
@ApiOperation( |
|
@ -168,7 +285,7 @@ public class UsersController { |
|
|
) { |
|
|
) { |
|
|
return userService.signup(CustomMapper.map(user, Users.class)); |
|
|
return userService.signup(CustomMapper.map(user, Users.class)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/activate") |
|
|
@PostMapping("/activate") |
|
|
@PreAuthorize("hasRole('ROLE_INSTITUTION') or hasRole('ROLE_CLIENT')") |
|
|
@PreAuthorize("hasRole('ROLE_INSTITUTION') or hasRole('ROLE_CLIENT')") |
|
|
@ApiOperation( |
|
|
@ApiOperation( |
|
|