Java tutorial
/* * Copyright (c) 2017 sainth (sainth@sainth.de) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package de.sainth.recipe.backend.db.repositories; import de.sainth.recipe.backend.db.generated.tables.records.FoodsRecord; import de.sainth.recipe.backend.rest.views.Food; import org.jooq.DSLContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import static de.sainth.recipe.backend.db.generated.tables.Foods.FOODS; import static org.jooq.impl.DSL.using; @Component public class FoodRepository { private final DSLContext create; @Autowired public FoodRepository(DSLContext create) { this.create = create; } public List<Food> findAll() { return create .selectFrom(FOODS).fetch().stream().map(record -> new Food(record.getId(), record.getName(), record.getPoints(), record.getPointsBaseAmount(), record.getBasicUnit())) .collect(Collectors.toList()); } public Food findOne(Long id) { return create.select(FOODS.ID, FOODS.NAME, FOODS.POINTS, FOODS.POINTS_BASE_AMOUNT, FOODS.BASIC_UNIT) .from(FOODS).where(FOODS.ID.eq(id)).fetchOneInto(Food.class); } public void delete(Long id) { create.delete(FOODS).where(FOODS.ID.eq(id)).execute(); } public void deleteAll() { create.deleteFrom(FOODS).execute(); } public Food save(Food food) { AtomicReference<Food> bu = new AtomicReference<>(); create.transaction(configuration -> { Long id = using(configuration).select(FOODS.ID).from(FOODS).where(FOODS.NAME.eq(food.getName())) .forUpdate().fetchOneInto(Long.class); FoodsRecord record; if (id == null) { record = using(configuration) .insertInto(FOODS, FOODS.NAME, FOODS.POINTS, FOODS.POINTS_BASE_AMOUNT, FOODS.BASIC_UNIT) .values(food.getName(), food.getPoints(), food.getPointsBaseAmount(), food.getBasicUnit()) .returning().fetchOne(); } else { record = using(configuration).update(FOODS).set(FOODS.NAME, food.getName()) .set(FOODS.POINTS, food.getPoints()) .set(FOODS.POINTS_BASE_AMOUNT, food.getPointsBaseAmount()) .set(FOODS.BASIC_UNIT, food.getBasicUnit()).where(FOODS.ID.eq(id)).returning().fetchOne(); } bu.set(new Food(record.getId(), record.getName(), record.getPoints(), record.getPointsBaseAmount(), record.getBasicUnit())); }); return bu.get(); } }