I don't understand why my weight record Repository is not called in spite of my code. I have written some logs before and after the weight record repository call. In my console, I can see that logs are called before and after, but not my repository. I have a code 200 OK, but in my database, my data is always here. I don't understand why. My Tomcat port is set to port 7777. I can create and read data but not delete it.
Just below my code : ENTITY, CONTROLLER, REPOSITORY and SERVICE.
https://gitlab.com/genetquentin/openweighttracker_backend/-/tree/develop
/*ENTITY 'PERSON'*/
@Entity
@Table(name = "person")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_person")
private Long idPerson;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id_initial_data")
private InitialData userInitData;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id_user")
private AppUser appUserPerson;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
private List<WeightRecord> weightsList = new ArrayList<WeightRecord>();
/* Getters and setters */
/*ENTITY 'WEIGHTRECORD'*/
@Entity
@Table(name = "weight_record")
public class WeightRecord implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_weight_record")
private Long idWeightRecord;
@Column(name = "weight_record_date", nullable = true)
private LocalDate weightRecordDate;
//JsonIgnore annotation because circular problem with Jackson and entity.
@JsonIgnore
@ManyToOne
private Person person;
@Min(1)
@Max(635)
@Column(name = "weight_kg_record", nullable = true, precision = 1)
private Double weightKgRecord;
@Min(1)
@Max(99)
@Column(name = "percent_fat_mass", nullable = true, precision = 1)
private Double percentFatMass;
@Min(1)
@Max(99)
@Column(name = "percent_muscular_mass", nullable = true, precision = 1)
private Double percentMuscularMass;
@Min(1)
@Max(99)
@Column(name = "percent_body_water", nullable = true, precision = 1)
private Double percentBodyWater;
@Min(1)
@Max(99)
@Column(name = "percent_bone_mass", nullable = true, precision = 1)
private Double percentBoneMass
/* Getters and setters */
/*CONTROLLER*/
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/weights")
public class WeightRecordController {
@Autowired
WeightRecordServiceImpl weightRecordServiceImpl;
@Autowired
WeightRecordRepository weightRecordRepository;
@Autowired
AppUserRepository appUserRepository;
@Autowired
PersonRepository personRepository;
private final Logger logger = LoggerFactory.getLogger(WeightRecordController.class);
//TODO : Weight is not deleted and repository not called ?
// USE deleteinbatch method repository from jpa ?
@DeleteMapping("/{weightId}")
public ResponseEntity<WeightRecord> deleteWeigthById(@PathVariable("weightId") Long weightId, Principal principal) {
logger.info("DELETE /weights/{}", weightId);
try {
Long appUserConnectedId = this.getAppUserConnectedId(principal);
Person personConnected = personRepository.findById(appUserConnectedId).orElseThrow();
WeightRecord weightRecordToDelete = weightRecordRepository.findById(weightId).orElseThrow();
if(personConnected != null && personConnected.getWeightsList().contains(weightRecordToDelete)) {
logger.info("SERVICE FOR DELETING");
logger.info(weightRecordToDelete.getWeightKgRecord().toString());
return ResponseEntity.ok(weightRecordServiceImpl.deleteWeightById(weightId));
} else {
logger.info("BAD USER FOR BAD WEIGHT");
return ResponseEntity.notFound().build();
}
} catch (NoSuchElementException nse) {
return ResponseEntity.notFound().build();
}
}
}
/*REPOSITORY*/
@Repository
public interface WeightRecordRepository extends JpaRepository<WeightRecord, Long> {
}
/*SERVICE*/
@Service
public class WeightRecordServiceImpl implements WeightRecordService {
@Autowired
WeightRecordRepository weightRecordRepository;
@Autowired
AppUserRepository appUserRepository;
@Autowired
PersonRepository personRepository;
private final Logger logger = LoggerFactory.getLogger(WeightRecordServiceImpl.class);
public WeightRecord deleteWeightById(Long weightRecordToDeleteId) {
logger.info("THIS IS THE WEIGHT ID TO DELETE : {}", weightRecordToDeleteId);
WeightRecord weightRecordToDelete = weightRecordRepository.findById(weightRecordToDeleteId).orElseThrow();
weightRecordRepository.deleteById(weightRecordToDeleteId);
logger.info("Weight with id n°{} is deleted now", weightRecordToDelete.getIdWeightRecord());
return weightRecordToDelete;
}