fixed user controller

This commit is contained in:
Tommaso Rodolfo Masera 2020-02-25 15:44:10 +01:00
parent e26f5976c6
commit 03fed60a34
2 changed files with 12 additions and 12 deletions

View file

@ -4,38 +4,37 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@EnableAutoConfiguration @EnableAutoConfiguration
@Controller @RequestMapping("/user")
public class UserController { public class UserController {
@Autowired private UserRepository userRepository; @Autowired private UserRepository userRepository;
@GetMapping @GetMapping
public List<User> getAll() { public Iterable<User> findAll() {
return userRepository.getList(); return userRepository.findAll();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public User getById(@PathVariable("id") long id) { public Optional<User> findById(@PathVariable("id") long id) {
return userRepository.getById(); return userRepository.findById(id);
} }
@PostMapping @PostMapping
public User create(@RequestBody User u) { public User save(@RequestBody User u) {
return userRepository.create(u); return userRepository.save(u);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public User update(@PathVariable("id") long id, @RequestBody User u) { public User update(@RequestBody User u) {
return userRepository.update(id, u); return userRepository.save(u);
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) { public void deleteById(@PathVariable("id") long id) {
userRepository.delete(id); userRepository.deleteById(id);
} }
} }

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.*;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {} public interface UserRepository extends CrudRepository<User, Long> {}