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:
parent
4a7f750226
commit
64b4b7e404
3 changed files with 77 additions and 18 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
// 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
|
Loading…
Add table
Add a link
Reference in a new issue