mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 13:40:50 +02:00
Refactoring administration
to database-manager
. Now all elements from the database are defined here.
This commit is contained in:
parent
ce4e4f6743
commit
e1ea269782
17 changed files with 53 additions and 39 deletions
|
@ -5,7 +5,7 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>administration</artifactId>
|
<artifactId>database-manager</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -4,12 +4,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AdministrationService {
|
public class AdministrationService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ConnectionRepository connectionRepository;
|
private ConnectionRepository connectionRepository;
|
||||||
|
@Autowired
|
||||||
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
public boolean checkToken(int idUser, String token) {
|
public boolean checkToken(int idUser, String token) {
|
||||||
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
||||||
|
@ -20,4 +23,23 @@ public class AdministrationService {
|
||||||
Connection c = connections.getFirst();
|
Connection c = connections.getFirst();
|
||||||
return c.getToken().equals(token) && c.getExpiresAt().isAfter(java.time.LocalDateTime.now());
|
return c.getToken().equals(token) && c.getExpiresAt().isAfter(java.time.LocalDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean checkRole(int idUser, int role)
|
||||||
|
{
|
||||||
|
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
||||||
|
|
||||||
|
if (connections.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Connection c = connections.getFirst();
|
||||||
|
Optional<Role> roleOption = roleRepository.findById(role);
|
||||||
|
if(!roleOption.isPresent()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Role> getRoles() {
|
||||||
|
return roleRepository.findAll();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,11 +7,11 @@ import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "requests", schema = "service-architecture")
|
@Table(name = "requests", schema = "service-architecture")
|
||||||
public class Requests {
|
public class Request {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@ -24,7 +24,7 @@ public class Requests {
|
||||||
private int idUser;
|
private int idUser;
|
||||||
|
|
||||||
@Column(name = "created_at", nullable = false, columnDefinition = "DATE DEFAULT CURRENT_DATE")
|
@Column(name = "created_at", nullable = false, columnDefinition = "DATE DEFAULT CURRENT_DATE")
|
||||||
private LocalDate createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
@Column(name = "message", nullable = false)
|
@Column(name = "message", nullable = false)
|
||||||
private String message;
|
private String message;
|
||||||
|
@ -54,11 +54,11 @@ public class Requests {
|
||||||
this.idUser = idUser;
|
this.idUser = idUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalDate getCreatedAt() {
|
public LocalDateTime getCreatedAt() {
|
||||||
return createdAt;
|
return createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCreatedAt(LocalDate createdAt) {
|
public void setCreatedAt(LocalDateTime createdAt) {
|
||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@ package insa.application.helpapp.rest;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface RequestRepository extends JpaRepository<Requests, Integer> {
|
public interface RequestRepository extends JpaRepository<Request, Integer> {
|
||||||
|
|
||||||
}
|
}
|
|
@ -7,8 +7,8 @@
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<!-- Administation creates a dependency for other services -->
|
<!-- database-manager is a dependency for other services -->
|
||||||
<module>administration</module>
|
<module>database-manager</module>
|
||||||
<module>request-service</module>
|
<module>request-service</module>
|
||||||
<module>role-service</module>
|
<module>role-service</module>
|
||||||
<module>user-service</module>
|
<module>user-service</module>
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
<!-- Depends on Administation Service to check roles -->
|
<!-- Depends on Administation Service to check roles -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>insa.application.helpapp</groupId>
|
<groupId>insa.application.helpapp</groupId>
|
||||||
<artifactId>administration</artifactId>
|
<artifactId>database-manager</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -10,11 +10,15 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
public class RequestServiceApplication {
|
public class RequestServiceApplication {
|
||||||
@Autowired
|
@Autowired
|
||||||
private AdministrationService administrationService;
|
private AdministrationService administrationService;
|
||||||
|
@Autowired
|
||||||
|
private RequestRepository requestRepository;
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(RequestServiceApplication.class, args);
|
SpringApplication.run(RequestServiceApplication.class, args);
|
||||||
}
|
}
|
||||||
|
@ -32,13 +36,18 @@ public class RequestServiceApplication {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/post_message")
|
@PostMapping("/post_request")
|
||||||
public ResponseEntity<?> postMessage(@RequestParam int idUser,@RequestParam String token, @RequestParam String message) {
|
public ResponseEntity<?> postMessage(@RequestParam int idUser,@RequestParam String token, @RequestParam String message) {
|
||||||
if(!administrationService.checkToken(idUser, token)) {
|
if(!administrationService.checkToken(idUser, token)) {
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Request request = new Request();
|
||||||
return ResponseEntity.ok("Message posted");
|
// id_status = 1 means waiting. it is always set to 1 when created.
|
||||||
|
request.setIdStatus(1);
|
||||||
|
request.setIdUser(idUser);
|
||||||
|
request.setCreatedAt(LocalDateTime.now());
|
||||||
|
request.setMessage(message);
|
||||||
|
return ResponseEntity.ok(requestRepository.save(request));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,12 @@
|
||||||
<version>8.0.33</version>
|
<version>8.0.33</version>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Depends on Administation Service to check roles -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>insa.application.helpapp</groupId>
|
||||||
|
<artifactId>database-manager</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -15,7 +15,7 @@ import java.util.List;
|
||||||
public class RoleServiceApplication {
|
public class RoleServiceApplication {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RoleRepository roleRepository;
|
private AdministrationService administrationService;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(RoleServiceApplication.class, args);
|
SpringApplication.run(RoleServiceApplication.class, args);
|
||||||
|
@ -36,6 +36,6 @@ public class RoleServiceApplication {
|
||||||
|
|
||||||
@GetMapping("/get_roles")
|
@GetMapping("/get_roles")
|
||||||
public List<Role> getRoles() {
|
public List<Role> getRoles() {
|
||||||
return roleRepository.findAll();
|
return administrationService.getRoles();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
<!-- Depends on Administation Service to check roles -->
|
<!-- Depends on Administation Service to check roles -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>insa.application.helpapp</groupId>
|
<groupId>insa.application.helpapp</groupId>
|
||||||
<artifactId>administration</artifactId>
|
<artifactId>database-manager</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
package insa.application.helpapp.rest;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UserService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ConnectionRepository connectionRepository;
|
|
||||||
|
|
||||||
public boolean checkToken(int idUser, String token) {
|
|
||||||
List<Connection> connections = connectionRepository.findByIdUser(idUser);
|
|
||||||
|
|
||||||
if (connections.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Connection c = connections.getFirst();
|
|
||||||
return c.getToken().equals(token) && c.getExpiresAt().isAfter(java.time.LocalDateTime.now());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue