blob: ffdfdf8abdc6119bd982d572c614f8a1a59c69fb (
plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package me.imsonmia.epqapi.controller;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Optional;
import javax.swing.text.html.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import me.imsonmia.epqapi.model.Message;
import me.imsonmia.epqapi.model.User;
import me.imsonmia.epqapi.repository.MessageRepository;
import me.imsonmia.epqapi.repository.UserRepository;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private MessageRepository messageRepository;
// request URL like .../user?id={number} or .../user?name={string}
@GetMapping("/user")
public ResponseEntity<User> getUserByParam(@RequestParam(value = "id") Optional<Long> id,
@RequestParam(value = "name") Optional<String> name) {
if (!id.isPresent()) {
if (!name.isPresent()) {
// malformed request
return ResponseEntity.badRequest().build();
} else {
// Filter by name branch
return ResponseEntity.ok().body(userRepository.findByUserName(name.get()));
}
} else {
// get by id branch
return ResponseEntity.ok().body(userRepository.findById(id.get()).get());
}
}
@PostMapping("/user")
public ResponseEntity<Optional<User>> addUser(
@RequestBody User newUser) {
if (userRepository.existsByUserName(newUser.getUserName())) {
return ResponseEntity.badRequest().build();
}
userRepository.save(newUser);
return ResponseEntity.ok().build();
}
@DeleteMapping("/user/{id}")
public boolean deleteUser(
@PathVariable(value = "id") Long id) {
// user doesn't exist
if (!userRepository.existsById(id)) {
return false;
}
userRepository.deleteById(id);
return true;
}
@GetMapping("/msg/{from}")
public ArrayList<Message> getMessagesFromTimestamp(@PathVariable(value = "from") Long fromTimestamp) {
if (fromTimestamp < 0) {
return new ArrayList<>();
}
;
ArrayList<Message> messages = new ArrayList<>();
Instant targetInstant = Instant.ofEpochMilli(fromTimestamp);
for (Message msg : messageRepository.findAll()) {
Instant t = Instant.ofEpochMilli(msg.getTimeMillis());
if (t.isBefore(targetInstant)) {
continue;
}
messages.add(msg);
}
return messages;
}
@PatchMapping("/user/{id}")
boolean changeUserProperties(@PathVariable(value = "id") Long userId, @RequestBody User newUser) {
if (!userRepository.existsById(userId)) {
return false;
}
userRepository.save(newUser);
return true;
}
}
|