Skip to content

Commit

Permalink
Implemented deletion of exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Delannoy committed Nov 24, 2019
1 parent c58fffa commit 13bd974
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fr.kungfunantes.backend.model.criteria;

import io.swagger.annotations.ApiModel;
import fr.kungfunantes.backend.model.exercise.taolu.Taolu;
import fr.kungfunantes.backend.model.exercise.ExerciseCriteria;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.List;

@Entity
@ApiModel
Expand All @@ -15,6 +18,9 @@ public class Criteria {
private Long id;
private String name;

@OneToMany
List<ExerciseCriteria> exerciseCriteria;

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ public Category getCategory() {
public void setCategory(Category category) {
this.category = category;
}

public abstract String getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.kungfunantes.backend.model.exercise;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import io.swagger.annotations.ApiModel;

import fr.kungfunantes.backend.model.exercise.taolu.Taolu;
import fr.kungfunantes.backend.model.criteria.Criteria;

@Entity
@ApiModel
public class ExerciseCriteria {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@ManyToOne
@JoinColumn(name = "exerciseId")
Taolu taolu;

@ManyToOne
@JoinColumn(name = "criteriaId")
Criteria criteria;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.kungfunantes.backend.model.exercise;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import io.swagger.annotations.ApiModel;

import fr.kungfunantes.backend.model.exercise.fight.Fight;
import fr.kungfunantes.backend.model.round.Round;

@Entity
@ApiModel
public class ExerciseRound {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@ManyToOne
@JoinColumn(name = "exerciseId")
Fight fight;

@ManyToOne
@JoinColumn(name = "roundId")
Round round;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package fr.kungfunantes.backend.model.exercise.fight;

import fr.kungfunantes.backend.model.exercise.ExerciseRound;
import fr.kungfunantes.backend.model.exercise.Exercise;
import fr.kungfunantes.backend.model.round.Round;
import io.swagger.annotations.ApiModel;

import javax.persistence.*;
import java.util.Set;
import java.util.List;

@Entity
@ApiModel
@DiscriminatorValue(value = "FIGHT")
public class Fight extends Exercise {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "exercise_round",
joinColumns = @JoinColumn(name = "exerciseId"),
inverseJoinColumns = @JoinColumn(name = "roundId")
)
private Set<Round> rounds;
@OneToMany
List<ExerciseRound> exerciseRound;

public Set<Round> getRounds() {
return rounds;
}

public void setRounds(Set<Round> rounds) {
this.rounds = rounds;
public String getType() {
return "FIGHT";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public MeasurementUnit getMeasurementUnit() {
public void setMeasurementUnit(MeasurementUnit measurementUnit) {
this.measurementUnit = measurementUnit;
}

public String getType() {
return "PHYSICAL";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@

import fr.kungfunantes.backend.model.criteria.Criteria;
import fr.kungfunantes.backend.model.exercise.Exercise;
import fr.kungfunantes.backend.model.exercise.ExerciseCriteria;
import io.swagger.annotations.ApiModel;

import javax.persistence.*;
import java.util.Set;
import java.util.List;

@Entity
@ApiModel
@DiscriminatorValue(value = "TAOLU")
public class Taolu extends Exercise {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "exercise_criteria",
joinColumns = @JoinColumn(name = "exerciseId"),
inverseJoinColumns = @JoinColumn(name = "criteriaId")
)
private Set<Criteria> criterion;
@OneToMany
List<ExerciseCriteria> exerciseCriteria;

public Set<Criteria> getCriterion() {
return criterion;
}

public void setCriterion(Set<Criteria> criterion) {
this.criterion = criterion;
public String getType() {
return "TAOLU";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public String getQuestion() {
public void setQuestion(String question) {
this.question = question;
}

public String getType() {
return "THEORETICAL";
}
}
5 changes: 5 additions & 0 deletions src/main/java/fr/kungfunantes/backend/model/round/Round.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.kungfunantes.backend.model.round;

import fr.kungfunantes.backend.model.criteria.Criteria;
import fr.kungfunantes.backend.model.exercise.ExerciseRound;
import io.swagger.annotations.ApiModel;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

@Entity
Expand All @@ -13,6 +15,9 @@ public class Round {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany
List<ExerciseRound> exerciseRound;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "round_criteria",
joinColumns = @JoinColumn(name = "roundId"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ExerciseCriteriaRepository extends JpaRepository<Criteria, Long>{
public interface ExerciseCriteriaRepository extends JpaRepository<Exercise, Long>{
@Modifying
@Query("delete from Round where id = :idDelete")
@Query("delete from ExerciseCriteria where exerciseId = :idDelete")
public void deleteById(@Param("idDelete") Long idDelete);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ExerciseRoundRepository extends JpaRepository<Round, Long>{
public interface ExerciseRoundRepository extends JpaRepository<Exercise, Long>{
@Modifying
@Query("delete from Round where id = :idDelete")
@Query("delete from ExerciseRound where exerciseId = :idDelete")
public void deleteById(@Param("idDelete") Long idDelete);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public class ExerciseResource {
@Autowired
private ExerciseRoundRepository exerciseRoundRepository;

@ResponseBody
public List<Exercise> all() {
return exerciseRepository.findAll();
@GetMapping("/exercises")
@ResponseBody
public List<Exercise> all() {
return exerciseRepository.findAll();
}

@ResponseBody
Expand All @@ -41,9 +42,8 @@ public Optional<Exercise> byId(@RequestParam long id) {
@ResponseStatus(HttpStatus.OK)
@Transactional
public void delete(@PathVariable("id") Long id) {
Optional<Exercise> exercise = byId(id);
System.out.println(exercise);
/*switch(exerciseType) {
String exerciseType = byId(id).get().getType();
switch(exerciseType) {
case "TAOLU":
exerciseCriteriaRepository.deleteById(id);
exerciseRepository.deleteById(id);
Expand All @@ -54,7 +54,7 @@ public void delete(@PathVariable("id") Long id) {
break;
default:
exerciseRepository.deleteById(id);
}*/
}
}

}
22 changes: 11 additions & 11 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ INSERT INTO criteria (id, name) VALUES (2, 'critère 2');
INSERT INTO criteria (id, name) VALUES (3, 'critère 3');
INSERT INTO criteria (id, name) VALUES (4, 'critère 4');

INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (3, 1);
INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (3, 2);
INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (3, 3);
INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (3, 4);
INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (4, 1);
INSERT INTO exercise_criteria (exerciseId, criteriaId) VALUES (4, 2);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (3, 1);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (3, 2);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (3, 3);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (3, 4);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (4, 1);
INSERT INTO exerciseCriteria (exerciseId, criteriaId) VALUES (4, 2);

INSERT INTO round (id) VALUES (1);
INSERT INTO round (id) VALUES (2);
INSERT INTO round (id) VALUES (3);

INSERT INTO exercise_round (exerciseId, roundId) VALUES (5, 1);
INSERT INTO exercise_round (exerciseId, roundId) VALUES (5, 2);
INSERT INTO exercise_round (exerciseId, roundId) VALUES (5, 3);
INSERT INTO exercise_round (exerciseId, roundId) VALUES (6, 2);
INSERT INTO exercise_round (exerciseId, roundId) VALUES (6, 3);
INSERT INTO exerciseRound (exerciseId, roundId) VALUES (5, 1);
INSERT INTO exerciseRound (exerciseId, roundId) VALUES (5, 2);
INSERT INTO exerciseRound (exerciseId, roundId) VALUES (5, 3);
INSERT INTO exerciseRound (exerciseId, roundId) VALUES (6, 2);
INSERT INTO exerciseRound (exerciseId, roundId) VALUES (6, 3);

INSERT INTO round_criteria (roundId, criteriaId) VALUES (1, 1);
INSERT INTO round_criteria (roundId, criteriaId) VALUES (1, 2);
Expand Down

0 comments on commit 13bd974

Please sign in to comment.