Java tutorial
/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.curly.gathering.list; import io.curly.commons.github.GitHubAuthentication; import io.curly.commons.github.User; import io.curly.commons.web.ModelErrors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.hateoas.ExposesResourceFor; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.async.DeferredResult; import javax.validation.Valid; import java.util.List; import java.util.concurrent.Callable; import static io.curly.commons.rx.RxResult.defer; import static java.util.stream.Collectors.toList; /** * Controller that handles api calls for GatheringList and actions for a certain GatheringList * * @author Joo Pedro Evangelista */ @RestController @ExposesResourceFor(GatheringList.class) @RequestMapping("/lists") public class GatheringController { private final GatheringStorage storage; @Autowired public GatheringController(GatheringStorage storage) { this.storage = storage; } @RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public Callable<HttpEntity<?>> createList(@Valid @RequestBody ListBody body, @GitHubAuthentication User user, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return () -> new ResponseEntity<>(new ModelErrors(bindingResult), HttpStatus.BAD_REQUEST); } else { return () -> { storage.save(new GatheringList(body.getName(), user.getId())); return new ResponseEntity<>(HttpStatus.CREATED); }; } } @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<HttpEntity<List<SimpleGatheringList>>> listNames(@GitHubAuthentication User user) { return defer(storage.findUsersList(user) .map(gatheringLists -> gatheringLists.stream().map(SimpleGatheringList::from).collect(toList())) .map(ResponseEntity::ok)); } @RequestMapping(value = "/{listId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<HttpEntity<GatheringList>> listOne(@PathVariable String listId, @GitHubAuthentication User user) { return defer(storage.findOne(listId, user) .map(res -> res.<ResourceNotFoundException>orElseThrow(ResourceNotFoundException::new)) .map(ResponseEntity::ok)); } @RequestMapping(value = "/{listId}", method = RequestMethod.DELETE) public DeferredResult<HttpEntity<?>> delete(@PathVariable String listId, @GitHubAuthentication User user) { return defer(storage.findOne(listId, user) .map(res -> res.<ResourceNotFoundException>orElseThrow(ResourceNotFoundException::new)) .doOnNext(storage::delete).map(gatheringList -> new ResponseEntity<>(HttpStatus.NO_CONTENT))); } }