package controllers;

import helper.JsonLinker;

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

import models.Entry;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;

public class Entries extends Controller {

    public static Result list() {
    	final List<Entry> entries = Entry.find.all();
        return ok(JsonLinker.from(entries).to());
    }
    
    @BodyParser.Of(BodyParser.Json.class)
    public static Result create() {
    	return Optional.ofNullable(request().body().asJson())
    			.map(node -> Json.fromJson(node, Entry.class))
    			.map(Entries::saveAndSetLocationHeader)
    			.map(entry -> created(JsonLinker.from(entry).to()))
    			.orElse(badRequest());
    }
    
    private static Entry saveAndSetLocationHeader(Entry entry){
    	entry.save();
    	response().setHeader(LOCATION, JsonLinker.getLocation(entry));
    	return entry;
    }
    
    public static Result show(Integer id) {
    	return Optional
    			.ofNullable(Entry.find.byId(id))
    			.map(entry -> ok(JsonLinker.from(entry).to()))
    			.orElse(notFound());
    }
    
    public static Result update(Integer id) {
        return ok("update id: " + id);
    }
    
    public static Result delete(Integer id) {
    	return Optional
    			.ofNullable(Entry.find.byId(id))
    			.map(Entries::deleteNoContent)
    			.orElse(notFound());
    }
    
    private static Status deleteNoContent(Entry entry){
    	entry.delete();
    	return noContent();
    }

}

@@ -1116,32 +1116,74 @@
 ());%0A    %7D%0A    %0A
+    @BodyParser.Of(BodyParser.Json.class)%0A
     public stati
@@ -1220,40 +1220,378 @@
     
-    return ok(%22update id: %22 + id
+%09return Optional.ofNullable(Entry.find.byId(id))%0A%09%09.map(Entries::update)%0A%09%09.orElse(notFound());%0A    %7D%0A    %0A    private static Status update(Entry entry)%7B%0A    %09final Entry updatedEntry = Json.fromJson(request().body().asJson(), Entry.class);%0A    %09entry.setMessage(updatedEntry.message);%0A    %09entry.setTitle(updatedEntry.title);%0A    %09entry.update();%0A    %09return noContent(
 );%0A 

PUT Request for editing entry