io.curly.gathering.list.GatheringStorage.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.gathering.list.GatheringStorage.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.curly.gathering.list;

import java.util.List;
import java.util.Optional;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.ObservableResult;
import io.curly.commons.github.User;
import io.curly.gathering.GatheringListRepository;
import rx.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Circuit isolated storage access layers
 *
 * @author Joo Pedro Evangelista
 */
@Component
public class GatheringStorage {

    private final GatheringListRepository repository;

    @Autowired
    public GatheringStorage(GatheringListRepository repository) {

        this.repository = repository;
    }

    public void save(GatheringList gatheringList) {
        if (gatheringList != null) {
            repository.save(gatheringList);
        }

    }

    public void delete(GatheringList gatheringList) {
        if (gatheringList != null) {
            this.repository.delete(gatheringList);
        }
    }

    @HystrixCommand
    public Observable<List<GatheringList>> findUsersList(User principal) {
        return new ObservableResult<List<GatheringList>>() {
            @Override
            public List<GatheringList> invoke() {
                return repository.findAllByOwner(principal.getId());
            }
        };
    }

    @HystrixCommand
    public Observable<Optional<GatheringList>> findOne(String id, User principal) {
        return new ObservableResult<Optional<GatheringList>>() {
            @Override
            public Optional<GatheringList> invoke() {
                return Optional.ofNullable(repository.findByOwnerAndId(principal.getId(), id));
            }
        };
    }
}