de.sainth.recipe.backend.db.repositories.BasicUnitRepository.java Source code

Java tutorial

Introduction

Here is the source code for de.sainth.recipe.backend.db.repositories.BasicUnitRepository.java

Source

/*
 * 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.rest.views.BasicUnit;
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.BasicUnits.BASIC_UNITS;
import static org.jooq.impl.DSL.using;

@Component
public class BasicUnitRepository {
    private final DSLContext create;

    @Autowired
    public BasicUnitRepository(DSLContext create) {
        this.create = create;
    }

    public List<BasicUnit> findAll() {
        return create.selectFrom(BASIC_UNITS).fetch().stream()
                .map(record -> new BasicUnit(record.getShortname(), record.getLongname()))
                .collect(Collectors.toList());
    }

    public BasicUnit findOne(String shortname) {
        return create.select(BASIC_UNITS.SHORTNAME, BASIC_UNITS.LONGNAME).from(BASIC_UNITS)
                .where(BASIC_UNITS.SHORTNAME.eq(shortname)).fetchOneInto(BasicUnit.class);
    }

    public void delete(String shortname) {
        create.delete(BASIC_UNITS).where(BASIC_UNITS.SHORTNAME.eq(shortname)).execute();
    }

    public void deleteAll() {
        create.deleteFrom(BASIC_UNITS).execute();
    }

    public BasicUnit save(BasicUnit basicUnit) {
        AtomicReference<BasicUnit> bu = new AtomicReference<>();
        create.transaction(configuration -> {
            using(configuration).insertInto(BASIC_UNITS, BASIC_UNITS.SHORTNAME, BASIC_UNITS.LONGNAME)
                    .values(basicUnit.getShortname(), basicUnit.getLongname()).onConflict().doUpdate()
                    .set(BASIC_UNITS.LONGNAME, basicUnit.getLongname()).execute();
            bu.set(using(configuration).selectFrom(BASIC_UNITS)
                    .where(BASIC_UNITS.SHORTNAME.eq(basicUnit.getShortname())).fetchOneInto(BasicUnit.class));
        });

        return bu.get();
    }
}