Skip to content

Commit

Permalink
chore: move api
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Oct 3, 2024
1 parent f7c32a0 commit bfa2156
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package fuck.manthe.nmsl.controller.admin;

import cn.hutool.crypto.SecureUtil;
import fuck.manthe.nmsl.entity.CrackedUser;
import fuck.manthe.nmsl.entity.RestBean;
import fuck.manthe.nmsl.entity.dto.AnalysisDTO;
import fuck.manthe.nmsl.entity.dto.CrackedUserDTO;
import fuck.manthe.nmsl.service.AnalysisService;
import fuck.manthe.nmsl.service.CrackedUserService;
import fuck.manthe.nmsl.utils.Const;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/admin")
@RestController
@Log4j2
public class AdminController {
@Resource
RedisTemplate<String, Long> redisTemplate;

@Resource
CrackedUserService crackedUserService;

Expand All @@ -41,51 +30,6 @@ public ResponseEntity<String> ping() {
return ResponseEntity.ok("Pong");
}

@PostMapping("addUser")
public ResponseEntity<RestBean<String>> addUser(@RequestParam String username, @RequestParam String password, @RequestParam int day) {
long expire = -1L;
log.info("User {} has added by an admin", username);
if (day != -1) {
expire = System.currentTimeMillis() + (long) day * 24 * 60 * 60 * 1000;
}
if (crackedUserService.addUser(CrackedUser.builder().password(SecureUtil.sha1(password)).username(username).expire(expire).build())) {
return ResponseEntity.ok(RestBean.success("OK"));
}
return new ResponseEntity<>(RestBean.failure(409, "Conflict"), HttpStatus.CONFLICT);
}

@PostMapping("colddown/reset")
public ResponseEntity<RestBean<String>> reset() {
log.info("Inject cold down was reset.");
redisTemplate.opsForValue().set(Const.COLD_DOWN, System.currentTimeMillis());
return ResponseEntity.ok(RestBean.success("Reset CD."));
}

@PostMapping("renew/{username}")
public ResponseEntity<RestBean<String>> renew(@PathVariable String username, @RequestParam int day) {
log.info("An admin renewed the expire date of user {} ({}d)", username, day);
if (crackedUserService.renewUser(username, day)) {
return ResponseEntity.ok(RestBean.success("OK"));
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@DeleteMapping("removeUser/{username}")
public RestBean<Object> removeUser(@PathVariable String username) {
log.info("An admin removed a user with name {}", username);
crackedUserService.removeUser(username);
return RestBean.success();
}

@PostMapping("password/{username}/reset")
public ResponseEntity<RestBean<String>> resetPassword(@PathVariable String username, @RequestParam String password) {
log.info("An admin reset the password of user {}", username);
if (crackedUserService.resetPassword(username, password)) {
return ResponseEntity.ok(RestBean.success());
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@GetMapping("analysis")
public AnalysisDTO analysis() {
return AnalysisDTO.builder()
Expand All @@ -96,17 +40,6 @@ public AnalysisDTO analysis() {
.build();
}

@GetMapping("listUsers")
public List<CrackedUserDTO> listUsers() {
return crackedUserService.list().stream().map((user) -> CrackedUserDTO.builder()
.id(user.getId())
.username(user.getUsername())
.expire(user.getExpire())
.totalLaunch(analysisService.getTotalLaunch(user.getUsername()))
.lastLaunch(analysisService.getLastLaunch(user.getUsername()))
.build()).toList();
}

@GetMapping("logSuper")
public String logSuper() {
// code is in Filter
Expand All @@ -120,11 +53,4 @@ public void autoJob() {
}
analysisService.reset();
}

@DeleteMapping("removeExpired")
@Transactional
public RestBean<String> removeExpired() {
crackedUserService.removeExpired();
return RestBean.success("Success");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fuck.manthe.nmsl.controller.admin;

import fuck.manthe.nmsl.entity.RestBean;
import fuck.manthe.nmsl.utils.Const;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("admin/colddown")
public class ColdDownAdminController {
@Resource
RedisTemplate<String, Long> redisTemplate;

@PostMapping("reset")
public ResponseEntity<RestBean<String>> reset() {
log.info("Global inject cold down was reset.");
// todo migrate to VapeAccountService
redisTemplate.opsForValue().set(Const.COLD_DOWN, System.currentTimeMillis());
return ResponseEntity.ok(RestBean.success("Reset CD."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package fuck.manthe.nmsl.controller.admin;

import cn.hutool.crypto.SecureUtil;
import fuck.manthe.nmsl.entity.CrackedUser;
import fuck.manthe.nmsl.entity.RestBean;
import fuck.manthe.nmsl.entity.dto.CrackedUserDTO;
import fuck.manthe.nmsl.service.AnalysisService;
import fuck.manthe.nmsl.service.CrackedUserService;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@RequestMapping("/admin/user")
public class UserManageController {
@Resource
CrackedUserService crackedUserService;

@Resource
AnalysisService analysisService;

@GetMapping("list")
public List<CrackedUserDTO> listUsers() {
return crackedUserService.list().stream().map((user) -> CrackedUserDTO.builder()
.id(user.getId())
.username(user.getUsername())
.expire(user.getExpire())
.totalLaunch(analysisService.getTotalLaunch(user.getUsername()))
.lastLaunch(analysisService.getLastLaunch(user.getUsername()))
.build()).toList();
}

@PostMapping("add")
public ResponseEntity<RestBean<String>> addUser(@RequestParam String username, @RequestParam String password, @RequestParam int day) {
long expire = -1L;
log.info("User {} has added by an admin", username);
if (day != -1) {
expire = System.currentTimeMillis() + (long) day * 24 * 60 * 60 * 1000;
}
if (crackedUserService.addUser(CrackedUser.builder().password(SecureUtil.sha1(password)).username(username).expire(expire).build())) {
return ResponseEntity.ok(RestBean.success("OK"));
}
return new ResponseEntity<>(RestBean.failure(409, "Conflict"), HttpStatus.CONFLICT);
}


@PostMapping("renew/{username}")
public ResponseEntity<RestBean<String>> renew(@PathVariable String username, @RequestParam int day) {
log.info("An admin renewed the expire date of user {} ({}d)", username, day);
if (crackedUserService.renewUser(username, day)) {
return ResponseEntity.ok(RestBean.success("OK"));
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@DeleteMapping("remove/{username}")
public RestBean<Object> removeUser(@PathVariable String username) {
log.info("An admin removed a user with name {}", username);
crackedUserService.removeUser(username);
return RestBean.success();
}

@PostMapping("password/{username}/reset")
public ResponseEntity<RestBean<String>> resetPassword(@PathVariable String username, @RequestParam String password) {
log.info("An admin reset the password of user {}", username);
if (crackedUserService.resetPassword(username, password)) {
return ResponseEntity.ok(RestBean.success());
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@DeleteMapping("removeExpired")
@Transactional
public RestBean<String> removeExpired() {
crackedUserService.removeExpired();
return RestBean.success("Success");
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/dashboard/add-user.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1>添加用户</h1>
return;
}

fetch(`/admin/addUser?username=${username}&password=${password}&day=${days}`, {
fetch(`/admin/user/add?username=${username}&password=${password}&day=${days}`, {
method: 'POST',
headers: {
'X-Admin-Password': adminPassword
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/templates/dashboard/user-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1>用户列表</h1>
const adminPassword = localStorage.getItem('adminPassword');

function fetchUserData() {
fetch('/admin/listUsers', {
fetch('/admin/user/list', {
headers: {
'X-Admin-Password': adminPassword
}
Expand Down Expand Up @@ -87,7 +87,7 @@ <h1>用户列表</h1>
function removeExpiredUsers() {
mdui.confirm('确定要清除所有过期用户吗?', '清除过期用户',
function () {
fetch('/admin/removeExpired', {
fetch('/admin/user/removeExpired', {
method: 'DELETE',
headers: {
'X-Admin-Password': adminPassword
Expand Down Expand Up @@ -122,7 +122,7 @@ <h1>用户列表</h1>
return
}
// 这里应该调用API来更新用户天数
fetch(`/admin/renew/${username}?day=${days}`, {
fetch(`/admin/user/renew/${username}?day=${days}`, {
method: 'POST',
headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
Expand Down Expand Up @@ -153,7 +153,7 @@ <h1>用户列表</h1>
function changePassword(username) {
mdui.prompt('请输入新密码:', '修改密码',
function (value) {
fetch(`/admin/password/${username}/reset?password=${value}`, {
fetch(`/admin/user/password/${username}/reset?password=${value}`, {
method: 'POST',
headers: {
'X-Admin-Password': localStorage.getItem('adminPassword')
Expand Down Expand Up @@ -184,7 +184,7 @@ <h1>用户列表</h1>
mdui.confirm('确定要删除用户 ' + username + ' 吗?', '删除用户',
function () {
// 这里应该调用API来删除用户
fetch(`/admin/removeUser/${username}`, {
fetch(`/admin/user/remove/${username}`, {
method: 'DELETE',
headers: {
'X-Admin-Password': localStorage.getItem('adminPassword')
Expand Down Expand Up @@ -217,8 +217,8 @@ <h1>用户列表</h1>
mdui.alert('请输入一个有效的正整数');
return;
}
fetch('/admin/listUsers', {

fetch('/admin/user/list', {
headers: {
'X-Admin-Password': adminPassword
}
Expand All @@ -227,8 +227,8 @@ <h1>用户列表</h1>
.then(users => {
const promises = users
.filter(user => user.expire !== -1)
.map(user =>
fetch(`/admin/renew/${user.username}?day=${days}`, {
.map(user =>
fetch(`/admin/user/renew/${user.username}?day=${days}`, {
method: 'POST',
headers: {
'X-Admin-Password': adminPassword
Expand Down

0 comments on commit bfa2156

Please sign in to comment.