aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-01-17 23:45:58 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2024-01-17 23:45:58 +0200
commit64b4b7e40473b40db9847c78a255e32fd7cdc6e5 (patch)
tree96501a9b485b25be53e84b064ab297b785c3e94a
parent4a7f750226e7b99a75eb8af11add356da68ed956 (diff)
downloadepq-api-64b4b7e40473b40db9847c78a255e32fd7cdc6e5.tar.gz
epq-api-64b4b7e40473b40db9847c78a255e32fd7cdc6e5.tar.bz2
epq-api-64b4b7e40473b40db9847c78a255e32fd7cdc6e5.zip
Added UserRepository if exists by UserName handler
added corresponding typedefs to User entity class according to commit 5ce5f9 on epq-web added combined user getMapping for id and name-identified User getters
-rw-r--r--src/main/java/me/imsonmia/epqapi/controller/UserController.java68
-rw-r--r--src/main/java/me/imsonmia/epqapi/model/User.java18
-rw-r--r--src/main/java/me/imsonmia/epqapi/repository/UserRepository.java9
3 files changed, 77 insertions, 18 deletions
diff --git a/src/main/java/me/imsonmia/epqapi/controller/UserController.java b/src/main/java/me/imsonmia/epqapi/controller/UserController.java
index cd35735..ffdfdf8 100644
--- a/src/main/java/me/imsonmia/epqapi/controller/UserController.java
+++ b/src/main/java/me/imsonmia/epqapi/controller/UserController.java
@@ -2,14 +2,20 @@ 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;
@@ -24,32 +30,70 @@ public class UserController {
private UserRepository userRepository;
@Autowired
private MessageRepository messageRepository;
- @GetMapping("/user/{id}")
- public User getUserById(@PathVariable(value = "id") Long id) {
- return userRepository.findById(id).get();
+
+ // 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 User addUser(
- @RequestBody
- User newUser
- ) {
- return userRepository.save(newUser);
+ 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 void deleteUser(
- @PathVariable(value = "id") Long 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;}
+ 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;
+ }
}
diff --git a/src/main/java/me/imsonmia/epqapi/model/User.java b/src/main/java/me/imsonmia/epqapi/model/User.java
index aeee586..ee79cf3 100644
--- a/src/main/java/me/imsonmia/epqapi/model/User.java
+++ b/src/main/java/me/imsonmia/epqapi/model/User.java
@@ -16,19 +16,29 @@ public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
- Long id;
+ Long id;
@Getter
@Setter
- String userName;
+ String userName;
@Getter
@Setter
- Date dateJoined;
+ Date dateJoined;
+ @Getter
+ @Setter
+ Date lastSeen;
+ @Getter
+ @Setter
+ String passwordHash;
+
public User() {
}
- public User(Long id, String userName, Date dateJoined) {
+
+ public User(Long id, String userName, Date dateJoined, Date lastSeen, String passwordHash) {
this.id = id;
this.userName = userName;
this.dateJoined = dateJoined;
+ this.lastSeen = lastSeen;
+ this.passwordHash = passwordHash;
}
}
diff --git a/src/main/java/me/imsonmia/epqapi/repository/UserRepository.java b/src/main/java/me/imsonmia/epqapi/repository/UserRepository.java
index 9ee6779..296500b 100644
--- a/src/main/java/me/imsonmia/epqapi/repository/UserRepository.java
+++ b/src/main/java/me/imsonmia/epqapi/repository/UserRepository.java
@@ -6,7 +6,12 @@ import me.imsonmia.epqapi.model.User;
public interface UserRepository extends CrudRepository<User, Long> {
User findByUserName(String userName);
+
+ boolean existsByUserName(String userName);
}
// NOTE timestamp is a reserved SQL keyword
-// create table user (date_joined datetime(6), id bigint not null, user_name varchar(255), primary key (id)) engine=InnoDB
-// create table message (id bigint not null, timestamp bigint, content varchar(255), from varchar(255), to varchar(255), primary key (id)) engine=InnoDB \ No newline at end of file
+// create table user (date_joined datetime(6), id bigint not null, user_name
+// varchar(255), primary key (id)) engine=InnoDB
+// create table message (id bigint not null, timestamp bigint, content
+// varchar(255), from varchar(255), to varchar(255), primary key (id))
+// engine=InnoDB \ No newline at end of file