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
This commit is contained in:
Zhongheng Liu 2024-01-17 23:45:58 +02:00
commit 64b4b7e404
No known key found for this signature in database
3 changed files with 77 additions and 18 deletions

View file

@ -2,14 +2,20 @@ package me.imsonmia.epqapi.controller;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Optional;
import javax.swing.text.html.Option;
import org.springframework.beans.factory.annotation.Autowired; 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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import me.imsonmia.epqapi.model.Message; import me.imsonmia.epqapi.model.Message;
@ -24,32 +30,70 @@ public class UserController {
private UserRepository userRepository; private UserRepository userRepository;
@Autowired @Autowired
private MessageRepository messageRepository; private MessageRepository messageRepository;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable(value = "id") Long id) { // request URL like .../user?id={number} or .../user?name={string}
return userRepository.findById(id).get(); @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") @PostMapping("/user")
public User addUser( public ResponseEntity<Optional<User>> addUser(
@RequestBody @RequestBody User newUser) {
User newUser if (userRepository.existsByUserName(newUser.getUserName())) {
) { return ResponseEntity.badRequest().build();
return userRepository.save(newUser); }
userRepository.save(newUser);
return ResponseEntity.ok().build();
} }
@DeleteMapping("/user/{id}") @DeleteMapping("/user/{id}")
public void deleteUser( public boolean deleteUser(
@PathVariable(value = "id") Long id @PathVariable(value = "id") Long id) {
) { // user doesn't exist
if (!userRepository.existsById(id)) {
return false;
}
userRepository.deleteById(id); userRepository.deleteById(id);
return true;
} }
@GetMapping("/msg/{from}") @GetMapping("/msg/{from}")
public ArrayList<Message> getMessagesFromTimestamp(@PathVariable(value = "from") Long fromTimestamp) { public ArrayList<Message> getMessagesFromTimestamp(@PathVariable(value = "from") Long fromTimestamp) {
if (fromTimestamp < 0) {
return new ArrayList<>();
}
;
ArrayList<Message> messages = new ArrayList<>(); ArrayList<Message> messages = new ArrayList<>();
Instant targetInstant = Instant.ofEpochMilli(fromTimestamp); Instant targetInstant = Instant.ofEpochMilli(fromTimestamp);
for (Message msg : messageRepository.findAll()) { for (Message msg : messageRepository.findAll()) {
Instant t = Instant.ofEpochMilli(msg.getTimeMillis()); Instant t = Instant.ofEpochMilli(msg.getTimeMillis());
if (t.isBefore(targetInstant)) {continue;} if (t.isBefore(targetInstant)) {
continue;
}
messages.add(msg); messages.add(msg);
} }
return messages; 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;
}
} }

View file

@ -16,19 +16,29 @@ public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Getter
Long id; Long id;
@Getter @Getter
@Setter @Setter
String userName; String userName;
@Getter @Getter
@Setter @Setter
Date dateJoined; Date dateJoined;
@Getter
@Setter
Date lastSeen;
@Getter
@Setter
String passwordHash;
public User() { 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.id = id;
this.userName = userName; this.userName = userName;
this.dateJoined = dateJoined; this.dateJoined = dateJoined;
this.lastSeen = lastSeen;
this.passwordHash = passwordHash;
} }
} }

View file

@ -6,7 +6,12 @@ import me.imsonmia.epqapi.model.User;
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {
User findByUserName(String userName); User findByUserName(String userName);
boolean existsByUserName(String userName);
} }
// NOTE timestamp is a reserved SQL keyword // 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 user (date_joined datetime(6), id bigint not null, user_name
// create table message (id bigint not null, timestamp bigint, content varchar(255), from varchar(255), to varchar(255), primary key (id)) engine=InnoDB // 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