Commits

Sebastian Sdorra committed c28807a

improve RepositoryResource

Comments (0)

Files changed (1)

scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryResource.java

 
 import javax.inject.Singleton;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.UriInfo;
 
 /**
  *
                                        "SONIA WebApplications"));
   }
 
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   *  Method description
+   *
+   *
+   *
+   * @param uriInfo
+   * @param repository
+   *
+   *  @return
+   */
+  @POST
+  @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+  public Response add(@Context UriInfo uriInfo, Repository repository)
+  {
+    repositoryStore.put(repository.getName(), repository);
+
+    return Response.created(
+        uriInfo.getAbsolutePath().resolve(
+          "repositories/".concat(repository.getName()))).build();
+  }
+
+  /**
+   *   Method description
+   *
+   *
+   *   @param name
+   *
+   *   @return
+   */
+  @DELETE
+  @Path("{name}")
+  public Response delete(@PathParam("name") String name)
+  {
+    Repository repository = repositoryStore.get(name);
+
+    if (repository == null)
+    {
+      throw new WebApplicationException(Status.NOT_FOUND);
+    }
+
+    repositoryStore.remove(name);
+
+    return Response.noContent().build();
+  }
+
+  /**
+   * Method description
+   *
+   *
+   *
+   *
+   * @param uriInfo
+   * @param name
+   * @param repository
+   *
+   * @return
+   */
+  @PUT
+  @Path("{name}")
+  @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+  public Response update(@Context UriInfo uriInfo,
+                         @PathParam("name") String name, Repository repository)
+  {
+    Repository updateRepository = repositoryStore.get(name);
+
+    if (updateRepository == null)
+    {
+      throw new WebApplicationException(Status.NOT_FOUND);
+    }
+
+    updateRepository.setName(name);
+    updateRepository.setDescription(repository.getDescription());
+    updateRepository.setContact(repository.getContact());
+
+    return Response.created(
+        uriInfo.getAbsolutePath().resolve(repository.getName())).build();
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**