package com.hendyirawan.jws1033;

import com.google.common.collect.ImmutableList;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Component
@Path("/countries")
public class CountriesController {

    private final List<Country> countries = new ArrayList<>();

    public CountriesController(){
        countries.add(new Country("IDN", "Indonesia"));
        countries.add(new Country("MYS", "Malaysia"));
        countries.add(new Country("SGP", "Singapore"));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Collection getAllCountries(@Context UriInfo uriInfo) {
        String query = uriInfo.getRequestUri().getQuery();
        System.out.println(query);
        final ImmutableList<Country> sortedCountries;
        // Get query parameter
        if ("sort=name,asc".equals(query)) {
            sortedCountries = ImmutableList.sortedCopyOf(
                    (a, b) -> b.getCode().compareTo(a.getCode()), this.countries);
        } else {
            // default sorting: name (ascending)
            sortedCountries = ImmutableList.sortedCopyOf(
                    (a, b) -> a.getCode().compareTo(b.getCode()), this.countries);
        }
        return sortedCountries;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/version") //http://localhost:8080/countries/version
    public String version() {
        return "Versi 0.1";
    }

    @GET
    @Produces("application/json")
    @Path("/{code}")
    public Country getBook(@PathParam("code") String code) {
        Optional<Country> country = countries.stream().filter(it -> code.equals(it.getCode())).findAny();
        if(country.isPresent()) {
            return country.get();
        }
        return null;
    }

    @POST
    @Produces("application/json")
    @Consumes("application/json")
    public Response addCountry(Country country) {
        countries.add(country);
        return Response.created(URI.create("/" + country.getCode())).build();
    }

    @PUT
    @Consumes("application/json")
    @Path("/{code}")
    public Response updateBook(@PathParam("code") String code, Country country) {
        Optional<Country> found = countries.stream().filter(it -> code.equals(it.getCode())).findAny();
        if(found.isPresent()) {
            found.get().setCode(country.getCode());
            found.get().setName(country.getName());
            System.out.println("FOUND");
        }else{
            countries.add(country);
            System.out.println("NOT FOUND");
        }
        return Response.noContent().build();
    }

    @DELETE
    @Path("/{code}")
    public Response deleteBook(@PathParam("code") String code) {
        Optional<Country> found = countries.stream().filter(it -> code.equals(it.getCode())).findAny();
        if(found.isPresent()) {
            countries.remove(found.get());
        }
        return Response.ok().build();
    }

}

@@ -128,125 +128,29 @@
 nt;%0A
-import org.springframework.web.bind.annotation.RequestMapping;%0A%0Aimport javax.ws.rs.*;%0Aimport javax.ws.rs.core.Context
+%0Aimport javax.ws.rs.*
 ;%0Aim
@@ -220,41 +220,8 @@
 se;%0A
-import javax.ws.rs.core.UriInfo;%0A
 impo
@@ -376,16 +376,92 @@
 tries%22)%0A
+@Consumes(MediaType.APPLICATION_JSON)%0A@Produces(MediaType.APPLICATION_JSON)%0A
 public c
@@ -777,205 +777,76 @@
     
-@Produces(MediaType.APPLICATION_JSON)%0A    public Collection getAllCountries(@Context UriInfo uriInfo) %7B%0A        String query = uriInfo.getRequestUri().getQuery();%0A        System.out.println(query);
+public Collection getAllCountries(@QueryParam(%22sort%22) String sort) %7B
 %0A   
@@ -944,19 +944,15 @@
 f (%22
-sort=
 name,
-a
+de
 sc%22.
@@ -958,21 +958,20 @@
 .equals(
-query
+sort
 )) %7B%0A   
@@ -1551,42 +1551,8 @@
 GET%0A
-    @Produces(%22application/json%22)%0A
     
@@ -1590,20 +1590,23 @@
 ntry get
-Book
+Country
 (@PathPa
@@ -1858,76 +1858,8 @@
 OST%0A
-    @Produces(%22application/json%22)%0A    @Consumes(%22application/json%22)%0A
     
@@ -2034,42 +2034,8 @@
 PUT%0A
-    @Consumes(%22application/json%22)%0A
     
@@ -2077,20 +2077,23 @@
 e update
-Book
+Country
 (@PathPa
@@ -2638,12 +2638,15 @@
 lete
-Book
+Country
 (@Pa

fix JAX-RS