Compare commits

..

2 Commits

7 changed files with 47 additions and 33 deletions

View File

@ -0,0 +1,31 @@
package github.benjamin.equipreservebackend.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.List;
@Configuration
public class CorsConfig {
@Value("${equip-reserve.allowed-origins}")
private String allowedOrigins;
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(List.of(allowedOrigins));
config.setAllowCredentials(true);
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}

View File

@ -6,6 +6,7 @@ import github.benjamin.equipreservebackend.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
@ -24,7 +25,9 @@ public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
return http.csrf(AbstractHttpConfigurer::disable)
return http
.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login").permitAll()

View File

@ -1,25 +0,0 @@
package github.benjamin.equipreservebackend.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/device_image/**")
.addResourceLocations("file:" + System.getProperty("user.dir") + "/device_image/");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true);
}
}

View File

@ -5,7 +5,7 @@ import lombok.Getter;
@Getter
public enum DeviceReservationState {
FREE("可预约"),
FREE("空闲"),
RESERVED("有预约");
private final String label;

View File

@ -22,9 +22,10 @@ public class DeviceController {
@PreAuthorize("hasRole('USER')")
@GetMapping
public ResponseResult<Page<DeviceUserVO>> getDevices(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size) {
@RequestParam(defaultValue = "10") Integer size,
@RequestParam(required = false) String name) {
Page<Device> pageRequest = new Page<>(page, size);
Page<DeviceUserVO> res = deviceService.getDeviceVO(pageRequest);
Page<DeviceUserVO> res = deviceService.getDeviceVO(pageRequest, name);
return ResponseResult.success(res);
}

View File

@ -8,7 +8,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
public interface DeviceService {
Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest);
Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest, String name);
void addDevice(Device device);

View File

@ -38,10 +38,14 @@ public class DeviceServiceImpl implements DeviceService {
private final ReservationService reservationService;
@Override
public Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest) {
Page<Device> devices = deviceMapper.selectPage(pageRequest, new LambdaQueryWrapper<Device>()
public Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest, String name) {
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<Device>()
.eq(Device::getStatus, DeviceStatus.AVAILABLE)
.orderByAsc(Device::getName));
.orderByAsc(Device::getName);
if (StringUtils.hasText(name)) {
queryWrapper.like(Device::getName, name);
}
Page<Device> devices = deviceMapper.selectPage(pageRequest,queryWrapper);
List<Long> deviceIds = devices.getRecords().stream()
.map(Device::getId)
.toList();