DocuSphere/server/src/main/java/com/mixel/docusphere/controller/UserController.java

52 lines
No EOL
1.7 KiB
Java

package com.mixel.docusphere.controller;
import com.mixel.docusphere.dto.request.UserRequestDTO;
import com.mixel.docusphere.dto.response.UserRespondDTO;
import com.mixel.docusphere.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<UserRespondDTO> getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<UserRespondDTO> getUserById(@PathVariable UUID id) {
Optional<UserRespondDTO> user = userService.findById(id);
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
@PostMapping
public UserRespondDTO createUser(@RequestBody UserRequestDTO userRequestDTO) {
return userService.save(userRequestDTO);
}
@PutMapping("/{id}")
public ResponseEntity<UserRespondDTO> updateUser(@PathVariable UUID id, @RequestBody UserRequestDTO userRequestDTO) {
Optional<UserRespondDTO> updatedUser = userService.update(id, userRequestDTO);
return updatedUser.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable UUID id) {
userService.deleteById(id);
return ResponseEntity.noContent().build();
}
}