mirror of
https://gitea.gofwd.group/Forward_Group/ballistic-builder-spring.git
synced 2025-12-06 02:56:44 -05:00
lots of changes
This commit is contained in:
70
src/main/java/group/goforward/ballistic/ApiResponse.java
Normal file
70
src/main/java/group/goforward/ballistic/ApiResponse.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package group.goforward.ballistic;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class ApiResponse<T> {
|
||||
|
||||
private static final String API_SUCCESS = "success";
|
||||
private static final String API_FAILURE = "failure";
|
||||
private static final String API_ERROR = "error";
|
||||
private String[] messages;
|
||||
private T data;
|
||||
|
||||
|
||||
|
||||
private String status;
|
||||
private LocalDateTime timestamp;
|
||||
private ApiResponse(String status, String[] message, T data) {
|
||||
this.status = status;
|
||||
this.messages = message;
|
||||
this.data = data;
|
||||
this.timestamp = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data, String message) {
|
||||
String[] msg = {message};
|
||||
return new ApiResponse<>(API_SUCCESS, msg, data);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data) {
|
||||
String[] msg = {};
|
||||
return new ApiResponse<>(API_SUCCESS, msg, data);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> error(String[] messages) {
|
||||
return new ApiResponse<>(API_ERROR, messages, null);
|
||||
}
|
||||
public static <T> ApiResponse<T> error(String message) {
|
||||
String[] msg = {};
|
||||
return new ApiResponse<>(API_ERROR, msg, null);
|
||||
}
|
||||
|
||||
public String[] getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public void setMessages(String[] messages) {
|
||||
this.messages = messages;
|
||||
}
|
||||
|
||||
public T getData() {return data;}
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public LocalDateTime getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(LocalDateTime timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,15 @@ package group.goforward.ballistic;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EntityScan("group.goforward.ballistic.model")
|
||||
@ComponentScan("group.goforward.ballistic.configuration")
|
||||
@ComponentScan("group.goforward.ballistic.controllers")
|
||||
@ComponentScan("group.goforward.ballistic.service")
|
||||
@SpringBootApplication
|
||||
public class BallisticApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// src/main/java/com/example/config/CorsConfig.java
|
||||
package group.goforward.ballistic.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
|
||||
// Allow credentials
|
||||
config.setAllowCredentials(true);
|
||||
|
||||
// Allow Angular development server
|
||||
config.setAllowedOrigins(Arrays.asList(
|
||||
"http://localhost:4200",
|
||||
"http://localhost:4201",
|
||||
"http://localhost:8070",
|
||||
"https://localhost:8070",
|
||||
"http://192.168.11.210:8070",
|
||||
"https://192.168.11.210:8070",
|
||||
"http://localhost:4200",
|
||||
"http://citysites.gofwd.group",
|
||||
"https://citysites.gofwd.group",
|
||||
"http://citysites.gofwd.group:8070",
|
||||
"https://citysites.gofwd.group:8070"
|
||||
|
||||
));
|
||||
|
||||
// Allow all headers
|
||||
config.addAllowedHeader("*");
|
||||
|
||||
// Allow all HTTP methods
|
||||
config.setAllowedMethods(Arrays.asList(
|
||||
"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"
|
||||
));
|
||||
|
||||
// Expose headers
|
||||
config.setExposedHeaders(Arrays.asList(
|
||||
"Authorization",
|
||||
"Content-Type",
|
||||
"X-Total-Count"
|
||||
));
|
||||
|
||||
// Max age for preflight cache (1 hour)
|
||||
config.setMaxAge(3600L);
|
||||
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Alternative using WebMvcConfigurer:
|
||||
/*
|
||||
package group.goforward.citysites.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/api/**")
|
||||
.allowedOrigins("http://localhost:4200")
|
||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,10 @@
|
||||
package group.goforward.ballistic.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@Configuration
|
||||
|
||||
public class JpaConfig {
|
||||
// Enables @CreatedDate / @LastModifiedDate processing
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package group.goforward.ballistic.configuration;
|
||||
@@ -1,6 +1,6 @@
|
||||
package group.goforward.ballistic.controllers;
|
||||
|
||||
import group.goforward.ballistic.jpa.Psa;
|
||||
import group.goforward.ballistic.model.Psa;
|
||||
import group.goforward.ballistic.service.PsaService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -11,7 +11,7 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/psa")
|
||||
@RequestMapping("/api/psa")
|
||||
public class PsaController {
|
||||
|
||||
private final PsaService psaService;
|
||||
@@ -21,12 +21,12 @@ public class PsaController {
|
||||
this.psaService = psaService;
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@GetMapping("/api/getAllPsa")
|
||||
public List<Psa> getAllPsa() {
|
||||
return psaService.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@GetMapping("/api/getPSAById/{id}")
|
||||
public ResponseEntity<Psa> getPsaById(@PathVariable UUID id) {
|
||||
Optional<Psa> psa = psaService.findById(id);
|
||||
return psa.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||
@@ -37,7 +37,7 @@ public class PsaController {
|
||||
return psaService.save(psa);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@PutMapping("/api/updatePsa/{id}")
|
||||
public ResponseEntity<Psa> updatePsa(@PathVariable UUID id, @RequestBody Psa psaDetails) {
|
||||
Optional<Psa> psa = psaService.findById(id);
|
||||
if (psa.isPresent()) {
|
||||
@@ -49,7 +49,7 @@ public class PsaController {
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@DeleteMapping("/api/deletePsa/{id}")
|
||||
public ResponseEntity<Void> deletePsa(@PathVariable UUID id) {
|
||||
psaService.deleteById(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package group.goforward.ballistic.controllers;
|
||||
|
||||
import group.goforward.ballistic.ApiResponse;
|
||||
import group.goforward.ballistic.model.State;
|
||||
import group.goforward.ballistic.repos.StateRepository;
|
||||
import group.goforward.ballistic.service.StatesService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping()
|
||||
public class StateController {
|
||||
@Autowired
|
||||
private StateRepository repo;
|
||||
@Autowired
|
||||
private StatesService statesService;
|
||||
|
||||
@GetMapping("/api/getAllStates")
|
||||
public ResponseEntity<List<State>> getAllStates() {
|
||||
List<State> state = repo.findAll();
|
||||
return ResponseEntity.ok(state);
|
||||
}
|
||||
|
||||
@GetMapping("/api/getAllStatesTest")
|
||||
public ApiResponse<List<State>> getAllStatesTest() {
|
||||
List<State> state = repo.findAll();
|
||||
return ApiResponse.success(state);
|
||||
}
|
||||
|
||||
@GetMapping("/api/getAllStatesById/{id}")
|
||||
public ResponseEntity<State> getAllStatesById(@PathVariable Integer id) {
|
||||
return repo.findById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
@GetMapping("/api/getAllStatesByAbbreviation/{abbreviation}")
|
||||
public ResponseEntity<State> getAllStatesByAbbreviation(@PathVariable String abbreviation) {
|
||||
return repo.findByAbbreviation(abbreviation)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
@PostMapping("/api/addState")
|
||||
public ResponseEntity<State> createState(@RequestBody State item) {
|
||||
State created = statesService.save(item);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(created);
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/deleteState/{id}")
|
||||
public ResponseEntity<Void> deleteItem(@PathVariable Integer id) {
|
||||
return statesService.findById(id)
|
||||
.map(item -> {
|
||||
statesService.deleteById(id);
|
||||
return ResponseEntity.noContent().<Void>build();
|
||||
})
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Embeddable;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -6,8 +6,6 @@ import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "psa")
|
||||
public class Psa {
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package group.goforward.ballistic.jpa;
|
||||
package group.goforward.ballistic.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,5 +1,5 @@
|
||||
package group.goforward.ballistic.repos;
|
||||
import group.goforward.ballistic.jpa.Psa;
|
||||
import group.goforward.ballistic.model.Psa;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package group.goforward.ballistic.repos;
|
||||
|
||||
import group.goforward.ballistic.model.State;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface StateRepository extends JpaRepository<State, Integer> {
|
||||
Optional<State> findByAbbreviation(String abbreviation);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
package group.goforward.ballistic.service;
|
||||
import group.goforward.ballistic.jpa.Psa;
|
||||
import group.goforward.ballistic.model.Psa;
|
||||
import group.goforward.ballistic.repos.PsaRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package group.goforward.ballistic.service;
|
||||
|
||||
import group.goforward.ballistic.model.State;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface StatesService {
|
||||
|
||||
List<State> findAll();
|
||||
|
||||
Optional<State> findById(Integer id);
|
||||
|
||||
State save(State item);
|
||||
void deleteById(Integer id);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package group.goforward.ballistic.service.impl;
|
||||
|
||||
|
||||
import group.goforward.ballistic.model.State;
|
||||
import group.goforward.ballistic.repos.StateRepository;
|
||||
import group.goforward.ballistic.service.StatesService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class StatesServiceImpl implements StatesService {
|
||||
|
||||
@Autowired
|
||||
private StateRepository repo;
|
||||
|
||||
@Override
|
||||
public List<State> findAll() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<State> findById(Integer id) {
|
||||
return repo.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public State save(State item) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Integer id) {
|
||||
deleteById(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user