Skip to content

Commit 70adbe1

Browse files
committed
add /api/users/{id}
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent d4f8064 commit 70adbe1

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

src/main/java/io/hexlet/blog/controller/api/UsersController.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import java.util.List;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PutMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
812
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
914
import org.springframework.web.bind.annotation.RestController;
1015

1116
import io.hexlet.blog.dto.UserDTO;
12-
import io.hexlet.blog.mapper.UserMapperImpl;
17+
import io.hexlet.blog.dto.UserUpdateDTO;
18+
import io.hexlet.blog.exception.ResourceNotFoundException;
19+
import io.hexlet.blog.mapper.UserMapper;
1320
import io.hexlet.blog.repository.UserRepository;
1421
import lombok.AllArgsConstructor;
1522

@@ -21,7 +28,7 @@ public class UsersController {
2128
private final UserRepository repository;
2229

2330
@Autowired
24-
private UserMapperImpl userMapper;
31+
private UserMapper userMapper;
2532

2633
@GetMapping("/users")
2734
ResponseEntity<List<UserDTO>> index() {
@@ -33,4 +40,15 @@ ResponseEntity<List<UserDTO>> index() {
3340
.header("X-Total-Count", String.valueOf(users.size()))
3441
.body(result);
3542
}
43+
44+
@PutMapping("/users/{id}")
45+
@ResponseStatus(HttpStatus.OK)
46+
UserDTO update(@RequestBody UserUpdateDTO userData, @PathVariable Long id) {
47+
var user = repository.findById(id)
48+
.orElseThrow(() -> new ResourceNotFoundException("Not Found"));
49+
userMapper.update(userData, user);
50+
repository.save(user);
51+
var userDTO = userMapper.map(user);
52+
return userDTO;
53+
}
3654
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.hexlet.blog.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Setter
8+
@Getter
9+
public class UserUpdateDTO {
10+
@NotNull
11+
private String firstName;
12+
// private String lastName;
13+
}
14+

src/main/java/io/hexlet/blog/mapper/UserMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.mapstruct.ReportingPolicy;
1111

1212
import io.hexlet.blog.dto.UserDTO;
13+
import io.hexlet.blog.dto.UserUpdateDTO;
1314
import io.hexlet.blog.model.User;
1415

1516
@Mapper(
@@ -20,8 +21,9 @@
2021
)
2122
public abstract class UserMapper {
2223
public abstract User map(UserDTO model);
24+
public abstract User map(UserUpdateDTO model);
2325

2426
public abstract UserDTO map(User model);
2527

26-
public abstract void update(UserDTO update, @MappingTarget User destination);
28+
public abstract void update(UserUpdateDTO update, @MappingTarget User destination);
2729
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,86 @@
11
package io.hexlet.blog.controller.api;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
34
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
45
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
57
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
68

9+
import java.util.HashMap;
10+
11+
import org.instancio.Instancio;
12+
import org.instancio.Select;
13+
import org.junit.jupiter.api.BeforeEach;
714
import org.junit.jupiter.api.Test;
815
import org.springframework.beans.factory.annotation.Autowired;
916
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1017
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.http.MediaType;
19+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.JwtRequestPostProcessor;
1120
import org.springframework.test.web.servlet.MockMvc;
1221

22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
24+
import io.hexlet.blog.model.User;
25+
import io.hexlet.blog.repository.UserRepository;
26+
import io.hexlet.blog.util.ModelGenerator;
27+
import net.datafaker.Faker;
28+
1329
@SpringBootTest
1430
@AutoConfigureMockMvc
1531
public class UsersControllerTest {
1632

1733
@Autowired
1834
private MockMvc mockMvc;
1935

36+
@Autowired
37+
private Faker faker;
38+
39+
@Autowired
40+
private UserRepository userRepository;
41+
42+
@Autowired
43+
private ModelGenerator modelGenerator;
44+
45+
@Autowired
46+
private ObjectMapper om;
47+
48+
private JwtRequestPostProcessor token;
49+
50+
@BeforeEach
51+
public void setUp() {
52+
token = jwt().jwt(builder -> builder.subject("[email protected]"));
53+
var testUser = Instancio.of(modelGenerator.getUserModel())
54+
.create();
55+
userRepository.save(testUser);
56+
}
57+
2058
@Test
2159
public void testIndex() throws Exception {
2260
mockMvc.perform(get("/api/users").with(jwt()))
2361
.andExpect(status().isOk());
2462
}
63+
64+
@Test
65+
public void testUpdate() throws Exception {
66+
var user = Instancio.of(User.class)
67+
.ignore(Select.field(User::getId))
68+
.supply(Select.field(User::getEmail), () -> faker.internet().emailAddress())
69+
.create();
70+
userRepository.save(user);
71+
72+
var data = new HashMap<>();
73+
data.put("firstName", "Mike");
74+
75+
var request = put("/api/users/" + user.getId())
76+
.with(token)
77+
.contentType(MediaType.APPLICATION_JSON)
78+
.content(om.writeValueAsString(data));
79+
80+
mockMvc.perform(request)
81+
.andExpect(status().isOk());
82+
83+
user = userRepository.findById(user.getId()).get();
84+
assertThat(user.getFirstName()).isEqualTo(("Mike"));
85+
}
2586
}

src/test/java/io/hexlet/blog/util/ModelGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ private void init() {
3131

3232
userModel = Instancio.of(User.class)
3333
.ignore(Select.field(Post::getId))
34-
.supply(Select.field(Post::getName), () -> faker.gameOfThrones().house())
35-
.supply(Select.field(Post::getBody), () -> faker.gameOfThrones().quote())
34+
.supply(Select.field(User::getEmail), () -> faker.internet().emailAddress())
3635
.toModel();
3736
}
3837
}

0 commit comments

Comments
 (0)