public TrainingModel getByName(String trainingName) { Training foundTraining = trainingDao.getByName(trainingName); if (foundTraining != null) { return getById(foundTraining.getId()); } return null; }
public ArrayList<TrainingModel> getAll() { ArrayList<TrainingModel> allTrainings = new ArrayList<TrainingModel>(); ArrayList<Training> trainings = trainingDao.getAll(); for (Training training : trainings) { allTrainings.add(getById(training.getId())); } return allTrainings; }
public TrainingModel updateExisting(TrainingModel model) { Training oldTraining = trainingDao.getById(model.getId()); Training existsWithSameName = trainingDao.getByName(model.getName()); if (oldTraining == null || existsWithSameName != null) { return null; } oldTraining.setDescription(model.getDescription()); Training saved = trainingDao.save(oldTraining); return saved != null ? model : null; }
public TrainingModel getById(int trainingId) { Training training = trainingDao.getById(trainingId); if (training == null) { return null; } TrainingModel model = new TrainingModel(); model.setId(training.getId()); model.setName(training.getName()); model.setDescription(training.getDescription()); model.setDateCreated(training.getDateCreated()); ArrayList<TrainingsExercises> trainingsExercises = trainingsExercisesDao.getAllForTraining(trainingId); for (TrainingsExercises trainingExercise : trainingsExercises) { ExerciseModel exerciseModel = new ExerciseModel(); exerciseModel.setId(trainingExercise.getId()); Exercise exercise = exerciseDao.getById(trainingExercise.getExerciseId()); exerciseModel.setName(exercise.getName()); exerciseModel.setDescription(exercise.getDescription()); exerciseModel.setImage(exercise.getImage()); ArrayList<ExerciseSets> exerciseSets = exerciseSetsDao.getAllForExercise(trainingExercise.getId()); for (ExerciseSets exerciseSet : exerciseSets) { ExerciseSetModel setModel = new ExerciseSetModel(); setModel.setId(exerciseSet.getId()); setModel.setNumberOfRepeats(exerciseSet.getNumberOfRepeats()); setModel.setOrder(exerciseSet.getOrder()); exerciseModel.addSet(setModel); } model.addExercise(exerciseModel); } return model; }
public TrainingModel createNew(TrainingModel model) { Training exists = trainingDao.getByName(model.getName()); if (exists != null) { return null; } Training training = new Training(); training.setName(model.getName()); training.setDescription(model.getDescription()); training.setDateCreated(model.getDateCreated()); Training savedTraining = trainingDao.save(training); if (savedTraining != null) { model.setId(savedTraining.getId()); return model; } return null; }