22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
| 3 | import com.pt.constant.Constants; |
| 4 | import com.pt.entity.Admin; |
| 5 | import com.pt.repository.AdminRepository; |
| 6 | import com.pt.service.AdminService; |
| 7 | import com.pt.utils.JWTUtils; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.web.bind.annotation.CrossOrigin; |
| 10 | import org.springframework.web.bind.annotation.PostMapping; |
| 11 | import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | import org.springframework.web.bind.annotation.RestController; |
| 13 | import org.springframework.web.bind.annotation.RequestParam; |
| 14 | import org.springframework.http.ResponseEntity; |
| 15 | |
| 16 | import java.util.HashMap; |
| 17 | import java.util.Map; |
| 18 | |
| 19 | @RestController |
| 20 | @RequestMapping("/api/admin") |
| 21 | @CrossOrigin(origins = "*") |
| 22 | public class AdminController { |
| 23 | |
| 24 | @Autowired |
| 25 | private AdminService adminService; |
| 26 | |
| 27 | @PostMapping("/login") |
| 28 | public ResponseEntity<?> loginAdmin(@RequestParam("username") String username, |
| 29 | @RequestParam("password") String password) { |
| 30 | Map<String, Object> ans = new HashMap<>(); |
| 31 | Admin admin = adminService.findByUsernameAndPassword(username, password); |
| 32 | if (admin != null) { |
| 33 | ans.put("result", "Login successful"); |
22301102 | aadb0ac | 2025-06-05 18:02:21 +0800 | [diff] [blame^] | 34 | ans.put("data", Map.of( |
| 35 | "token", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, (int)1.2e8) |
| 36 | )); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 37 | return ResponseEntity.ok().body(ans); |
| 38 | } else { |
| 39 | ans.put("result", "Invalid username or password"); |
| 40 | return ResponseEntity.badRequest().body(ans); |
| 41 | } |
| 42 | } |
| 43 | } |