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

1
.idea/misc.xml generated
View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager"> <component name="MavenProjectsManager">
<option name="originalFiles"> <option name="originalFiles">
<list> <list>

View File

@@ -49,6 +49,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -19,26 +19,26 @@ import java.util.List;
@RequestMapping(value = "api/progetto152", produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "api/progetto152", produces = MediaType.APPLICATION_JSON_VALUE)
public class Controller { public class Controller {
private static final String location = "location"; private static final String location1 = "location";
private static final String waypoint = "waypoint"; private static final String waypoint1 = "waypoint";
@Autowired @Autowired
private UserService userService; private UserService userService;
@GetMapping(value= location, produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Location>> getLocations() { public ResponseEntity<List<Location>> getLocations() {
List<Location> locations = LocationService.getLocations(); List<Location> locations = LocationService.getLocations();
return new ResponseEntity<>(locations, HttpStatus.OK); 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() { public ResponseEntity<List<Waypoints>> getWaypoints() {
List<Waypoints> waypoints = WaypointService.getWaypoints(); List<Waypoints> waypoints = WaypointService.getWaypoints();
return new ResponseEntity<>(waypoints, HttpStatus.OK); 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) { public ResponseEntity<String> createLocation(@RequestBody Location location) {
LocationService.createLocation(location); LocationService.createLocation(location);
return new ResponseEntity<>(HttpStatus.CREATED); 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; package ch.progetto152.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Table
public class User { public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") @Column(name = "id")
private int id; private int id;
@Basic @Basic
@@ -20,35 +27,9 @@ public class User {
@Column(name = "password") @Column(name = "password")
private String password; private String password;
public int getId() { public User(String name, String username, String password) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username; this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = 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.entity.User;
import ch.progetto152.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@@ -9,8 +11,16 @@ import java.util.List;
@Service @Service
public class UserService { public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> getAllUsers() { public List<User> getAllUsers() {
return null; System.out.println(userRepository.findAll());
return userRepository.findAll();
} }
public User getUserByIdService(Long id) { public User getUserByIdService(Long id) {
@@ -18,7 +28,7 @@ public class UserService {
} }
public User createUser(User user) { public User createUser(User user) {
return null; return userRepository.save(user);
} }
public User updateUser(Long id, User user) { public User updateUser(Long id, User user) {

View File

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

View File

@@ -1,4 +1,5 @@
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Progetto152 spring.datasource.url=jdbc:mysql://localhost:3306/Progetto152
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=Eragon05 spring.datasource.password=Admin123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect