|
1 | 1 | package com.bnta.dark_heresy_character_sheet.controllers;
|
2 | 2 |
|
| 3 | +import com.bnta.dark_heresy_character_sheet.models.Dude; |
| 4 | +import com.bnta.dark_heresy_character_sheet.models.Weapon; |
| 5 | +import com.bnta.dark_heresy_character_sheet.repositories.DudeRepository; |
| 6 | +import com.bnta.dark_heresy_character_sheet.repositories.WeaponRepository; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.web.bind.annotation.*; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +@RestController |
| 16 | +@RequestMapping("weapons") |
3 | 17 | public class WeaponController {
|
| 18 | + |
| 19 | + @Autowired |
| 20 | + WeaponRepository weaponRepository; |
| 21 | + |
| 22 | + //INDEX |
| 23 | + @GetMapping //localhost:8080/weapons |
| 24 | + public ResponseEntity<List<Weapon>> getAllWeapons(){ |
| 25 | + return new ResponseEntity<>(weaponRepository.findAll(), HttpStatus.OK); |
| 26 | + } |
| 27 | + |
| 28 | + //SHOW |
| 29 | + @GetMapping(value = "/{id}") //localhost:8080/weapons/1 |
| 30 | + public ResponseEntity<Optional<Weapon>> getWeapon(@PathVariable Long id){ |
| 31 | + return new ResponseEntity<>(weaponRepository.findById(id), HttpStatus.OK); |
| 32 | + } |
| 33 | + |
| 34 | + //POST |
| 35 | + @PostMapping //POST localhost:8080/weapons |
| 36 | + public ResponseEntity<Weapon> createWeapon(@RequestBody Weapon newWeapon){ |
| 37 | + weaponRepository.save(newWeapon); |
| 38 | + return new ResponseEntity<>(newWeapon, HttpStatus.CREATED); |
| 39 | + } |
| 40 | + |
| 41 | + //DELETE |
| 42 | + @DeleteMapping(value = "/{id}") //localhost:8080/weapons/1 |
| 43 | + public ResponseEntity<Weapon> deleteWeapon(@PathVariable Long id){ |
| 44 | + weaponRepository.deleteById(id); |
| 45 | + return new ResponseEntity(id,HttpStatus.OK); |
| 46 | + } |
4 | 47 | }
|
0 commit comments