-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUserController.java
More file actions
77 lines (61 loc) · 2.69 KB
/
UserController.java
File metadata and controls
77 lines (61 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.accenture.codingtest.springbootcodingtest.controller;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.accenture.codingtest.springbootcodingtest.entity.User;
import com.accenture.codingtest.springbootcodingtest.entity.exception.RecordNotFoundException;
import com.accenture.codingtest.springbootcodingtest.entity.model.UserDto;
import com.accenture.codingtest.springbootcodingtest.service.UserService;
@RestController
@RequestMapping(value = "/api/v1/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "")
public List<UserDto> findAll() {
return userService.findAll().stream().map(this::convertUserEntityToUserDto).collect(Collectors.toList());
}
@GetMapping(value = "/{userId}")
public UserDto findByUserId(@PathVariable String userId) {
return userService.findByUserId(userId).map(this::convertUserEntityToUserDto)
.orElseThrow(RecordNotFoundException::new);
}
@PostMapping("")
public UserDto create(@RequestBody UserDto userDto) {
return this.convertUserEntityToUserDto(userService.save(this.convertUserDtoToUserEntity(userDto)));
}
@PutMapping("/{userId}")
public UserDto updateIdempotent(@RequestBody UserDto userDto, @PathVariable String userId) {
return this.convertUserEntityToUserDto(userService.update(this.convertUserDtoToUserEntity(userDto), userId));
}
@PatchMapping("/{userId}")
public UserDto update(@RequestBody UserDto userDto, @PathVariable String userId) {
return this.convertUserEntityToUserDto(userService.update(this.convertUserDtoToUserEntity(userDto), userId));
}
@DeleteMapping("/{userId}")
public void delete(@PathVariable String userId) {
userService.delete(userId);
}
private UserDto convertUserEntityToUserDto(User user) {
UserDto userDto = new UserDto();
userDto.setId(user.getId());
userDto.setUsername(user.getUsername());
return userDto;
}
private User convertUserDtoToUserEntity(UserDto userDto) {
User user = new User();
user.setId(userDto.getId());
user.setUsername(userDto.getUsername());
user.setPassword(userDto.getPassword());
return user;
}
}