From e068e8c18e0f61292c8bb4326ebb9274ddd2c763 Mon Sep 17 00:00:00 2001 From: Laurent <58115082+naaturel@users.noreply.github.com> Date: Wed, 12 Mar 2025 14:18:01 +0100 Subject: [PATCH] Setup database schema --- back/build.gradle | 9 ++- .../configurations/AppConfigurations.java | 18 ++++++ .../letsmeet/configurations/AppSecurity.java | 58 +++++++++++++++++++ .../letsmeet/controllers/EventController.java | 28 +++++++++ .../letsmeet/entities/DateEntity.java | 28 +++++++++ .../letsmeet/entities/EventEntity.java | 20 +++++++ .../letsmeet/entities/ParticipantEntity.java | 19 ++++++ .../be/naaturel/letsmeet/models/Date.java | 7 +++ .../be/naaturel/letsmeet/models/Event.java | 12 ++++ .../letsmeet/repositories/EventRepo.java | 4 ++ .../src/main/resources/application.properties | 19 ++++++ .../letsmeet/LetsmeetApplicationTests.java | 4 +- 12 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 back/src/main/java/be/naaturel/letsmeet/configurations/AppConfigurations.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/configurations/AppSecurity.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/controllers/EventController.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/entities/DateEntity.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/entities/EventEntity.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/entities/ParticipantEntity.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/models/Date.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/models/Event.java create mode 100644 back/src/main/java/be/naaturel/letsmeet/repositories/EventRepo.java diff --git a/back/build.gradle b/back/build.gradle index e4fbcac..44b209d 100644 --- a/back/build.gradle +++ b/back/build.gradle @@ -18,7 +18,14 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'jakarta.validation:jakarta.validation-api:3.0.2' + implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final' + implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.28' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/back/src/main/java/be/naaturel/letsmeet/configurations/AppConfigurations.java b/back/src/main/java/be/naaturel/letsmeet/configurations/AppConfigurations.java new file mode 100644 index 0000000..bf4b03f --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/configurations/AppConfigurations.java @@ -0,0 +1,18 @@ +package be.naaturel.letsmeet.configurations; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class AppConfigurations { + + @Value("${sec.cors.authorizedHots}") + public String[] authorizedHosts; + + @Value("${sec.cors.authorizedMethods}") + public String[] authorizedMethods; + + @Value("${sec.cors.authorizedHeader}") + public String[] authorizedHeaders; + +} \ No newline at end of file diff --git a/back/src/main/java/be/naaturel/letsmeet/configurations/AppSecurity.java b/back/src/main/java/be/naaturel/letsmeet/configurations/AppSecurity.java new file mode 100644 index 0000000..7d25f09 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/configurations/AppSecurity.java @@ -0,0 +1,58 @@ +package be.naaturel.letsmeet.configurations; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.annotation.web.configurers.LogoutConfigurer; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; + +import java.util.Arrays; + +@Configuration +@EnableWebSecurity +public class AppSecurity { + + private final AppConfigurations conf; + + @Autowired + public AppSecurity(AppConfigurations appConf) { + this.conf = appConf; + } + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + + return http + .cors(cors -> {}) + .csrf(AbstractHttpConfigurer::disable) + .authorizeHttpRequests((requests) -> requests + .requestMatchers("/**" ).permitAll() + //.anyRequest().authenticated() + ) + .formLogin((form) -> form + .defaultSuccessUrl("/", true) + .permitAll() + ) + .logout(LogoutConfigurer::permitAll) + .build(); + } + + @Bean + public CorsFilter corsFilter() { + + CorsConfiguration config = new CorsConfiguration(); + config.setAllowedOrigins(Arrays.asList(conf.authorizedHosts)); + config.setAllowedMethods(Arrays.asList(conf.authorizedMethods)); + config.setAllowedHeaders(Arrays.asList(conf.authorizedHeaders)); + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", config); + return new CorsFilter(source); + } +} \ No newline at end of file diff --git a/back/src/main/java/be/naaturel/letsmeet/controllers/EventController.java b/back/src/main/java/be/naaturel/letsmeet/controllers/EventController.java new file mode 100644 index 0000000..5b7d799 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/controllers/EventController.java @@ -0,0 +1,28 @@ +package be.naaturel.letsmeet.controllers; + +import be.naaturel.letsmeet.models.Event; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class EventController { + + @Autowired + public EventController(){ + } + + @PostMapping({"/create", "/create/"}) + public ResponseEntity submit(@RequestBody Event e){ + return ResponseEntity.ok().build(); + } + + @GetMapping({"/join", "/join/"}) + public ResponseEntity leaderboard(){ + return ResponseEntity.ok().build(); + } + +} \ No newline at end of file diff --git a/back/src/main/java/be/naaturel/letsmeet/entities/DateEntity.java b/back/src/main/java/be/naaturel/letsmeet/entities/DateEntity.java new file mode 100644 index 0000000..a355d39 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/entities/DateEntity.java @@ -0,0 +1,28 @@ +package be.naaturel.letsmeet.entities; + +import jakarta.persistence.*; + +import java.util.Set; + +import static jakarta.persistence.CascadeType.ALL; + +@Entity(name = "Date") +public class DateEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + public String id; + + @Column + public String name; + + @Column + public long timeStamp; + + @ManyToOne + @JoinColumn(name="eventId", nullable=false) + public EventEntity event; + + @OneToMany(targetEntity= ParticipantEntity.class, cascade=ALL, mappedBy="id") + public Set participants; +} diff --git a/back/src/main/java/be/naaturel/letsmeet/entities/EventEntity.java b/back/src/main/java/be/naaturel/letsmeet/entities/EventEntity.java new file mode 100644 index 0000000..0e76aff --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/entities/EventEntity.java @@ -0,0 +1,20 @@ +package be.naaturel.letsmeet.entities; + +import jakarta.persistence.*; + +import java.util.Set; + +import static jakarta.persistence.CascadeType.ALL; + +@Entity(name = "Event") +public class EventEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + public String id; + + @Column + @OneToMany(targetEntity= DateEntity.class, cascade=ALL, mappedBy="event") + public Set timeStamps; + +} diff --git a/back/src/main/java/be/naaturel/letsmeet/entities/ParticipantEntity.java b/back/src/main/java/be/naaturel/letsmeet/entities/ParticipantEntity.java new file mode 100644 index 0000000..b304979 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/entities/ParticipantEntity.java @@ -0,0 +1,19 @@ +package be.naaturel.letsmeet.entities; + +import jakarta.persistence.*; + +@Entity(name = "Participant") +public class ParticipantEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + public String id; + + @Column + public String name; + + @ManyToOne + @JoinColumn(name="dateId", nullable=false) + public DateEntity date; + +} diff --git a/back/src/main/java/be/naaturel/letsmeet/models/Date.java b/back/src/main/java/be/naaturel/letsmeet/models/Date.java new file mode 100644 index 0000000..a90aae5 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/models/Date.java @@ -0,0 +1,7 @@ +package be.naaturel.letsmeet.models; + +public class Date { + + private long timestamp; + +} diff --git a/back/src/main/java/be/naaturel/letsmeet/models/Event.java b/back/src/main/java/be/naaturel/letsmeet/models/Event.java new file mode 100644 index 0000000..d1bdad1 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/models/Event.java @@ -0,0 +1,12 @@ +package be.naaturel.letsmeet.models; + +public class Event { + + private String name; + private long[] timestamp; + + public Event(String name, long[] timestamp){ + this.name = name; + this.timestamp = timestamp; + } +} diff --git a/back/src/main/java/be/naaturel/letsmeet/repositories/EventRepo.java b/back/src/main/java/be/naaturel/letsmeet/repositories/EventRepo.java new file mode 100644 index 0000000..566def5 --- /dev/null +++ b/back/src/main/java/be/naaturel/letsmeet/repositories/EventRepo.java @@ -0,0 +1,4 @@ +package be.naaturel.letsmeet.repositories; + +public class EventRepo { +} diff --git a/back/src/main/resources/application.properties b/back/src/main/resources/application.properties index bcd509a..bdd115c 100644 --- a/back/src/main/resources/application.properties +++ b/back/src/main/resources/application.properties @@ -1 +1,20 @@ +#=============MAIN============= spring.application.name=letsmeet + +#=============SERVER============= +server.port=5000 + +#=============SECURITY============= +sec.cors.authorizedHots=* +sec.cors.authorizedMethods=GET,POST,PUT,DELETE,OPTION +sec.cors.authorizedHeader=Authorization,Content-type + +#=============DATABASE============= +spring.datasource.url=jdbc:${DB_URL} +spring.datasource.username=${DB_USER} +spring.datasource.password=${DB_PASSWORD} + +spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver \ No newline at end of file diff --git a/back/src/test/java/be/naaturel/letsmeet/LetsmeetApplicationTests.java b/back/src/test/java/be/naaturel/letsmeet/LetsmeetApplicationTests.java index 93d8ecd..8973a25 100644 --- a/back/src/test/java/be/naaturel/letsmeet/LetsmeetApplicationTests.java +++ b/back/src/test/java/be/naaturel/letsmeet/LetsmeetApplicationTests.java @@ -6,8 +6,8 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class LetsmeetApplicationTests { - @Test + /*@Test void contextLoads() { - } + }*/ }