Compare commits
3 Commits
615322087e
...
95f4d10901
Author | SHA1 | Date | |
---|---|---|---|
95f4d10901 | |||
66f71e5641 | |||
2b05a1f1af |
@ -1,13 +1,13 @@
|
|||||||
package github.benjamin.equipreservebackend.controller;
|
package github.benjamin.equipreservebackend.controller;
|
||||||
|
|
||||||
import github.benjamin.equipreservebackend.mapper.TeamMapper;
|
import github.benjamin.equipreservebackend.dto.TeamDTO;
|
||||||
import github.benjamin.equipreservebackend.response.ResponseResult;
|
import github.benjamin.equipreservebackend.response.ResponseResult;
|
||||||
import github.benjamin.equipreservebackend.service.TeamService;
|
import github.benjamin.equipreservebackend.service.TeamService;
|
||||||
|
import github.benjamin.equipreservebackend.vo.TeamLabelVO;
|
||||||
import github.benjamin.equipreservebackend.vo.TeamVO;
|
import github.benjamin.equipreservebackend.vo.TeamVO;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -18,9 +18,35 @@ public class TeamController {
|
|||||||
|
|
||||||
private final TeamService teamService;
|
private final TeamService teamService;
|
||||||
|
|
||||||
|
@GetMapping("/team-label")
|
||||||
|
public ResponseResult<List<TeamLabelVO>> getTeamLabel() {
|
||||||
|
return ResponseResult.success(teamService.getTeamLabel());
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/teams")
|
@GetMapping("/teams")
|
||||||
public ResponseResult<List<TeamVO>> getTeams() {
|
public ResponseResult<List<TeamVO>> getTeams() {
|
||||||
return ResponseResult.success(teamService.getTeams());
|
return ResponseResult.success(teamService.getTeams());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@DeleteMapping("/team/{id}")
|
||||||
|
public ResponseResult<?> deleteTeam(@PathVariable("id") Long id) {
|
||||||
|
teamService.deleteTeam(id);
|
||||||
|
return ResponseResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@PostMapping("/team")
|
||||||
|
public ResponseResult<?> addTeam(@RequestBody TeamDTO dto) {
|
||||||
|
teamService.addTeam(dto.getName());
|
||||||
|
return ResponseResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@PutMapping("/team/{id}")
|
||||||
|
public ResponseResult<?> updateTeam(@PathVariable Long id,
|
||||||
|
@RequestBody TeamDTO dto) {
|
||||||
|
teamService.updateTeam(id, dto.getName());
|
||||||
|
return ResponseResult.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package github.benjamin.equipreservebackend.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class TeamDTO {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
@ -2,16 +2,20 @@ package github.benjamin.equipreservebackend.entity;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("teams")
|
@TableName("teams")
|
||||||
|
@NoArgsConstructor
|
||||||
public class Team {
|
public class Team {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private Long leaderId;
|
|
||||||
private LocalDateTime createdTime;
|
private LocalDateTime createdTime;
|
||||||
|
|
||||||
|
public Team(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,23 @@ package github.benjamin.equipreservebackend.exception;
|
|||||||
|
|
||||||
import github.benjamin.equipreservebackend.response.ResponseCode;
|
import github.benjamin.equipreservebackend.response.ResponseCode;
|
||||||
import github.benjamin.equipreservebackend.response.ResponseResult;
|
import github.benjamin.equipreservebackend.response.ResponseResult;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authorization.AuthorizationDeniedException;
|
import org.springframework.security.authorization.AuthorizationDeniedException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(ApiException.class)
|
@ExceptionHandler(ApiException.class)
|
||||||
public ResponseResult<?> handleApiException(ApiException e) {
|
public ResponseResult<?> handleApiException(ApiException e) {
|
||||||
|
log.error("[API异常] 错误代码: {}, 错误信息: {}", e.getCode(), e.getMessage(), e);
|
||||||
return ResponseResult.fail(e.getCode(), e.getMessage());
|
return ResponseResult.fail(e.getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,10 +28,20 @@ public class GlobalExceptionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseResult<?> handleException(Exception e) {
|
public ResponseResult<?> handleException(Exception e, HttpServletRequest request) {
|
||||||
// TODO 日志
|
log.error("[系统异常] 请求地址: {}, 方法: {}, 异常信息: {}, 请求参数: {}",
|
||||||
e.printStackTrace();
|
request.getRequestURI(),
|
||||||
|
request.getMethod(),
|
||||||
|
e.getMessage(),
|
||||||
|
getRequestParams(request),
|
||||||
|
e);
|
||||||
return ResponseResult.fail(ResponseCode.UNKNOWN_ERROR,"服务器内部异常,请联系开发人员");
|
return ResponseResult.fail(ResponseCode.UNKNOWN_ERROR,"服务器内部异常,请联系开发人员");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getRequestParams(HttpServletRequest request) {
|
||||||
|
Map<String, String[]> paramMap = request.getParameterMap();
|
||||||
|
return paramMap.entrySet().stream()
|
||||||
|
.map(entry -> entry.getKey() + "=" + Arrays.toString(entry.getValue()))
|
||||||
|
.collect(Collectors.joining(", ", "{", "}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,12 @@ package github.benjamin.equipreservebackend.mapper;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import github.benjamin.equipreservebackend.entity.Team;
|
import github.benjamin.equipreservebackend.entity.Team;
|
||||||
|
import github.benjamin.equipreservebackend.vo.TeamVO;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface TeamMapper extends BaseMapper<Team> {
|
public interface TeamMapper extends BaseMapper<Team> {
|
||||||
|
List<TeamVO> getTeams();
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
package github.benjamin.equipreservebackend.service;
|
package github.benjamin.equipreservebackend.service;
|
||||||
|
|
||||||
|
import github.benjamin.equipreservebackend.vo.TeamLabelVO;
|
||||||
import github.benjamin.equipreservebackend.vo.TeamVO;
|
import github.benjamin.equipreservebackend.vo.TeamVO;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface TeamService {
|
public interface TeamService {
|
||||||
|
List<TeamLabelVO> getTeamLabel();
|
||||||
|
|
||||||
List<TeamVO> getTeams();
|
List<TeamVO> getTeams();
|
||||||
|
|
||||||
|
void deleteTeam(Long id);
|
||||||
|
|
||||||
|
void addTeam(String name);
|
||||||
|
|
||||||
|
void updateTeam(Long id, String name);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package github.benjamin.equipreservebackend.service.impl;
|
package github.benjamin.equipreservebackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import github.benjamin.equipreservebackend.entity.Team;
|
import github.benjamin.equipreservebackend.entity.Team;
|
||||||
import github.benjamin.equipreservebackend.mapper.TeamMapper;
|
import github.benjamin.equipreservebackend.mapper.TeamMapper;
|
||||||
import github.benjamin.equipreservebackend.service.TeamService;
|
import github.benjamin.equipreservebackend.service.TeamService;
|
||||||
|
import github.benjamin.equipreservebackend.vo.TeamLabelVO;
|
||||||
import github.benjamin.equipreservebackend.vo.TeamVO;
|
import github.benjamin.equipreservebackend.vo.TeamVO;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -16,10 +18,32 @@ public class TeamServiceImpl implements TeamService {
|
|||||||
private final TeamMapper teamMapper;
|
private final TeamMapper teamMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TeamVO> getTeams() {
|
public List<TeamLabelVO> getTeamLabel() {
|
||||||
|
|
||||||
List<Team> teams = teamMapper.selectList(null);
|
List<Team> teams = teamMapper.selectList(null);
|
||||||
|
|
||||||
return teams.stream().map(TeamVO::new).toList();
|
return teams.stream().map(TeamLabelVO::new).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<TeamVO> getTeams() {
|
||||||
|
return teamMapper.getTeams();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteTeam(Long id) {
|
||||||
|
teamMapper.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addTeam(String name) {
|
||||||
|
teamMapper.insert(new Team(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTeam(Long id, String name) {
|
||||||
|
teamMapper.update(new LambdaUpdateWrapper<Team>()
|
||||||
|
.eq(Team::getId, id)
|
||||||
|
.set(Team::getName, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
package github.benjamin.equipreservebackend.vo;
|
||||||
|
|
||||||
|
import github.benjamin.equipreservebackend.entity.Team;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class TeamLabelVO {
|
||||||
|
|
||||||
|
private String label;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public TeamLabelVO(Team t) {
|
||||||
|
this.label = t.getName();
|
||||||
|
this.value = t.getId().toString();
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,15 @@
|
|||||||
package github.benjamin.equipreservebackend.vo;
|
package github.benjamin.equipreservebackend.vo;
|
||||||
|
|
||||||
import github.benjamin.equipreservebackend.entity.Team;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
|
||||||
public class TeamVO {
|
public class TeamVO {
|
||||||
|
|
||||||
private String label;
|
@JsonSerialize(using = ToStringSerializer.class)
|
||||||
private String value;
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private Integer size;
|
||||||
|
|
||||||
public TeamVO(Team t) {
|
|
||||||
this.label = t.getName();
|
|
||||||
this.value = t.getId().toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ spring:
|
|||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
username: your-username
|
username: your-username
|
||||||
password: your-password
|
password: your-password
|
||||||
url: jdbc:mysql://127.0.0.1:3306/equip_reserve?serverTimeZone=UTC
|
url: jdbc:mysql://127.0.0.1:3306/equip_reserve?serverTimeZone=Asia/Shanghai
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
max-file-size: 15MB
|
max-file-size: 15MB
|
||||||
|
21
src/main/resources/mapper/TeamMapper.xml
Normal file
21
src/main/resources/mapper/TeamMapper.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="github.benjamin.equipreservebackend.mapper.TeamMapper">
|
||||||
|
|
||||||
|
<select id="getTeams" resultType="github.benjamin.equipreservebackend.vo.TeamVO">
|
||||||
|
SELECT
|
||||||
|
t.id AS id,
|
||||||
|
t.name AS name,
|
||||||
|
COUNT(u.id) AS size
|
||||||
|
FROM
|
||||||
|
teams t
|
||||||
|
LEFT JOIN
|
||||||
|
users u ON t.id = u.team_id
|
||||||
|
GROUP BY
|
||||||
|
t.id, t.name
|
||||||
|
ORDER BY t.name
|
||||||
|
</select>
|
||||||
|
|
||||||
|
</mapper>
|
Loading…
x
Reference in New Issue
Block a user