Added reading data from database using springboot
This commit is contained in:
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
|
||||
5
pom.xml
5
pom.xml
@@ -49,6 +49,11 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>2.2.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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);
|
||||
|
||||
66
src/main/java/ch/progetto152/controller/UserController.java
Normal file
66
src/main/java/ch/progetto152/controller/UserController.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
10
src/main/java/ch/progetto152/repository/UserRepository.java
Normal file
10
src/main/java/ch/progetto152/repository/UserRepository.java
Normal 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> {
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/Progetto152
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=Eragon05
|
||||
spring.datasource.password=Admin123
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
Reference in New Issue
Block a user