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 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; }