package fi.github.marsojm.runnersnotes.core.interactors;

import fi.github.marsojm.runnersnotes.boundary.*;
import fi.github.marsojm.runnersnotes.core.entities.Note;
import fi.github.marsojm.runnersnotes.gateway.boundaries.InvalidIdException;
import fi.github.marsojm.runnersnotes.gateway.boundaries.NoteGateway;

import java.util.List;

/**
 * Created by Marko on 7.9.2015.
 */
public class NotesInteractor implements NoteBoundary {

    private NoteGateway<NoteData> gateway;

    public NotesInteractor(NoteGateway<NoteData> gateway) {
        this.gateway = gateway;
    }

    @Override
    public NoteData getNote(GetNoteRequest request) {
        return gateway.getNote(request.getId());
    }

    @Override
    public int createNote(CreateNoteRequest request) {
        Note note = new Note(request.getCreated(), request.getDistance(), request.getDuration(), request.getComments());
        List<String> errors = note.validate();
        int id = 0;
        if (errors.isEmpty()) {
            try {
                id = gateway.listNotes()
                        .stream()
                        .map(n -> n.getId())
                        .reduce(1, (a, b) -> {if (a < b) return b; else return a; } ) + 1;

                gateway.createNote(id, new NoteData(id, note.getCreated(), note.getDistance(), note.getDuration(), note.getComments()));
            } catch (InvalidIdException e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    @Override
    public List<NoteData> getNoteList(GetNoteListRequest request) {
        return gateway.listNotes();
    }
}

@@ -1028,197 +1028,19 @@
  = g
-ateway.listNotes()%0A                        .stream()%0A                        .map(n -%3E n.getId())%0A                        .reduce(1, (a, b) -%3E %7Bif (a %3C b) return b; else return a; %7D ) + 1;%0A
+etNextId();
 %0A   
@@ -1428,11 +1428,230 @@
 );%0A    %7D
+%0A%0A    private int getNextId() %7B%0A        return gateway.listNotes()%0A                .stream()%0A                .map(n -%3E n.getId())%0A                .reduce(0, (a, b) -%3E %7B if (a %3C b) return b; else return a; %7D ) + 1;%0A    %7D
 %0A%7D%0A

Add separate for getting next id