Last active
July 19, 2017 01:24
-
-
Save GabrielCzar/759690c51fa19386abfc69edc8e93208 to your computer and use it in GitHub Desktop.
Rest Controller Basic Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class CourseResource { | |
class Course { | |
private Long id; | |
private String name; | |
private String duration; | |
public Course(Long id, String name, String duration) { | |
this.id = id; | |
this.name = name; | |
this.duration = duration; | |
} | |
public Long getId() { | |
return id; | |
} | |
} | |
private Map<Long, Course> courses; | |
public CursoResource() { | |
courses = new HashMap<Integer, Course>(); | |
Course c1 = new Course(1, "Workshop Rest", "24hs"); | |
Course c2 = new Course(2, "Workshop Spring MVC", "24hs"); | |
Course c3 = new Course(3, "Desenvolvimento Web com JSF 2", "60hs"); | |
courses.put(1, c1); | |
courses.put(2, c2); | |
courses.put(3, c3); | |
} | |
@GetMapping("/courses") | |
public ResponseEntity<List<Course>> findAllCourses() { | |
return new ResponseEntity<List<Course>>(new ArrayList<Course>(courses.values()), HttpStatus.OK); | |
} | |
@GetMapping("/courses/{id}") | |
public ResponseEntity<Course> findOne(@PathVariable("id") Integer id) { | |
Course course = courses.get(id); | |
if (course == null) | |
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | |
return new ResponseEntity<Course>(course, HttpStatus.OK); | |
} | |
@PostMapping("/courses") | |
public ResponseEntity<?> create(@ModelAttribute("course") @Valid Course course) { | |
if (courses.containsKey(course.getId()) | |
return new ResponseEntity<>(HttpStatus.CONFLICT); | |
courses.put(course.getId(), course); | |
return new ResponseEntity<>(HttpStatus.CREATED); | |
} | |
@PutMapping("/courses/{id}") | |
public ResponseEntity<?> update(@PathVariable("id") Long id, Course course) { | |
if (!courses.containsKey(course.getId()) | |
return new ResponseEntity<>(HttpStatus.NOT_FOUND) | |
courses.put(course.getId(), course); | |
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | |
} | |
@DeleteMapping("/courses/{id}") | |
public ResponseEntity<?> delete(@PathVariable("id") int id) { | |
Course course = courses.remove(id); | |
if (course == null) | |
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | |
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment