blob: 1ddfc92e18a379950996b51ff06e8cf8eb7afe2a [file] [log] [blame]
22301102aa5adbc2025-05-18 17:51:55 +08001package com.pt.controller;
2
3import com.pt.constant.Constants;
4import com.pt.entity.Admin;
5import com.pt.repository.AdminRepository;
6import com.pt.service.AdminService;
7import com.pt.utils.JWTUtils;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.web.bind.annotation.CrossOrigin;
10import org.springframework.web.bind.annotation.PostMapping;
11import org.springframework.web.bind.annotation.RequestMapping;
12import org.springframework.web.bind.annotation.RestController;
13import org.springframework.web.bind.annotation.RequestParam;
14import org.springframework.http.ResponseEntity;
15
16import java.util.HashMap;
17import java.util.Map;
18
19@RestController
20@RequestMapping("/api/admin")
21@CrossOrigin(origins = "*")
22public 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");
34 ans.put("token", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, (int)1.2e8));
35 return ResponseEntity.ok().body(ans);
36 } else {
37 ans.put("result", "Invalid username or password");
38 return ResponseEntity.badRequest().body(ans);
39 }
40 }
41}