Added reading data from database using springboot

This commit is contained in:
grata
2023-04-27 09:55:31 +02:00
parent 6c7f9c6007
commit 1ea79fbda4
9 changed files with 112 additions and 36 deletions

View File

@@ -19,26 +19,26 @@ import java.util.List;
@RequestMapping(value = "api/progetto152", produces = MediaType.APPLICATION_JSON_VALUE)
public class Controller {
private static final String location = "location";
private static final String waypoint = "waypoint";
private static final String location1 = "location";
private static final String waypoint1 = "waypoint";
@Autowired
private UserService userService;
@GetMapping(value= location, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Location>> getLocations() {
List<Location> locations = LocationService.getLocations();
return new ResponseEntity<>(locations, HttpStatus.OK);
}
@GetMapping(value= waypoint, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value= waypoint1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Waypoints>> getWaypoints() {
List<Waypoints> waypoints = WaypointService.getWaypoints();
return new ResponseEntity<>(waypoints, HttpStatus.OK);
}
@PostMapping(value= location, produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createLocation(@RequestBody Location location) {
LocationService.createLocation(location);
return new ResponseEntity<>(HttpStatus.CREATED);

View File

@@ -0,0 +1,66 @@
package ch.progetto152.controller;
import ch.progetto152.entity.User;
import ch.progetto152.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/get/all")
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
@GetMapping("/get/{id}")
public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
User user = userService.getUserByIdService(id);
if (user != null) {
return new ResponseEntity<>(user, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/create/{id}")
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = userService.createUser(user);
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
}
@PutMapping("/put/{id}")
public ResponseEntity<User> updateUser(@PathVariable("id") Long id, @RequestBody User user) {
User updatedUser = userService.updateUser(id, user);
if (updatedUser != null) {
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable("id") Long id) {
boolean deleted = userService.deleteUser(id);
if (deleted) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}

View File

@@ -1,13 +1,20 @@
package ch.progetto152.entity;
import jakarta.persistence.*;
import lombok.*;
import java.util.Objects;
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table
public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Basic
@@ -20,35 +27,9 @@ public class User {
@Column(name = "password")
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
public User(String name, String username, String password) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

View File

@@ -0,0 +1,10 @@
package ch.progetto152.repository;
import ch.progetto152.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}

View File

@@ -2,6 +2,8 @@ package ch.progetto152.services;
import ch.progetto152.entity.User;
import ch.progetto152.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -9,8 +11,16 @@ import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAllUsers() {
return null;
System.out.println(userRepository.findAll());
return userRepository.findAll();
}
public User getUserByIdService(Long id) {
@@ -18,7 +28,7 @@ public class UserService {
}
public User createUser(User user) {
return null;
return userRepository.save(user);
}
public User updateUser(Long id, User user) {

View File

@@ -1,9 +1,11 @@
package ch.progetto152.services;
import ch.progetto152.entity.Waypoints;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WaypointService {
public static List<Waypoints> getWaypoints() {
return null;