Implement more database CRUDL endpoints
This commit is contained in:
parent
ab71d13d2b
commit
5eb33db38e
@ -1,8 +1,10 @@
|
|||||||
package com.example.demo;
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -18,6 +20,7 @@ import java.nio.channels.Channels;
|
|||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.websocket.server.PathParam;
|
import javax.websocket.server.PathParam;
|
||||||
@ -39,6 +42,27 @@ public class Controller {
|
|||||||
public Data _2(@RequestBody Data data){
|
public Data _2(@RequestBody Data data){
|
||||||
return database.save(data);
|
return database.save(data);
|
||||||
}
|
}
|
||||||
|
@GetMapping("/data/{id}")
|
||||||
|
public Optional<Data> _3(@PathVariable Long id) {
|
||||||
|
return database.findById(id);
|
||||||
|
}
|
||||||
|
@PutMapping("/data/{id}")
|
||||||
|
public Object _5(@PathVariable Long id,@RequestBody Data data) {
|
||||||
|
if (database.existsById(id)) {
|
||||||
|
return database.save(data);
|
||||||
|
} else {
|
||||||
|
return "ID "+id+" does not exist!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@DeleteMapping("/data/{id}")
|
||||||
|
public String _4(@PathVariable Long id) {
|
||||||
|
if (database.existsById(id)) {
|
||||||
|
database.deleteById(id);
|
||||||
|
return "Removed ID "+id+" from database.";
|
||||||
|
} else {
|
||||||
|
return "ID "+id+" does not exist!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void downloadFileFromUrl(String url, String file) throws IOException{
|
public static void downloadFileFromUrl(String url, String file) throws IOException{
|
||||||
File filer = new File(file);
|
File filer = new File(file);
|
||||||
|
@ -10,9 +10,11 @@ import javax.persistence.Id;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="data")
|
@Table(name="data")
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class Data {
|
public class Data {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user