Skip to content

Instantly share code, notes, and snippets.

@nazartm
Created March 18, 2013 18:17
Show Gist options
  • Save nazartm/5189451 to your computer and use it in GitHub Desktop.
Save nazartm/5189451 to your computer and use it in GitHub Desktop.
@Named
@Scope("request")
public class BlogServiceImpl implements BlogRestfulService {
@Inject
private BlogService service;
@Inject
private Mapper mapper;
@Override
public List<Post> getPosts() {
List<MyPost> posts = service.getPosts();
List<Post> dtoPosts = new ArrayList<Post>(posts.size());
for (MyPost myPost : posts) {
dtoPosts.add(mapper.map(myPost, Post.class));
}
return dtoPosts;
}
@Override
public Post getPost(int id) {
MyPost post = service.getPost(id);
if (post == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return mapper.map(post, Post.class);
}
@Override
public Response addPost(Post post) {
int id = service.addPost(mapper.map(post, MyPost.class));
return Response.created(UriBuilder.fromPath(Integer.toString(id)).build()).build();
}
@Override
public Response updatePost(int id, Post post) {
boolean success = service.updatePost(id, mapper.map(post, MyPost.class));
if (!success) {
throw new WebApplicationException(Response.Status.NOT_MODIFIED);
}
return Response.ok().build();
}
@Override
public Response deletePost(int id) {
boolean success = service.deletePost(id);
if (!success) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return Response.ok().build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment