数据库链接测试

Change-Id: If3262e1fe7b9202521a1a26444501628f8f5b3b0
diff --git a/src/main/java/com/pt/BackendApplication.java b/src/main/java/com/pt/BackendApplication.java
new file mode 100644
index 0000000..28bc5cb
--- /dev/null
+++ b/src/main/java/com/pt/BackendApplication.java
@@ -0,0 +1,13 @@
+package com.pt;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class BackendApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(BackendApplication.class, args);
+    }
+
+}
diff --git a/src/main/java/com/pt/controller/UserController.java b/src/main/java/com/pt/controller/UserController.java
new file mode 100644
index 0000000..4ced292
--- /dev/null
+++ b/src/main/java/com/pt/controller/UserController.java
@@ -0,0 +1,86 @@
+package com.pt.controller;
+
+import com.pt.entity.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import com.pt.service.UserService;
+
+@RestController
+@RequestMapping("/api/user")
+@CrossOrigin(origins = "*")
+public class UserController {
+
+    @Autowired
+    private UserService userService;
+
+    @PostMapping("/register")
+    public ResponseEntity<?> registerUser(@RequestParam("username") String username,
+                                          @RequestParam("password") String password,
+                                          @RequestParam("email") String email) {
+        User user = userService.findByUsername(username);
+        if (user != null) {
+            return ResponseEntity.badRequest().body("User already exists");
+        } else {
+            User newUser = new User();
+
+            String uid = String.valueOf(System.currentTimeMillis());
+            newUser.setUid(uid);
+            newUser.setUsername(username);
+            newUser.setPassword(password);
+            newUser.setEmail(email);
+            userService.save(newUser);
+            return ResponseEntity.ok("User registered successfully");
+        }
+    }
+
+    @PostMapping("/login")
+    public ResponseEntity<?> loginUser(@RequestParam("username") String username,
+                                       @RequestParam("password") String password) {
+        User user = userService.findByUsernameAndPassword(username, password);
+        if (user != null) {
+            return ResponseEntity.ok("Login successful");
+        } else {
+            return ResponseEntity.badRequest().body("Invalid username or password");
+        }
+    }
+
+    @PostMapping("/update/username")
+    public ResponseEntity<?> updateUsername(@RequestParam("username") String oldUsername,
+                                            @RequestParam("newUsername") String newUsername) {
+        User user = userService.findByUsername(oldUsername);
+        if (user != null) {
+            user.setUsername(newUsername);
+            userService.save(user);
+            return ResponseEntity.ok("Username updated successfully");
+        } else {
+            return ResponseEntity.badRequest().body("User not found");
+        }
+    }
+
+    @PostMapping("/update/password")
+    public ResponseEntity<?> updatePassword(@RequestParam("username") String username,
+                                            @RequestParam("newPassword") String newPassword) {
+        User user = userService.findByUsername(username);
+        if (user != null) {
+            user.setPassword(newPassword);
+            userService.save(user);
+            return ResponseEntity.ok("Password updated successfully");
+        } else {
+            return ResponseEntity.badRequest().body("Invalid username or password");
+        }
+    }
+
+    @PostMapping("/update/email")
+    public ResponseEntity<?> updateEmail(@RequestParam("username") String username,
+                                         @RequestParam("newEmail") String newEmail) {
+        User user = userService.findByUsername(username);
+        if (user != null) {
+            user.setEmail(newEmail);
+            userService.save(user);
+            return ResponseEntity.ok("Email updated successfully");
+        } else {
+            return ResponseEntity.badRequest().body("User not found");
+        }
+    }
+}
diff --git a/src/main/java/com/pt/entity/User.java b/src/main/java/com/pt/entity/User.java
new file mode 100644
index 0000000..a2f462d
--- /dev/null
+++ b/src/main/java/com/pt/entity/User.java
@@ -0,0 +1,63 @@
+package com.pt.entity;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.Id;
+
+@Entity
+public class User {
+
+    @Id
+    private String uid;
+
+    private String username;
+    private String password;
+    private String email;
+    private int level;
+    private int points;
+
+    public User() {
+    }
+    public User(String uid, String username, String password, String email, int level) {
+        this.uid = uid;
+        this.username = username;
+        this.password = password;
+        this.email = email;
+        this.level = level;
+    }
+    public String getUid() {
+        return uid;
+    }
+    public void setUid(String uid) {
+        this.uid = uid;
+    }
+    public String getUsername() {
+        return username;
+    }
+    public void setUsername(String username) {
+        this.username = username;
+    }
+    public String getPassword() {
+        return password;
+    }
+    public void setPassword(String password) {
+        this.password = password;
+    }
+    public String getEmail() {
+        return email;
+    }
+    public void setEmail(String email) {
+        this.email = email;
+    }
+    public int getLevel() {
+        return level;
+    }
+    public void setLevel(int level) {
+        this.level = level;
+    }
+    public int getPoints() {
+        return points;
+    }
+    public void setPoints(int points) {
+        this.points = points;
+    }
+}
diff --git a/src/main/java/com/pt/repository/UserRepository.java b/src/main/java/com/pt/repository/UserRepository.java
new file mode 100644
index 0000000..480d070
--- /dev/null
+++ b/src/main/java/com/pt/repository/UserRepository.java
@@ -0,0 +1,14 @@
+package com.pt.repository;
+
+import com.pt.entity.User;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+public interface UserRepository extends JpaRepository<User, String> {
+
+    User findByUsername(String username);
+
+    User findByEmail(String email);
+    User findByUsernameAndPassword(String username, String password);
+    User findByEmailAndPassword(String email, String password);
+
+}
diff --git a/src/main/java/com/pt/service/UserService.java b/src/main/java/com/pt/service/UserService.java
new file mode 100644
index 0000000..7025e5a
--- /dev/null
+++ b/src/main/java/com/pt/service/UserService.java
@@ -0,0 +1,45 @@
+package com.pt.service;
+
+import com.pt.entity.User;
+import com.pt.repository.UserRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserService {
+
+    @Autowired
+    private UserRepository userRepository;
+
+    public UserService(UserRepository userRepository) {
+        this.userRepository = userRepository;
+    }
+
+    public User findByUsername(String username) {
+        return userRepository.findByUsername(username);
+    }
+
+    public User findByEmail(String email) {
+        return userRepository.findByEmail(email);
+    }
+
+    public User findByUsernameAndPassword(String username, String password) {
+        return userRepository.findByUsernameAndPassword(username, password);
+    }
+
+    public User findByEmailAndPassword(String email, String password) {
+        return userRepository.findByEmailAndPassword(email, password);
+    }
+
+    public User save(User user) {
+        return userRepository.save(user);
+    }
+
+    public User findById(String uid) {
+        return userRepository.findById(uid).orElse(null);
+    }
+
+    public void deleteById(String uid) {
+        userRepository.deleteById(uid);
+    }
+}