This repository was archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.java
More file actions
72 lines (61 loc) · 3 KB
/
UserController.java
File metadata and controls
72 lines (61 loc) · 3 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
package pt.ua.deti.codespell.codespellbackend.controller;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 pt.ua.deti.codespell.codespellbackend.model.Achievement;
import pt.ua.deti.codespell.codespellbackend.model.Game;
import pt.ua.deti.codespell.codespellbackend.model.User;
import pt.ua.deti.codespell.codespellbackend.request.ChangeNameRequest;
import pt.ua.deti.codespell.codespellbackend.request.ChangePasswordRequest;
import pt.ua.deti.codespell.codespellbackend.request.MessageResponse;
import pt.ua.deti.codespell.codespellbackend.service.UserService;
@RestController
@RequestMapping("/api/user")
public class UserController {
private final PasswordEncoder passwordEncoder;
private final UserService userService;
@Autowired
public UserController(PasswordEncoder passwordEncoder, UserService userService) {
this.passwordEncoder = passwordEncoder;
this.userService = userService;
}
@GetMapping("/{username}/details")
public User getUserDetails(@PathVariable(value = "username") String username) {
return userService.findByUsername(username);
}
@GetMapping("/{username}/achievements")
public List<Achievement> getUserAchievements(@PathVariable(value = "username") String username) {
User user = getUserDetails(username);
List<Achievement> achievements = new ArrayList<>();
List<Game> games = user.getGames();
for (Game g : games) {
achievements.addAll(g.getAchievements());
}
return achievements;
}
@PutMapping("/{username}/password")
public MessageResponse changePassword(@PathVariable(value = "username") String username, @RequestBody ChangePasswordRequest changePasswordRequest) {
User user = getUserDetails(username);
String newPassword = changePasswordRequest.getPassword();
user.setPassword(passwordEncoder.encode(newPassword));
userService.updateUser(user);
return new MessageResponse(Date.from(Instant.now()), "Your password was successfully changed.");
}
@PutMapping("/{username}/name")
public MessageResponse changeName(@PathVariable(value = "username") String username, @RequestBody ChangeNameRequest changeNameRequest) {
User user = getUserDetails(username);
String newName = changeNameRequest.getName();
user.setName(newName);
userService.updateUser(user);
return new MessageResponse(Date.from(Instant.now()), "Your name was successfully changed.");
}
}